Qt5.3.0,安装步骤点这里。
这里我们不用Qt Creator这个IDE。而是直接写自己的源文件函数,然后用qmake编译就行。步骤如下:
1) 新建文件夹,随便取名:HelloQt5。在HelloQt5目录下创建一个“SayHello.cpp”文件,
me@test:~/WorkSpace/HelloQt5$ ls SayHello.cpp
#include <QtWidgets/QApplication> #include <QtWidgets/QtWidgets> #include <QtGui/QPalette> #include <QtWidgets/QLabel> int main(int argc, char *argv[]) { QApplication app(argc,argv); QWidget *widget=new QWidget(); widget->setAutoFillBackground(true); widget->resize(300,200); //设置QPalette对象的背景属性(颜色或图片) QPalette palette; palette.setColor(QPalette::Background, QColor(192,253,123)); //palette.setBrush(QPalette::Background, QBrush(QPixmap(":/background.png"))); widget->setPalette(palette); // 在widget里面输出一句话 QLabel *label = new QLabel("Hello World!", widget); label->setText("The first Qt5 program!"); widget->show(); return app.exec(); }
me@test:~/WorkSpace/HelloQt5$ ls SayHello.cpp me@test:~/WorkSpace/HelloQt5$ qmake -project me@test:~/WorkSpace/HelloQt5$ ls HelloQt5.pro SayHello.cpp
可以看到新生成了一个“HelloQt5.pro”文件。其所有内容如下:
4)预编译
me@test:~/WorkSpace/HelloQt5$ ls SayHello.cpp me@test:~/WorkSpace/HelloQt5$ qmake -project me@test:~/WorkSpace/HelloQt5$ ls HelloQt5.pro SayHello.cpp me@test:~/WorkSpace/HelloQt5$ qmake me@test:~/WorkSpace/HelloQt5$ ls HelloQt5.pro Makefile SayHello.cpp
5) 编译
me@test:~/WorkSpace/HelloQt5$ ls SayHello.cpp me@test:~/WorkSpace/HelloQt5$ qmake -project me@test:~/WorkSpace/HelloQt5$ ls HelloQt5.pro SayHello.cpp me@test:~/WorkSpace/HelloQt5$ qmake me@test:~/WorkSpace/HelloQt5$ ls HelloQt5.pro Makefile SayHello.cpp me@test:~/WorkSpace/HelloQt5$ make g++ -c -pipe -O2 -Wall -W -D_REENTRANT -fPIE -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -I/opt/Qt5.3.0/5.3/gcc_64/mkspecs/linux-g++ -I. -I. -I/opt/Qt5.3.0/5.3/gcc_64/include -I/opt/Qt5.3.0/5.3/gcc_64/include/QtGui -I/opt/Qt5.3.0/5.3/gcc_64/include/QtCore -I. -o SayHello.o SayHello.cpp g++ -Wl,-O1 -Wl,-rpath,/opt/Qt5.3.0/5.3/gcc_64 -Wl,-rpath,/opt/Qt5.3.0/5.3/gcc_64/lib -o HelloQt5 SayHello.o -L/opt/Qt5.3.0/5.3/gcc_64/lib -lQt5Gui -lQt5Core -lGL -lpthread SayHello.o: In function `main': SayHello.cpp:(.text.startup+0x20): undefined reference to `QApplication::QApplication(int&, char**, int)' SayHello.cpp:(.text.startup+0x39): undefined reference to `QWidget::QWidget(QWidget*, QFlags<Qt::WindowType>)' SayHello.cpp:(.text.startup+0x46): undefined reference to `QWidget::setAutoFillBackground(bool)' SayHello.cpp:(.text.startup+0x63): undefined reference to `QWidget::resize(QSize const&)' SayHello.cpp:(.text.startup+0xd0): undefined reference to `QWidget::setPalette(QPalette const&)' SayHello.cpp:(.text.startup+0x100): undefined reference to `QLabel::QLabel(QString const&, QWidget*, QFlags<Qt::WindowType>)' SayHello.cpp:(.text.startup+0x128): undefined reference to `QLabel::setText(QString const&)' SayHello.cpp:(.text.startup+0x13a): undefined reference to `QWidget::show()' SayHello.cpp:(.text.startup+0x13f): undefined reference to `QApplication::exec()' SayHello.cpp:(.text.startup+0x155): undefined reference to `QApplication::~QApplication()' SayHello.cpp:(.text.startup+0x16e): undefined reference to `QApplication::~QApplication()' collect2: ld returned 1 exit status make: *** [HelloQt5] Error 1