If you put Q_OBJECT macro inside a .cpp file, you have to add #include "filename.moc" at the end to let qmake know that it has to invoke moc on that file (normally qmake looks for Q_OBJECT only in header files).
If you put Q_OBJECT macro inside a .cpp file, you have to add #include "filename.moc" at the end to let qmake know that it has to invoke moc on that file (normally qmake looks for Q_OBJECT only in header files).
I'm not sure you can call connect from the main() routine; I believe it should be invoked from an object derived from QObject. Try moving your class declaration to a .h flie, and do the connect from the MyExit constructor.
Jim L
Seattle, WA
The slot seems find, You need to connect to it, though.
Qt Code:
MyExit myExit;To copy to clipboard, switch view to plain text mode
Right now your connecting to a QApplication object, which doesn't have the slot myExitSlot()![]()
tommy (9th November 2007)
Thanks to all who responded to me. You have given me lots of ideas and helped me a lot.
For example I did not know that custom slot classes should be in the *.h file with mingw compiler. I had absolutely no idea that you could include "filename.moc" file if your custom class is in the *.cpp file. I did not get this to work yet but it it good to know that such an option exists. Thanks for pointing out that I had the slot connected all wrong, too.
So I guess I have to start using *.h files for classes and I will. But at the same time I'm very curious if my compiler has some kind of a problem. I'd appreciate it a lot if somone was able to run the below code through their compiler and let me know what kind of error messages other compilers gave. Or perhaps it'll work with other compilers no problem.
Thanks a lot.
Here's the code:
Qt Code:
#include <QApplication> #include <QPushButton> #include <QHBoxLayout> using namespace std; { Q_OBJECT public: public slots: void myExitSlot(); }; void MyExit::myExitSlot() { qApp->quit(); } { } int main(int argc, char *argv[]) { QWidget window; mainLayout->addWidget(b1); window.show(); return a.exec(); }To copy to clipboard, switch view to plain text mode
Last edited by wysota; 6th November 2007 at 22:08. Reason: missing [code] tags
Looks like no instance of MyExit is being created. Take a look at spud's post.
J-P Nurmi
Thanks jpn.
Unfortunately I don't exactly understand how to do what I'm supposed to do: create that instance or make the connection (I just started with Qt a few days ago). Do you mind showing me the lines.
Thanks a lot.
Tommy
Great. I created the instance required but the code still won't compile. And it won't compile when I put the new class in a *.h file either.
Does anyone have any more ideas?
Thankfully - Tommy.
Code:
Qt Code:
#include <QApplication> #include <QPushButton> #include <QHBoxLayout> using namespace std; { Q_OBJECT public: public slots: void myExitSlot(); }; void MyExit::myExitSlot() { qApp->quit(); } { } int main(int argc, char *argv[]) { MyExit Exit; QWidget window; mainLayout->addWidget(b1); window.show(); return a.exec(); }To copy to clipboard, switch view to plain text mode
Last edited by wysota; 6th November 2007 at 22:07. Reason: missing [code] tags
The code is OK. Do you use qmake to compile it?
hi Jacek
Thanks for confirming my code. No, I do not use qmake. I'm going to need
another piece of help here if you don't mind because I do not know how to use qmake.
My compiler is Dev-C++ (mingw). I guess I have to put "qmake" somewhere in the
project options sections? Or is qmake a standalone program you run before or when you compile? (instead of your compiler?).
Thanks to you and the other helpful people my problem is now narrowed down to
compiler/linker problem and most likely qmake.
Tommy
qmake is a tool that generates makefiles from .pro files, which are much simplier. To generate an initial .pro file you have to run "qmake -project" in the directory where your project is. Then you run "qmake" or "qmake filename.pro" to generate makefile. I don't use Dev-C++, but AFAIR you can make it use a custom makefile, so you just have to point it to the makefile generated by qmake.
tommy (9th November 2007)
Hi Jacek,
I was having a look at Tommy's code and I am a bit confused. In this MyExitSlot() implementation, I see he is calling qapp.quit(); and I dont see a QApplication object named as qapp. Shouldnt he be declaring one in the private section of MyExit ? I think thats the reason why the compiler is complaining because there is no reference to the method in the vtable, since there is no object defined. Right ? Please correct me if I am wrong.
Thanks
- Rahul
See qApp in QApplication docs:
A global pointer referring to the unique application object. It is equivalent to the pointer returned by the QCoreApplication::instance() function except that, in GUI applications, it is a pointer to a QApplication instance.
J-P Nurmi
Bookmarks