歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> 在Ubuntu上編寫Qt Helloworld程序

在Ubuntu上編寫Qt Helloworld程序

日期:2017/3/1 9:42:34   编辑:Linux編程

不使用Qt Creator IDE的情況下,我只使用自己喜歡的Emacs和命令行來創建一個hello world工程。

Ubuntu 環境下Gtk與QT編譯環境安裝與配置 http://www.linuxidc.com/Linux/2013-08/88539.htm

Linux系統下QT環境搭建 http://www.linuxidc.com/Linux/2013-07/87576.htm

Ubuntu下QT控制台程序無法運行的解決方案以及XTerm的配置方法 http://www.linuxidc.com/Linux/2013-06/86244.htm

Ubuntu 10.04下QT4.7.4移植詳解 http://www.linuxidc.com/Linux/2013-01/77930.htm

Ubuntu 14.04下安裝部署Qt5開發環境 http://www.linuxidc.com/Linux/2014-05/101774.htm

1. 確保/usr/bin/qmake指向Qt 5.2.0安裝的qmake

/usr/bin# mv qmake qmake_bk
/usr/bin# ln -s /home/likewise-open/CHN/shu6889/Qt5.2.0/5.2.0/gcc_64/bin/qmake ./qmake

2. 寫代碼:

創建helloworld目錄,添加main.cc文件

#include <QtWidgets/QApplication>
#include <QtWidgets/QLabel>
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
QLabel hello("<center>Welcome to my first WikiHow Qt program</center>");
hello.setWindowTitle("My First WikiHow Qt Program");
hello.resize(400, 400);
hello.show();
return app.exec();
}

3. 創建project文件

在helloworld目錄下,運行下面的命令:

qmake -project

現在,我得到了helloworld.pro文件。

######################################################################
# Automatically generated by qmake (3.0) ?? 12? 16 15:49:40 2013
######################################################################
TEMPLATE = app
TARGET = helloworld
INCLUDEPATH += .
# Input
SOURCES += main.cc

4. 編譯

運行qmake,不帶參數。這樣就得到了Makefile文件。

qmake
make

得到-lGL的鏈接錯誤,安裝mesa

apt-get install libgl1-mesa-dev

但是得到了一些新的鏈接錯誤:

main.o: In function `main':
main.cc:(.text.startup+0x27): undefined reference to `QApplication::QApplication(int&, char**, int)'
main.cc:(.text.startup+0x56): undefined reference to `QLabel::QLabel(QString const&, QWidget*, QFlags<Qt::WindowType>)'
main.cc:(.text.startup+0x84): undefined reference to `QWidget::setWindowTitle(QString const&)'
main.cc:(.text.startup+0xa9): undefined reference to `QWidget::resize(QSize const&)'
main.cc:(.text.startup+0xb1): undefined reference to `QWidget::show()'
main.cc:(.text.startup+0xb6): undefined reference to `QApplication::exec()'
main.cc:(.text.startup+0xc1): undefined reference to `QLabel::~QLabel()'
main.cc:(.text.startup+0xc9): undefined reference to `QApplication::~QApplication()'
main.cc:(.text.startup+0xe5): undefined reference to `QApplication::~QApplication()'
main.cc:(.text.startup+0x106): undefined reference to `QLabel::~QLabel()'
main.cc:(.text.startup+0x118): undefined reference to `QLabel::~QLabel()'
collect2: error: ld returned 1 exit status
make: *** [helloworld] Error 1

在helloworld.pro文件中添加一行:

QT += widgets

重新編譯。好了。

運行hellowordl程序,看到Qt窗口了。

當切換到Windows上開發時,可以不使用命令行開發。直接用Qt Creator打開這個helloworld.pro文件,會默認使用一些配置。編譯運行,成功了。

Qt 的詳細介紹:請點這裡
Qt 的下載地址:請點這裡

更多Ubuntu相關信息見Ubuntu 專題頁面 http://www.linuxidc.com/topicnews.aspx?tid=2

Copyright © Linux教程網 All Rights Reserved