PDA

View Full Version : Signal one gui from another gui



RubyTigerSG
1st April 2011, 12:32
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

Zlatomir
1st April 2011, 12:48
You can use signals and slots (http://doc.qt.nokia.com/4.7/signalsandslots.html), there is no problem if the application have more than one window.

RubyTigerSG
2nd April 2011, 04:38
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?

Zlatomir
2nd April 2011, 11:21
QObject::connect(...) (http://doc.qt.nokia.com/latest/qobject.html#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(...)

RubyTigerSG
2nd April 2011, 12:02
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.

Zlatomir
2nd April 2011, 12:15
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:


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

MyClick aClick;
OtherClass other;

QObject::connect(aClick, SIGNAL(myButtonWasClicked()), other, SLOT(WHAT_EVER_SLOT_YOU_WANT_TO_CALL()) );

aClick.show();
other.show();

return a.exec();
}

RubyTigerSG
2nd April 2011, 13:40
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

QObject::connect(this,SIGNAL(mySignal()),userManag er, SLOT(mySlot()));

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.