Page 1 of 2 12 LastLast
Results 1 to 20 of 31

Thread: the simplest custom slot

  1. #1
    Join Date
    Nov 2007
    Posts
    103
    Thanks
    71
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default the simplest custom slot

    Hi all,

    I'm trying to figure out if I don't know how to make a custom slot or my compiler (Dev-C++) is not configured correctly to compile a program with a custom slot (everything compiles fine when I use a predefined slot). I'd be extremely thankful to you if you could either:
    1) send me the simplest fully functioning code (using just one *.cpp file) that uses a custom slot,
    or
    2) take a look at my code and suggest why this is not compiling and I get this error:
    [Linker error] undefined reference to `vtable for MyExit'

    Here's my code:
    Qt Code:
    1. #include <QApplication>
    2. #include <QPushButton>
    3. #include <QHBoxLayout>
    4.  
    5. using namespace std;
    6.  
    7. class MyExit : public QObject
    8. {
    9. Q_OBJECT
    10. public:
    11. MyExit (QObject *parent = 0);
    12. public slots:
    13. void myExitSlot();
    14. };
    15.  
    16. MyExit::MyExit(QObject *parent):QObject(parent)
    17. {
    18. }
    19.  
    20. void MyExit::myExitSlot()
    21. {
    22. qApp->quit();
    23. }
    24.  
    25. int main(int argc, char *argv[])
    26. {
    27. QApplication a(argc, argv);
    28. QWidget window;
    29. QHBoxLayout* mainLayout = new QHBoxLayout(&window);
    30. QPushButton *b1 = new QPushButton("Exit");
    31. mainLayout->addWidget(b1);
    32.  
    33. QObject::connect(b1, SIGNAL(clicked()), qApp, SLOT(myExitSlot()));
    34. window.show();
    35. return a.exec();
    36. }
    To copy to clipboard, switch view to plain text mode 

    Thanks a lot in advance. Tommy
    Last edited by jacek; 6th November 2007 at 00:07. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: the simplest custom slot

    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).

  3. #3
    Join Date
    Dec 2006
    Posts
    33
    Thanks
    10
    Thanked 5 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: the simplest custom slot

    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

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: the simplest custom slot

    Quote Originally Posted by jml View Post
    I'm not sure you can call connect from the main() routine; I believe it should be invoked from an object derived from QObject.
    No, you can call connect() from everywhere you want. It's a static method of QObject, so you just need to prefix it with QObject:: if you use it outside any QObject, just like the thread author did.

  5. #5
    Join Date
    Oct 2006
    Posts
    279
    Thanks
    6
    Thanked 40 Times in 39 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: the simplest custom slot

    The slot seems find, You need to connect to it, though.
    Qt Code:
    1. MyExit myExit;
    2. QObject::connect(b1, SIGNAL(clicked()), &myExit, SLOT(myExitSlot()));
    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()

  6. The following user says thank you to spud for this useful post:

    tommy (9th November 2007)

  7. #6
    Join Date
    Nov 2007
    Posts
    103
    Thanks
    71
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default thanks

    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:
    1. #include <QApplication>
    2. #include <QPushButton>
    3. #include <QHBoxLayout>
    4.  
    5. using namespace std;
    6.  
    7. class MyExit : public QObject
    8. {
    9. Q_OBJECT
    10.  
    11. public:
    12. MyExit (QObject *parent = 0);
    13. public slots:
    14. void myExitSlot();
    15. };
    16.  
    17.  
    18.  
    19. void MyExit::myExitSlot()
    20. {
    21. qApp->quit();
    22. }
    23.  
    24. MyExit::MyExit(QObject *parent):QObject(parent)
    25. {
    26. }
    27.  
    28.  
    29. int main(int argc, char *argv[])
    30. {
    31. QApplication a(argc, argv);
    32. QWidget window;
    33. QHBoxLayout* mainLayout = new QHBoxLayout(&window);
    34. QPushButton *b1 = new QPushButton("Exit");
    35.  
    36. mainLayout->addWidget(b1);
    37.  
    38. QObject::connect(b1, SIGNAL(clicked()), &MyExit, SLOT(myExitSlot()));
    39.  
    40. window.show();
    41. return a.exec();
    42. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 6th November 2007 at 21:08. Reason: missing [code] tags

  8. #7
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: the simplest custom slot

    Looks like no instance of MyExit is being created. Take a look at spud's post.
    J-P Nurmi

  9. #8
    Join Date
    Nov 2007
    Posts
    103
    Thanks
    71
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Kiitos jpn

    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

  10. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Kiitos jpn

    Quote Originally Posted by tommy View Post
    Unfortunately I don't exactly understand how to do what I'm supposed to do: create that instance or make the connection
    You can create connections only between objects, but MyExit is a class. You have to create an object of this class (just like you did with QWidget or QPushButton).

  11. #10
    Join Date
    Nov 2007
    Posts
    103
    Thanks
    71
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default hi

    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:
    1. #include <QApplication>
    2. #include <QPushButton>
    3. #include <QHBoxLayout>
    4.  
    5. using namespace std;
    6.  
    7. class MyExit : public QObject
    8. {
    9. Q_OBJECT
    10.  
    11. public:
    12. MyExit (QObject *parent = 0);
    13. public slots:
    14. void myExitSlot();
    15. };
    16.  
    17.  
    18.  
    19. void MyExit::myExitSlot()
    20. {
    21. qApp->quit();
    22. }
    23.  
    24. MyExit::MyExit(QObject *parent):QObject(parent)
    25. {
    26. }
    27.  
    28.  
    29. int main(int argc, char *argv[])
    30. {
    31. QApplication a(argc, argv);
    32. MyExit Exit;
    33. QWidget window;
    34. QHBoxLayout* mainLayout = new QHBoxLayout(&window);
    35. QPushButton *b1 = new QPushButton("Exit");
    36.  
    37. mainLayout->addWidget(b1);
    38.  
    39. QObject::connect(b1, SIGNAL(clicked()), &Exit, SLOT(myExitSlot()));
    40.  
    41. window.show();
    42. return a.exec();
    43. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 6th November 2007 at 21:07. Reason: missing [code] tags

  12. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: hi

    The code is OK. Do you use qmake to compile it?

  13. #12
    Join Date
    Nov 2007
    Posts
    103
    Thanks
    71
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default hi jacek

    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

  14. #13
    Join Date
    Nov 2007
    Posts
    3
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: hi

    Quote Originally Posted by jacek View Post
    The code is OK. Do you use qmake to compile it?
    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

  15. #14
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: hi

    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

  16. #15
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: hi jacek

    Quote Originally Posted by tommy View Post
    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?).
    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.

  17. The following user says thank you to jacek for this useful post:

    tommy (9th November 2007)

  18. #16
    Join Date
    Nov 2007
    Posts
    103
    Thanks
    71
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default hi jacek

    Thanks Jacek,

    You made a lot of things much more clear for me, I'm beginning to understand the basics now.
    I was able to use qmake just as you suggested but did not get it to work with Dev-C++. I'm thinking of abandoning Dev-C++ and trying command line (just get plain mingw). How do you include the makefile when you compile from command line? What's the syntax?

    Thankfully,
    Tommy

  19. #17
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: hi jacek

    Quote Originally Posted by tommy View Post
    I'm thinking of abandoning Dev-C++ and trying command line (just get plain mingw).
    I'm sure you can find some tutorials about using Dev-C++ for Qt applications on google.

  20. #18
    Join Date
    Sep 2007
    Location
    Szczecin, Poland
    Posts
    153
    Thanks
    7
    Thanked 11 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: the simplest custom slot

    Hi,
    I'm using qt on dev-cpp, so i had similar problem
    I'm not using qmake(it says it can't find FORCE target), but i didn't searched the reason of this error, because i'm using multiple makefiles to have effect of several projects in one solution (dev-cpp works on projects only).
    You need to set use custom makefile in projects options, add a tatget *.moc, with 2 commands:
    1 moc your *.h to *.moc(or moc_*.cpp)
    2 compile generated file

    or the simpler way makefile that calls only qmake & make on file generated by, unfortunately i can't test this because of mentioned qmake error.

  21. #19
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: the simplest custom slot

    Quote Originally Posted by mchara View Post
    I'm not using qmake(it says it can't find FORCE target), but i didn't searched the reason of this error
    What is the value of the TARGET variable in your .pro file? If it isn't set, what is the name of the .pro file?

    Quote Originally Posted by mchara View Post
    You need to set use custom makefile in projects options, add a tatget *.moc, with 2 commands:
    1 moc your *.h to *.moc(or moc_*.cpp)
    2 compile generated file
    What about all of those magic macros Qt needs?

  22. #20
    Join Date
    Sep 2007
    Location
    Szczecin, Poland
    Posts
    153
    Thanks
    7
    Thanked 11 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: the simplest custom slot

    Quote Originally Posted by jacek View Post
    What is the value of the TARGET variable in your .pro file? If it isn't set, what is the name of the .pro file?
    Doesn't matter, i have the same error for every makefile generated by qmake, i found that
    FORCE: is an empty target used to force something to be build even if there's no dependencies so it shall be empty definition.
    It is in deed in generated makefiles' but i suppose that problem might be in missing tab & newline after FORCE: - some make's are sensitive for tabs instead of spaces etc., i think make is one of them.
    What about all of those magic macros Qt needs?
    What macros?
    DO you mean those defines at the beginning
    Qt Code:
    1. -DUNICODE -DQT_LARGEFILE_SUPPORT -DQT_DLL -DQT_NO_DEBUG -DQT_CORE_LIB -DQT_GUI_LIB -DQT_THREAD_SUPPORT -DQT_NEEDS_QMAIN
    To copy to clipboard, switch view to plain text mode 
    , i've copied them from first makefile found in qt's exapmles & worked fine.

    Those are always the same view defines(& view more depending on used modules -DQT_*_LIB) so it's not much work.

    I thought that maybe custom makefie that would have defined only qmake -project; qmake &make on newly generated makefile would solve the problem if we would find what's wrong with FORCE error... can anyone check is this common problem on mingw or just mine?

    Oh, one more thing : make sure to not use --short-double or --expensive-optimizations because using one of them causes QPainter::drawPixmap()/drawImage() stops working... it's really strange but i've checked it several times & it really happens.

Similar Threads

  1. How to create Custom Slot upon widgets
    By ashukla in forum Qt Programming
    Replies: 6
    Last Post: 8th September 2007, 14:07
  2. Connect Button to a custom slot
    By Majestade in forum Qt Programming
    Replies: 2
    Last Post: 28th March 2007, 17:17
  3. Replies: 1
    Last Post: 26th January 2007, 07:10
  4. custom slot + Designer
    By bashamehboob in forum Qt Tools
    Replies: 1
    Last Post: 28th April 2006, 15:17
  5. How to create custom slot in Qt Designer 4.1?
    By jamadagni in forum Qt Tools
    Replies: 31
    Last Post: 18th January 2006, 20:46

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.