Results 1 to 8 of 8

Thread: How to use connect() in IDE's(slots)

  1. #1
    Join Date
    Jun 2010
    Posts
    21
    Qt products
    Qt4
    Platforms
    Windows

    Default How to use connect() in IDE's(slots)

    hello guys i need a small help how do i connect().In QT designer we add a slots in .ui file but when we are using IDE's(code blocks) how do i define a slot?

    thank you

  2. #2
    Join Date
    Dec 2009
    Posts
    8
    Thanks
    4
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: How to use connect() in IDE's(slots)

    If I understand your question correctly, look at your generated ui.mainwindow.h file and you will see.

  3. #3
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to use connect() in IDE's(slots)

    You want to ask: how you define slots from code? In this case the answer is: just like a regular function (only that it have public slots: "access specifier") and it usually returns void (it can have a return value, but it is ignored when the slot is called from connection with signal, it can be used only if you call the slot like any other member function of your class)

  4. #4
    Join Date
    Jun 2010
    Posts
    21
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to use connect() in IDE's(slots)

    so i guess my code should be

    Qt Code:
    1. public slots:
    2. void my_slot()
    3. {
    4.  
    5. /* some code here */
    6.  
    7. }
    8.  
    9.  
    10. int main(int argc, char* argv[])
    11. {
    12.  
    13. QApplication app(argc, argv);
    14.  
    15.  
    16. QPushButton quit("Click Me");
    17.  
    18.  
    19. QObject::connect(&quit,SIGNAL(clicked()),&app,SLOT(my_slot()));
    20.  
    21. quit.show();
    22. return app.exec();
    23. }
    To copy to clipboard, switch view to plain text mode 

    am i right.

    and i am extremely sorry for not express my question properly.

  5. #5
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to use connect() in IDE's(slots)

    No, that code it's not working
    The way if you really need to "add" a slot to the QApplication object is: create a class that derives from QApplication and declare the public slots in the .h file of the class, and define them in .cpp

    But: I don't think you need that (at least not right now)

    I think you need to connect your clicked() signal of the button to a already existing slot of your application, like this:
    Qt Code:
    1. #include <QApplication>
    2. #include <QtGui>
    3.  
    4. int main(int argc, char* argv[])
    5. {
    6.  
    7. QApplication app(argc, argv);
    8.  
    9.  
    10. QPushButton quit("Click Me");
    11.  
    12.  
    13. QObject::connect(&quit,SIGNAL(clicked()),
    14. &app,SLOT(quit())); // connect to already existing slot quit() of your QApplication object (meaning app)
    15.  
    16. quit.show();
    17. return app.exec();
    18. }
    To copy to clipboard, switch view to plain text mode 

    LE: i didn't said "politically correct" in the previous post, so:
    this: "Slots are just like functions"
    should be: "Slots are just like member functions (of some class)"

  6. #6
    Join Date
    Jun 2010
    Posts
    21
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to use connect() in IDE's(slots)

    Quote Originally Posted by Zlatomir View Post
    The way if you really need to "add" a slot to the QApplication object is: create a class that derives from QApplication and declare the public slots in the .h file of the class, and define them in .cpp
    can you explain me about this in detail.please!

    all i want to do is when user clicks the push button it must update the old values and repaint so that new values appear.
    Last edited by wenn32; 6th July 2010 at 16:05.

  7. #7
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to use connect() in IDE's(slots)

    Quote Originally Posted by wenn32 View Post
    can you explain me about this in detail.please!

    all i want to do is when user clicks the push button it must update the old values and repaint so that new values appear.
    just look at the first example code provided with qt assistant

  8. #8
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to use connect() in IDE's(slots)

    I'm pretty sure that you don't want or need to subclass the QApplication, so tell us with more details what you want (like what widgets do you have, etc...) So we can give you a better advice.
    Most likely you will need to just use signals and slots to connect your widgets and update values

    LE:
    Also post the code that doesn't do what you want (or some example with the same issue)

    And for creating your own class by inherit from another, you need a little experience with c++, so you will need to do some tutorials or better read a book (maybe a c++ book first if you are not comfortable with it)
    Last edited by Zlatomir; 6th July 2010 at 16:30.

Similar Threads

  1. Replies: 16
    Last Post: 16th February 2010, 13:17
  2. Connect to...without slots...
    By Peppy in forum Qt Programming
    Replies: 2
    Last Post: 16th October 2009, 13:47
  3. Replies: 12
    Last Post: 23rd June 2008, 08:05
  4. function 'connect' always look super class for slots.
    By Intaek Lim in forum Qt Programming
    Replies: 7
    Last Post: 12th March 2007, 13:38
  5. Replies: 4
    Last Post: 10th November 2006, 15:38

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.