Results 1 to 12 of 12

Thread: How to send data from a static function of other Class to a MainWindow function?

  1. #1
    Join Date
    Jul 2010
    Location
    /home/hakermania/
    Posts
    233
    Thanks
    129
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Unhappy How to send data from a static function of other Class to a MainWindow function?

    I have a class called Global and it does a repetitive task once in a while via a static function... This function is called through many different classes so it is important to be static so as not to create objects all the time.

    glob.h:
    Qt Code:
    1. class Global: public QObject
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. static void send_name(QString color_name);
    7. ...
    8.  
    9. Q_SIGNALS:
    10. send_color(QString color_name);
    11. };
    To copy to clipboard, switch view to plain text mode 

    glob.cpp:
    Qt Code:
    1. void Global::send_name(QString color_name){
    2. if(_window_loaded_){
    3. /*SEND MESSAGE WITH COLOR_NAME*/
    4. Q_EMIT send_color(color_name);
    5. }
    6. }
    To copy to clipboard, switch view to plain text mode 

    Inside my MainWindow class I have to connect the send_color message sent by my static function to a local function so as to update some gui content.

    I know that there are many many threads about this but, believe me, I've searched all of them and nothing worked for me. I searched in depth the mailing lists and still nothing. I even tried to use QCustomEvent with qApplication->postEvent() but it is deprecated and it is suggested to use QEvent , which doesn't have setdata() function though, so it is no usable for this case (?)

    I've spent my morning searching about this, so all suggestions are appreciated!
    When you 're trying to help somebody in the newbie section, don't forget that he is a newbie. Be specific and give examples.

  2. #2
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to send data from a static function of other Class to a MainWindow function?

    It's an interesting interpretation, but I would make this in other way: make this object as a singleton
    Qt Code:
    1. class Global: public QObject
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. static Global *instance();
    7.  
    8. void send_name(const QString &color_name);
    9. ...
    10.  
    11. Q_SIGNALS:
    12. void send_color(const QString &color_name);
    13.  
    14. private:
    15. Q_DISABLE_COPY(Global);
    16. };
    17.  
    18. #define myGlobal Global::instance() //shortcut
    To copy to clipboard, switch view to plain text mode 

    Then
    Qt Code:
    1. ...
    2. connect(myGlobal, SIGNAL(send_color(const QString &)), SLOT(applyColor(const QString &)));
    3. ...
    4. myGlobal->send_name("lightgray");
    To copy to clipboard, switch view to plain text mode 
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  3. The following user says thank you to spirit for this useful post:

    hakermania (4th September 2012)

  4. #3
    Join Date
    Jul 2010
    Location
    /home/hakermania/
    Posts
    233
    Thanks
    129
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to send data from a static function of other Class to a MainWindow function?

    Quote Originally Posted by spirit View Post
    It's an interesting interpretation, but I would make this in other way: make this object as a singleton
    Qt Code:
    1. class Global: public QObject
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. static Global *instance();
    7.  
    8. void send_name(const QString &color_name);
    9. ...
    10.  
    11. Q_SIGNALS:
    12. void send_color(const QString &color_name);
    13.  
    14. private:
    15. Q_DISABLE_COPY(Global);
    16. };
    17.  
    18. #define myGlobal Global::instance() //shortcut
    To copy to clipboard, switch view to plain text mode 

    Then
    Qt Code:
    1. ...
    2. connect(myGlobal, SIGNAL(send_color(const QString &)), SLOT(applyColor(const QString &)));
    3. ...
    4. myGlobal->send_name("lightgray");
    To copy to clipboard, switch view to plain text mode 
    Thanks for your reply but I am not 100% sure that I understand your code. I want to call a Global static function from my MainWindow class, let's say
    Qt Code:
    1. Global::set_new_color();
    To copy to clipboard, switch view to plain text mode 
    and then, from within set_new_color I want to send a message back to mainwindow getting the color_name. From your code I assume that the send_name function then emits the send_color signal? If I use
    Qt Code:
    1. Q_EMIT send_color(color_name);
    To copy to clipboard, switch view to plain text mode 
    it doesn't let me emit it without an object... And I cannot create a new Global instance, because, as far as I understood from your code, Q_DISABLE_COPY avoids me from doing so...

    If you want, I will provide you with example files.
    When you 're trying to help somebody in the newbie section, don't forget that he is a newbie. Be specific and give examples.

  5. #4
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to send data from a static function of other Class to a MainWindow function?

    Quote Originally Posted by hakermania View Post
    it doesn't let me emit it without an object... And I cannot create a new Global instance, because, as far as I understood from your code, Q_DISABLE_COPY avoids me from doing so...
    You should implement Global::instance. I left this for you.
    HINT: you'll be able to instantiate a Global object in Global::instance.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  6. #5
    Join Date
    Jul 2010
    Location
    /home/hakermania/
    Posts
    233
    Thanks
    129
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to send data from a static function of other Class to a MainWindow function?

    Quote Originally Posted by spirit View Post
    You should implement Global::instance. I left this for you.
    HINT: you'll be able to instantiate a Global object in Global::instance.
    But, as I said I haven't understood your code or what you're trying to achieve there...
    When you 're trying to help somebody in the newbie section, don't forget that he is a newbie. Be specific and give examples.

  7. #6
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to send data from a static function of other Class to a MainWindow function?

    What's not clear? It's a simple singleton implementation -- Q_DISABLE_COPY macro disables copy ctor & and assignment operator.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  8. #7
    Join Date
    Sep 2011
    Posts
    1,245
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to send data from a static function of other Class to a MainWindow function?

    "Inside my MainWindow class I have to connect the send_color message sent by my static function to a local function so as to update some gui content."

    Why not just emit that string as a signal from the class that uses Global, and connect those signals to the mainwindow???


    You have some widget (actually lots of widgets), W, Global, G, and main window, M:

    W -> G -> M.

    You could just have

    W -> M.

    So what are you doing?
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  9. #8
    Join Date
    Jul 2010
    Location
    /home/hakermania/
    Posts
    233
    Thanks
    129
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to send data from a static function of other Class to a MainWindow function?

    Quote Originally Posted by amleto View Post
    Why not just emit that string as a signal from the class that uses Global, and connect those signals to the mainwindow???
    Ok, I see your point, but hold on a little bit. I don't know if I made myself 100% clear.

    Example (wrong) code:

    MainWindow class:

    Qt Code:
    1. ...
    2. connect( Global::instance(), SIGNAL(mysignal(QString)), this, SLOT(update_gui(QString)); //<-instructor of MainWindow
    3. ...
    4. Global::do_stuff(); ///<-- inside some other mainwindow function.. (dostuff() is a MUST BE static function)
    5.  
    6. ...
    7. void MainWindow::update_gui(QString name){
    8. ui->pushButton->setText(name); //<- simple example
    9. }
    To copy to clipboard, switch view to plain text mode 

    inside this function I have this:

    Qt Code:
    1. void Global::do_stuff(QString name){
    2. //do a lot of things...
    3. ...
    4. ...
    5. if(MainWindow_is_visible){
    6. //generate a color_name depending on the previously done things...
    7. QString color_name = previous_stuff.component.at(3).toString(); //<- example
    8. Q_EMIT mysignal(color_name);
    9. }
    10. }
    To copy to clipboard, switch view to plain text mode 

    So, is your saying possible for my situation where I need the previous things of the do_stuff() function so as to emit the proper signal?

  10. #9
    Join Date
    Sep 2011
    Posts
    1,245
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to send data from a static function of other Class to a MainWindow function?

    I highly doubt you truly have a 'MUST BE' global static. It just points to bad design.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  11. #10
    Join Date
    Jul 2010
    Location
    /home/hakermania/
    Posts
    233
    Thanks
    129
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to send data from a static function of other Class to a MainWindow function?

    Quote Originally Posted by amleto View Post
    I highly doubt you truly have a 'MUST BE' global static. It just points to bad design.
    Nope, seriously, this function is being called from almost every class. Would you recommend making class objects and calling the function through there?

  12. #11
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,481
    Thanks
    252
    Thanked 711 Times in 701 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: How to send data from a static function of other Class to a MainWindow function?

    I think you don't understand the concept of a "Singleton".

    Basically, a singleton is an object that can only have a single instance. That instance can be used exactly like a global class with a static method in most cases.

    So, you implement your Global class without a static function for doing the global stuff (it is just an ordinary member function, which must be called through an instance pointer). Every time you want to call that method, you get a pointer to the global instance of your class (using the getInstance() static method, and call it. Since that pointer is a genuine pointer to an instance, the doStuff() method can emit signals, get connected to slots, whatever.

    Qt Code:
    1. // Global.h
    2. class Global : public QObject
    3. {
    4. Q_OBJECT
    5.  
    6. public:
    7. static Global * getInstance();
    8.  
    9. public slots:
    10. void doStuff();
    11.  
    12. signals:
    13. void somethingChanged();
    14.  
    15. private:
    16. Global( QObject * pParent = 0 );
    17. virtual ~Global() = 0; // singleton can't be deleted by a caller of getInstance()
    18. };
    19.  
    20. // Global.cpp
    21.  
    22. #include "Global.h"
    23.  
    24. static Global * theInstance = 0;
    25.  
    26. Global * Global::getInstance()
    27. {
    28. if ( theInstance == 0 )
    29. theInstance = new Global();
    30. return theInstance;
    31. }
    32.  
    33. Global:: Global( QObject * pParent )
    34. : QObject( pParent )
    35. {
    36. // Initialize whatever else needs to be done
    37. }
    38.  
    39. void Global::doStuff();
    40. {
    41. // do what you need to do
    42.  
    43. emit somethingChanged();
    44. }
    45.  
    46. // someOtherCode.cpp
    47.  
    48. #include "Global.h"
    49.  
    50. SomeOtherCode::SomeOtherCode( QObject * pParent )
    51. : QObject( pParent )
    52. {
    53. connect( Global::getInstance(), SIGNAL( somethingChanged() ), this, SLOT( whoaGlobalStuffHappened() ) );
    54.  
    55. // Two ways to kill the same bird:
    56. connect( somePushButton, SIGNAL( clicked() ), Global::getInstance(), SLOT( doStuff() ) );
    57. connect( someOtherButton, SIGNAL( clicked() ), this, SLOT( doTheGlobalThing() ) );
    58. }
    59.  
    60. void SomeOtherCode::doTheGlobalThing()
    61. {
    62. Global::getInstance()->doStuff();
    63. }
    To copy to clipboard, switch view to plain text mode 

    All the same functionality as your Global class with the static method, except you get to use all the Qt signal / slot functionality. If "doStuff" needs to change some global properties, make them private member variables of the Global class and provide access functions for the app to get at their values after they have changed:

    Qt Code:
    1. bool someConditionalValue = Global::getInstance()->conditionalValue();
    To copy to clipboard, switch view to plain text mode 
    Last edited by d_stranz; 5th September 2012 at 17:13.

  13. #12
    Join Date
    Sep 2011
    Posts
    1,245
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to send data from a static function of other Class to a MainWindow function?

    Quote Originally Posted by hakermania View Post
    Nope, seriously, this function is being called from almost every class. Would you recommend making class objects and calling the function through there?
    Then you have very poor encapsulation - since every class wants to use this code. You could call this bad design. I know I do. It Also breaks the one class, one responsibility paradigm, since almost every class is responsible for gui/mainwindow updates.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

Similar Threads

  1. Replies: 2
    Last Post: 19th July 2018, 06:38
  2. Replies: 4
    Last Post: 23rd August 2011, 21:53
  3. Replies: 1
    Last Post: 13th April 2011, 11:24
  4. Can you send a signal from a static callback function?
    By Eos Pengwern in forum Qt Programming
    Replies: 2
    Last Post: 12th August 2010, 13:47
  5. Replies: 3
    Last Post: 14th October 2008, 21:04

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.