PDA

View Full Version : Modifying ui elements of one dialog from another dialog.



harvey_slash
5th October 2013, 14:36
I am a total newbie. I have this doubt

suppose one has to change text
We use:

ui->lineEdit->setText("hello world") ;

for inside .cpp of any window, we use the above.

But if I want to setText of one dialog from another window,how do I do that?
I have a dialog called 'dialog',which has a lineEdit called "qwerty".
How to set text of qwerty from another window,say MainWindow?

toufic.dbouk
5th October 2013, 15:19
Hello,
Use Signals and Slots. Same logic as your previous post.
for example, emit a signal (from mainwindow) holding the QString that you wanna set from main window to the QDialog,
catch the emitted signal by connecting it to a slot that you should create in the QDailog.

QDialog *mDialog = new QDialog(this);
connect(this,SIGNAL(sendText(QString)),mDialog,SLO T(getText(QString)));
the Slot getText should be something like:

getText(QString txt)
{
ui->qwerty->setText("txt");
}

harvey_slash
5th October 2013, 16:16
I am totally new to signal slot, could you tell me what exactly is happening in your code ?
What is sendText?

toufic.dbouk
5th October 2013, 16:35
Sure ill tell you whats happening in English
lets take your example, you have a main window and dialog , and you want to change the text of a line edit in the dialog upon an event that happens in the main window.
lets say you want to press a button in the main window , and upon pressing it you want to set a specific text in that line edit of the dialog.
so to do that in signals and slots , when pressing the button emit a signal holding a QString. now this signal is emitted , so you have to catch it somewhere. Here comes the role of the connect , you connect that signal to a slot in the dialog. So that when the signal is emitted that dialog can catch it and perform an action according to it by the connected slot.

connect(this,SIGNAL(sendText(QString)),mDialog,SLO T(getText(QString)));
this: represents the parent of the signal , in our case it is the main window
sendText: is the signal that you declare in the header file under the SIGNALS: ( you emit the signal in your cpp file when needed by : emit sendText(QString txt)
mDialog: represents the parent of the slot , in our case it is the dialog
getText: is the slot in dialog that you create to handle the signal
when the signal sendText holding the QString is emitted , the dialog will catch it and execute the connected slot , hence you can set that QString in the dialog.
so to sum it up, when the button in the main window is pressed it will get a string and and emit a signal , the signal is caught in the dialog and you perform actions according to the connected slot.
check this link for better and more explanation Signals and Slots (http://qt-project.org/doc/qt-5.0/qtcore/signalsandslots.html)
Hope this explains it.

harvey_slash
5th October 2013, 16:37
I got most of the mechanism, but I don't know how to really use it.
what definitions , etc do I have to do, and what is sendText?

anda_skoa
5th October 2013, 17:02
You don't need sendText(), you can just connect to the QLIneEdit's textChanged(QString) signal.



connect(ui->lineEdit, SIGNAL(textChanged(QString)), dialog, SLOT(setText(QString)));


So the only thing you really need is a slot in your dialog class


class MyDialog : public QDIalog
{
Q_OBJECT

//...

public slots:
void setText(const QString &text); // this consumes the text in whatever way you deem right


Cheers,
_

toufic.dbouk
5th October 2013, 17:08
You don't need sendText(), you can just connect to the QLIneEdit's textChanged(QString) signal.
yea we went over this , was just making it clearer by emitting his own signal.
in the header file just add :

signals:
void sendText(QString);

and in the .cpp file in the method where you need to emit the signal add:

emit sendText(/*your string */);

harvey_slash
5th October 2013, 19:22
I am having difficulty understand this .
could you give me some reference ?
and, are there any other methods to get my job done ?

Added after 9 minutes:


You don't need sendText(), you can just connect to the QLIneEdit's textChanged(QString) signal.



connect(ui->lineEdit, SIGNAL(textChanged(QString)), dialog, SLOT(setText(QString)));


So the only thing you really need is a slot in your dialog class


class MyDialog : public QDIalog
{
Q_OBJECT

//...

public slots:
void setText(const QString &text); // this consumes the text in whatever way you deem right


Cheers,
_

could you please explain why you did everything ?
I want to learn signal slot mechanisms.
thanks.

anda_skoa
5th October 2013, 19:40
The slot is a "receiver" for data sent/emitted by a signal.
To create a slot you add a "slots" section to the class declaration of the receiver class, in this case I assume a QDialog subclass.
A slots section is either public, protected or private, just like for normal C++ methods of the class.

The connect() statement then hooks the signal and the slot up to each other, so that whenever the signal sends its value, it will be received by the slot.
I.e. the slot will be called with the text as its argument.

What you do in that method (the slot is just a normal C++ method in that regard) is up to you. You could e.g. set it on a label that is a child of the dialog


void MyDialog::setText(const QString &text)
{
ui->label->setText(text);
}


SIgnal/Slots are the most convenient way to letting other parts of the application know that something has happend.
But of course this is C++, so it is also possible to use other forms of notification mechanisms, e.g. the observer pattern

Cheers,
_