Results 1 to 20 of 31

Thread: the simplest custom slot

Hybrid View

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

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

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

    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

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    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.

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

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

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

    tommy (9th November 2007)

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

    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 22:08. Reason: missing [code] tags

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

    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

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

    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

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

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

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

    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 22:07. Reason: missing [code] tags

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

    Default Re: hi

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

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

    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

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

    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.

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

    tommy (9th November 2007)

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

    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

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

    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

Similar Threads

  1. How to create Custom Slot upon widgets
    By ashukla in forum Qt Programming
    Replies: 6
    Last Post: 8th September 2007, 15:07
  2. Connect Button to a custom slot
    By Majestade in forum Qt Programming
    Replies: 2
    Last Post: 28th March 2007, 18:17
  3. Replies: 1
    Last Post: 26th January 2007, 08:10
  4. custom slot + Designer
    By bashamehboob in forum Qt Tools
    Replies: 1
    Last Post: 28th April 2006, 16: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, 21: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
  •  
Qt is a trademark of The Qt Company.