Results 1 to 2 of 2

Thread: Writing custom slot method based on QT example 1&2

  1. #1
    Join Date
    Jan 2007
    Posts
    12
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Writing custom slot method based on QT example 1&2

    Hi, I have a question. I've been studying through tutorials under Qt. What I notice is that when you want to implement your custom slots, you have to create a custom widget and implement the slot methods inside the class.

    But what if I want to create my own slots without having a need to create a class? Based on the simple example below, without creating a custom widget class, I want to implement a custom slot method "customMethod".

    Qt Code:
    1. #include <QApplication>
    2. #include <QPushButton>
    3. #include <QObject>
    4.  
    5. // I want to implement a method called customMethod here
    6. // tried void QApplication::customMethod() but didn't work
    7. void customMethod()
    8. {
    9. //I want to do something here.
    10. }
    11.  
    12. int main(int argc, char* argv[])
    13. {
    14. QApplication app(argc,argv);
    15. QPushButton button("Quit");
    16. QObject::connect(button,SIGNAL(clicked()), &app,SLOT(customMethod()));
    17. button.show();
    18.  
    19. return app.exec();
    20. }
    To copy to clipboard, switch view to plain text mode 

    I'm guessing that I can't do the above, and I have to create custom widget class or something. What I was thinking, for example, is there a way, based on above coding to add my own about messagebox, using QMessageBox::information(//parameters).

    Thanks in advance!

  2. #2
    Join Date
    Jan 2006
    Location
    Alingsås, Sweden
    Posts
    437
    Thanks
    3
    Thanked 39 Times in 39 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Writing custom slot method based on QT example 1&2

    There is no need to create a custom widget, but you will have to put your slot in a class inheriting QObject and including the Q_OBJECT macro. This is because slots are based on the meta data system, and only QObjects has meta objects.

    Since QWidget inherits QObject it has a is-a relations. I.e. QWidget is-a QObject thus has a meta object.

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.