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 ?
Printable View
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 ?
When you create you dialogs instance in main window
Connect whatever signal you need from dialog to the needed slot in main window
Code:
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();
I cant get it done.
I have
how to connect the lineEdit qwe in dialog to lineEdit 'lineEdit' in mainWindow ?Code:
dialog=new Dialog(); dialog->show();
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"
the on click slot of the button as follows :Code:
ui->lineEdit->setText("hello");
Code:
void MainWindow::on_pushButton_clicked() { mDialog = new Dialog(this); // mDialog is declared in your header mDialog->setModal(true); // try it without this line too mDialog->show(); i++; // int i is declared in the header file }
in your dialog you have a line edit and setText slot
Code:
{ 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
Code:
void Dialog::on_pushButton_clicked() { 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
and of course add this under your old connect statement :
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.
You can forward the signal from the Dialog's line edit
Code:
{ Q_OBJECT public: signals: };
And then connect that signal in your main window to whatever slot you want.Code:
, ui(....) { }
Cheers,
_
Hi,
Your problem is like this thread http://www.qtcentre.org/threads/5647...ng-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
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.
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,
_
Alright got it anda_skoa.
Thanks for the explanation.