一、Qt的安装
使用的Linux系统Ubuntu14.10是已经安装好QT的
查看Qt版本,执行
qmake -vQt
可以看到版本为:version 4.8.6
若没有的话,自己安装一个也不难,相关教程网上都有,这里就不多说了。
二、开始编写第一个Qt程序:hello world
1、编写源文件:hello.cpp
#include <qapplication.h>
#include <qlabel.h>
int main(int argc, char **argv)
{
QApplication a(argc, argv); //create a window
QLabel hello("Hello world!", 0); //create a label
hello.resize(150, 50); //set size of label: width 150,high 50
//a.setMainWidget(&hello); //
hello.show(); //show the label
return a.exec(); //draw the form
}
2、建立工程文件:hello.pro
SOURCES = hello.cpp
HEADERS =
CONFIG += qt warn_on release
3、编译和运行程序
(1)预处理工程,生成makefile文件:
qmake -o makefile hello.pro
(2)编译程序:
make
(3)运行程序:
./hello
三、注意
源文件中注释了一行代码,不注释会发现提示错误:
“setMainWidget”: 不是“QApplication”的成员
这是由于setMainWidget是QT3的,QT4以后就不用了,没必要再setmainwidget了,直接删掉,并show出你的窗口就行了
使用的Linux系统Ubuntu14.10是已经安装好QT的
查看Qt版本,执行
qmake -vQt
可以看到版本为:version 4.8.6
若没有的话,自己安装一个也不难,相关教程网上都有,这里就不多说了。
二、开始编写第一个Qt程序:hello world
1、编写源文件:hello.cpp
#include <qapplication.h>
#include <qlabel.h>
int main(int argc, char **argv)
{
QApplication a(argc, argv); //create a window
QLabel hello("Hello world!", 0); //create a label
hello.resize(150, 50); //set size of label: width 150,high 50
//a.setMainWidget(&hello); //
hello.show(); //show the label
return a.exec(); //draw the form
}
2、建立工程文件:hello.pro
SOURCES = hello.cpp
HEADERS =
CONFIG += qt warn_on release
3、编译和运行程序
(1)预处理工程,生成makefile文件:
qmake -o makefile hello.pro
(2)编译程序:
make
(3)运行程序:
./hello
三、注意
源文件中注释了一行代码,不注释会发现提示错误:
“setMainWidget”: 不是“QApplication”的成员
这是由于setMainWidget是QT3的,QT4以后就不用了,没必要再setmainwidget了,直接删掉,并show出你的窗口就行了