Ubuntu 12.04创建第一个Qt5小程序(qmake编译Hello World)

Qt5.3.0,安装步骤点这里。

这里我们不用Qt Creator这个IDE。而是直接写自己的源文件函数,然后用qmake编译就行。步骤如下:

1) 新建文件夹,随便取名:HelloQt5。在HelloQt5目录下创建一个“SayHello.cpp”文件,

me@test:~/WorkSpace/HelloQt5$ ls
SayHello.cpp

2)编写“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();
}

3)创建项目文件:
me@test:~/WorkSpace/HelloQt5$ ls
SayHello.cpp
me@test:~/WorkSpace/HelloQt5$ qmake -project
me@test:~/WorkSpace/HelloQt5$ ls
HelloQt5.pro  SayHello.cpp

可以看到新生成了一个“HelloQt5.pro”文件。其所有内容如下:
Ubuntu 12.04创建第一个Qt5小程序(qmake编译Hello World)_第1张图片

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

可以看到新生成了“Makefile”文件。内容很长,就不贴了。


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

6)解决错误,继续编译:
打开HelloQt5.pro,添加一行“QT += widgets”,重新编译,错误消失,编译完成。全部代码如下:
Ubuntu 12.04创建第一个Qt5小程序(qmake编译Hello World)_第2张图片
可以看到生成了可执行文件HelloQt5(绿色)。

7)运行
./HelloQt5

结果如下:

Ubuntu 12.04创建第一个Qt5小程序(qmake编译Hello World)_第3张图片

你可能感兴趣的:(qmake,qt5入门实例)