2

I download this : http://get.qt.nokia.com/qt/source/qt-mac-opensource-4.7.2.dmg and install it. Then I got a Qt helloworld.cc.

 #include <QApplication>
 #include <QPushButton>

 int main(int argc, char *argv[])
 {
     QApplication app(argc, argv);

     QPushButton hello("Hello world!");
     hello.resize(100, 30);

     hello.show();
     return app.exec();
 }

I tried to compile it but failed. "

‘QApplication’ was not declared in this scope "

How can I fix it?

Josh Morrison
  • 7,488
  • 25
  • 67
  • 86

2 Answers2

4

you can also do this (I hope I'm not wrong):

$ qmake -project
$ qmake
$ make

of course you should cd to your source file. Also, I think it's a good idea to stick to the *.cpp file naming when you work with Qt

ak.
  • 3,329
  • 3
  • 38
  • 50
  • qmake on MAC would create by default xproject project. have to use qmake with a switch. e.g: qmake -spec macx-g++ – Valentin H Mar 13 '11 at 19:58
  • @koan: I'm not saying that there are problems, it's just a good idea. For me the *.cc files are associated with pure C. @Valentin Heinitz: I have compiled Qt from sources, and it generates a makefile by default. I think it's configurable from within the .pro file - what is generated by default. – ak. Mar 14 '11 at 18:46
  • In what world are .cc files associated with pure C ? See http://en.wikipedia.org/wiki/C%2B%2B – koan Mar 14 '11 at 21:05
  • @koan: you are right, that was just in my world, but here is a link on this specific topic: http://stackoverflow.com/questions/1545080/correct-c-code-file-extension-cc-vs-cpp – ak. Mar 14 '11 at 22:26
3

It works here for me. You didn't show your command line, but it seems like you aren't passing the right flags to tell your compiler where the headers/frameworks are. Here's what I used:

g++ -I /Library/Frameworks/QtGui.framework/Versions/4/Headers \
    -o example example.cpp \
    -framework QtGui -framework QtCore
Carl Norum
  • 219,201
  • 40
  • 422
  • 469