PDA

View Full Version : Connecting from a dialog to mainWindow



harvey_slash
7th October 2013, 14:06
I want to use the connect function to connect from a dialog class called Dialog to my mainwindow.
I am new to signal/slot.
how do I go about it ?

toufic.dbouk
7th October 2013, 14:26
When you create you dialogs instance in main window
Connect whatever signal you need from dialog to the needed slot in main window

Dialog *mDialog = new Dialog(this);
comnect(mDialog,SIGNAL(finished(/*implement your own signal if needed*/)),this,SLOT(execute(/*implement your own slot as needed*/ )));
mDialog->show();

harvey_slash
7th October 2013, 17:38
I cant get it done.
I have


dialog=new Dialog();
dialog->show();
how to connect the lineEdit qwe in dialog to lineEdit 'lineEdit' in mainWindow ?

toufic.dbouk
7th October 2013, 18:53
You don't connect two line edits together there isn't any sense behind that.
You connect signals and slots from one window to another for example to change some GUI or execute functions etc...
You emit or catch a signal and perform some execution in a slot according to that caught signal.
example: dialog has a line edit , main window has a button and a line edit.
In main window constructor you set the text of the line edit to "hello"

ui->lineEdit->setText("hello");
the on click slot of the button as follows :

void MainWindow::on_pushButton_clicked()
{
mDialog = new Dialog(this); // mDialog is declared in your header
connect(ui->lineEdit,SIGNAL(textChanged(QString)),mDialog,SLOT (setText(QString)));
mDialog->setModal(true); // try it without this line too
mDialog->show();
ui->lineEdit->setText("test"+QString::number(i));
i++; // int i is declared in the header file
}

in your dialog you have a line edit and setText slot

void Dialog::setText(QString str) // declared in you .hpp as a public slot
{
ui->lineEdit->setText(str);
}

so here anytime you press the button in main window a dialog will appear having the same text on the mainWindo in the line edit.

Now change it into this,
in your dialog add a button

void Dialog::on_pushButton_clicked()
{
ui->lineEdit->setText("later"+QString::number(i));
i++; // declare it in your header
emit sendText(ui->lineEdit->text()); // declare this signal in your header under signals
}

and in your MainWindow add a slot

void MainWindow::setMainWindowText(QString str)
{
ui->lineEdit->setText(str);
}

and of course add this under your old connect statement :

connect(mDialog,SIGNAL(sendText(QString)),this,SLO T(setMainWindowText(QString)));
now when you press the button of mainwindow you will change the text in the dialog and when you press the button in the dialog you will change the text of the main window.
this should explain and clear it all.
good luck.

anda_skoa
11th October 2013, 10:47
You can forward the signal from the Dialog's line edit


class Dialog : public QDialog
{
Q_OBJECT
public:
explicit Dialog(QWidget *parent = 0);

signals:
void textChanged(const QString &text);
};



Dialog::Dialog(QWidget *parent)
: QDialog(parent)
, ui(....)
{
connect(ui->lineEdit, SIGNAL(textChanged(QString)), this, SIGNAL(textChanged(QString)));
}

And then connect that signal in your main window to whatever slot you want.

Cheers,
_

toufic.dbouk
11th October 2013, 11:17
Is there any advantage behind forwarding the signal to another signal

connect(ui->lineEdit, SIGNAL(textChanged(QString)), this, SIGNAL(textChanged(QString)));
over just emitting a new signal such as ?

emit sendText(ui->lineEdit->text());

myta212
11th October 2013, 11:22
Hi,
Your problem is like this thread http://www.qtcentre.org/threads/56471-Image-processing-error/page2
I have created a sample code from above link (signal and slot using QmainWindow and QDialog).
You can check that and I hope get idea how to solve your problem.

Best regards,

Toto

toufic.dbouk
11th October 2013, 11:28
Hello, if you are addressing me , i dont have a problem.
My question was if there is any advantages of one way over the other.
Good Luck.

anda_skoa
11th October 2013, 18:48
Is there any advantage behind forwarding the signal to another signal

connect(ui->lineEdit, SIGNAL(textChanged(QString)), this, SIGNAL(textChanged(QString)));
over just emitting a new signal such as ?

emit sendText(ui->lineEdit->text());

It depends on what the goal is.

Given the thread starter's question it looks like the goal is a synchronisation between two line edits.
For that goal the forwarding is "nicer" because you don't need a slot just to emit a signal again and the signal/signal connect makes the forwarding very obvious when reading the code.

In the example you gave, i.e. reacting to a button, the slot plus explicit emit is the only possible choice.

Cheers,
_

toufic.dbouk
11th October 2013, 19:18
Alright got it anda_skoa.
Thanks for the explanation.