user3182532
user3182532

Reputation: 1109

Qt Hello World Tutorial doesn't work

I am trying to run a hello world program and the tutorials don't work for me.

I am guessing that it's got something to do with qt4 <-> qt5 and linux <-> windows confusion.

I'm on Ubuntu 14.04, 64 bit. I did a sudo apt-get install build-essential and a sudo apt-get install qt5-default.

This is the code in my main.cpp:

#include <QApplication>
#include <QLabel>

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    QLabel *label = new QLabel("Linux is wonderful", 0);
    app.setMainWidget(label);
    label->show();
    return app.exec();
}

I run these commands:

qmake -project
qmake test.pro (for some reason it's not "main.pro")
make

Here is the generated .pro file:

###################################################################### 
# Automatically generated by qmake (3.0) So. Okt. 25 15:51:35 2015 
###################################################################### 
TEMPLATE = app 
TARGET = test 
INCLUDEPATH += . 
# Input 
SOURCES += mymain.cpp 

And then I get QApplication: No such file or directory. Why?

Upvotes: 0

Views: 378

Answers (1)

hyde
hyde

Reputation: 62848

You are missing the necessary module from .pro file. Apparently qmake -project does not add that by default (makes sense, since not all Qt apps are widget applications). So check for and add this:

QT += widgets

This is because Qt5 has widgets in a separate module (Qt4 had them in gui), and QApplication is part of that, as shown by docs too. The two modules which qmake adds automatically (and you have to remove if you don't want them) are core and gui (documented here), others you have to add to .pro explicitly.


Some notes: You generally run qmake -project only once to create initial .pro file. Then you need to edit it by hand, and don't want it to be overwritten! Then you never edit Makefiles by hand, instead you regenerate them by running qmake after editing the .pro file.

Upvotes: 1

Related Questions