Results 1 to 7 of 7

Thread: Signal one gui from another gui

  1. #1
    Join Date
    Apr 2011
    Posts
    7
    Qt products
    Qt4
    Platforms
    Windows

    Default Signal one gui from another gui

    Hi guys,

    I'm not even sure how to ask this question. So I'll tell you what Im trying to do and then maybe one of you could tell me what I should be asking.

    I have an application which has a gui where a user can log in. Then in the same application there is a screen which displays all the users which are logged in.

    What I want to do , is that when a new user logs in , somehow the display screen gets notified so that it can update itself.

    Is there anyway to do this?

    Thanks a million.
    RT

  2. #2
    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: Signal one gui from another gui

    You can use signals and slots, there is no problem if the application have more than one window.

  3. #3
    Join Date
    Apr 2011
    Posts
    7
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Signal one gui from another gui

    Thanks Zlatomir,

    That is indeed a helpful link. however I am still confused about something.
    Each of my gui's are executed from different classes.

    so where do I do the connection. Do I connect in the login class on the click of the login button and emit a public signal?
    Or do I have to connect from the class which wants to receive the signal?

  4. #4
    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: Signal one gui from another gui

    QObject::connect(...) has a static version, which can be used to connect two QObjects from outside of QObjects.
    You can make the connection in your main(...) function, or wherever is appropriate in your application's design.

    LE: the code for static version is just QObject:: in front of connect: QObject::connect(...)

  5. #5
    Join Date
    Apr 2011
    Posts
    7
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Signal one gui from another gui

    Hi Zlatomir,
    Thanks again. Let me post some code because I still don't get it. I appologise if this is a little tedious. But I am starting to realise I actually dont have a clude what Im doing here.

    --myclick.h
    #ifndef MYCLICK_H
    #define MYCLICK_H
    #include <QDialog>

    class MyClick: public QDialog
    {Q_OBJECT
    public:
    MyClick();
    private slots:
    void btnClicked();
    };

    #endif // MYCLICK_H

    --myclick.cpp
    #include "myclick.h"
    #include <QPushButton>
    #include <QMessageBox>
    #include "otherclass.h"
    #include <QApplication>

    MyClick::MyClick()
    {
    QPushButton *mybutton = new QPushButton("Click Me!",this);

    connect(mybutton, SIGNAL(clicked()),this, SLOT(btnClicked()));

    }

    void MyClick::btnClicked()
    { QMessageBox message;

    message.information(this,"Clicked", "You clicked me!","OK");

    }

    --otherclass.h
    #ifndef OTHERCLASS_H
    #define OTHERCLASS_H
    #include <QDialog>

    class OtherClass: public QDialog
    {Q_OBJECT
    public:
    OtherClass();
    public slots:
    void btnClicked();

    };

    #endif // OTHERCLASS_H


    --otherclass.cpp
    #include "otherclass.h"
    #include <QLabel>
    #include <QMessageBox>


    OtherClass::OtherClass()

    {
    QLabel *myLabel = new QLabel("This is a label",this);
    }

    void OtherClass::btnClicked()
    { QMessageBox message;

    message.information(this,"Clicked", "You clicked OTHER!","OK");

    }

    --Main.cpp
    #include <QApplication>
    #include "myclick.h"
    #include "otherclass.h"

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);

    MyClick aClick;
    aClick.show();

    OtherClass other;
    other.show();


    return a.exec();
    }


    So essentially I am launching an instance of myclick. which has a button, which currently connects to MyClick::btnClicked() slot. But what I want is for it to connect to the the public slot of OtherClass::btnClicked().
    But the two instances are not aware of each other. So when I specify the target I dont have a pointer to a specific instance of OtherClass.

    Your help is very much appreciated.

  6. #6
    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: Signal one gui from another gui

    Please use the code tags to post code, it will keep the indent and make the code more readable

    You can do something like:
    Make the pointer to the button a member in MyClick class (i'm talking about mybutton)
    create a signal MyClick::myButtonWasClicked(); this will signal to outside world that inside your MyClicked object something changed (a button was clicked)
    in MyClick constructor connect the mybutton clicked() signal with this myButtonWasClicked() (you can connect a signal to another signal), or since i see you already have a slot connected to the button clicked, you can use emit myButtonWasClicked(); inside that slot.
    And then in main:
    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QApplication a(argc, argv);
    4.  
    5. MyClick aClick;
    6. OtherClass other;
    7.  
    8. QObject::connect(aClick, SIGNAL(myButtonWasClicked()), other, SLOT(WHAT_EVER_SLOT_YOU_WANT_TO_CALL()) );
    9.  
    10. aClick.show();
    11. other.show();
    12.  
    13. return a.exec();
    14. }
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Apr 2011
    Posts
    7
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Signal one gui from another gui

    Ah... Ok Now I get it.

    Thank you sooo much.

    Sorry about posting wihtout code tags. Promise to do that next time.


    Added after 1 20 minutes:


    Ok so this is almost working. Just one more question.

    I have no wupdated my program so that OtherClass is instantiated from MyClick.
    And then I do the connect in MyClick like this
    Qt Code:
    1. QObject::connect(this,SIGNAL(mySignal()),userManager, SLOT(mySlot()));
    To copy to clipboard, switch view to plain text mode 

    And this works great when there is only once instance of MyClick. But as soon as I have more than one instance of my click. It just stops working.
    Any idea how I can resolve this?

    Thank you for your patience. You have really helped me a lot so far.
    Last edited by RubyTigerSG; 2nd April 2011 at 12:40.

Similar Threads

  1. Connect signal/signal in Qt Designer
    By jlemaitre in forum Newbie
    Replies: 1
    Last Post: 22nd September 2010, 15:53
  2. Signal connected to slot (or signal)
    By Althor in forum Newbie
    Replies: 2
    Last Post: 6th July 2010, 10:00
  3. signal mapping on pushbutton signal clicked
    By wagmare in forum Qt Programming
    Replies: 2
    Last Post: 17th March 2009, 07:54
  4. Signal-Signal Connections Between Threads
    By PhilippB in forum Qt Programming
    Replies: 2
    Last Post: 15th December 2008, 18:27
  5. Replies: 1
    Last Post: 8th November 2007, 17:11

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.