Results 1 to 4 of 4

Thread: Connecting SIGNAL to functions

  1. #1
    Join Date
    Oct 2014
    Posts
    4
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Connecting SIGNAL to functions

    Is there any way to connect SIGNAL to a function which have global scope? Like when button is clicked a function display_error() is evoked? Is it possible or I would need to make a custom class for the widget inheriting from QPushButton adding display_error function in it?
    I want to ignore the Object Oriented design as I am more of a C guy.

  2. #2
    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: Connecting SIGNAL to functions

    It is possible in Qt5. In Qt4 you'd have to create a wrapper class to make the call to the function.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Oct 2014
    Posts
    4
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Connecting SIGNAL to functions

    A custom class inheriting from QObject who have display_error() function as SLOT would work? like this example:
    Qt Code:
    1. #include <QObject>
    2.  
    3. class Counter : public QObject
    4. {
    5. Q_OBJECT
    6.  
    7. public:
    8. Counter() { m_value = 0; }
    9.  
    10. int value() const { return m_value; }
    11.  
    12. public slots:
    13. void setValue(int value);
    14. void display_error();
    15.  
    16. signals:
    17. void valueChanged(int newValue);
    18.  
    19. private:
    20. int m_value;
    21. };
    To copy to clipboard, switch view to plain text mode 

  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: Connecting SIGNAL to functions

    Yes, but I rather meant that the class accepts a function pointer as its constructor's argument and executes the function when it receives a signal. Then you can attach any global function to it.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. connecting Pop up Menu List to actions/functions
    By ebsaith in forum Qt Programming
    Replies: 3
    Last Post: 19th June 2013, 07:43
  2. Connecting signal and slot by name
    By grayfox in forum Qt Programming
    Replies: 1
    Last Post: 22nd July 2011, 09:00
  3. Replies: 1
    Last Post: 14th March 2011, 10:21
  4. dynamic signal connecting
    By donglebob in forum Qt Programming
    Replies: 4
    Last Post: 19th November 2008, 09:58
  5. Connecting signal to button
    By steg90 in forum Qt Programming
    Replies: 8
    Last Post: 13th December 2007, 10:37

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.