Results 1 to 10 of 10

Thread: Connecting from a dialog to mainWindow

  1. #1
    Join Date
    Sep 2013
    Posts
    44
    Thanks
    9
    Qt products
    Qt5
    Platforms
    MacOS X Windows Android

    Default Connecting from a dialog to mainWindow

    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 ?

  2. #2
    Join Date
    Dec 2012
    Posts
    197
    Thanks
    25
    Thanked 41 Times in 33 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Connecting from a dialog to mainWindow

    When you create you dialogs instance in main window
    Connect whatever signal you need from dialog to the needed slot in main window
    Qt Code:
    1. Dialog *mDialog = new Dialog(this);
    2. comnect(mDialog,SIGNAL(finished(/*implement your own signal if needed*/)),this,SLOT(execute(/*implement your own slot as needed*/ )));
    3. mDialog->show();
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Sep 2013
    Posts
    44
    Thanks
    9
    Qt products
    Qt5
    Platforms
    MacOS X Windows Android

    Default Re: Connecting from a dialog to mainWindow

    I cant get it done.
    I have
    Qt Code:
    1. dialog=new Dialog();
    2. dialog->show();
    To copy to clipboard, switch view to plain text mode 
    how to connect the lineEdit qwe in dialog to lineEdit 'lineEdit' in mainWindow ?

  4. #4
    Join Date
    Dec 2012
    Posts
    197
    Thanks
    25
    Thanked 41 Times in 33 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Connecting from a dialog to mainWindow

    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"
    Qt Code:
    1. ui->lineEdit->setText("hello");
    To copy to clipboard, switch view to plain text mode 
    the on click slot of the button as follows :
    Qt Code:
    1. void MainWindow::on_pushButton_clicked()
    2. {
    3. mDialog = new Dialog(this); // mDialog is declared in your header
    4. connect(ui->lineEdit,SIGNAL(textChanged(QString)),mDialog,SLOT(setText(QString)));
    5. mDialog->setModal(true); // try it without this line too
    6. mDialog->show();
    7. ui->lineEdit->setText("test"+QString::number(i));
    8. i++; // int i is declared in the header file
    9. }
    To copy to clipboard, switch view to plain text mode 

    in your dialog you have a line edit and setText slot
    Qt Code:
    1. void Dialog::setText(QString str) // declared in you .hpp as a public slot
    2. {
    3. ui->lineEdit->setText(str);
    4. }
    To copy to clipboard, switch view to plain text mode 

    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
    Qt Code:
    1. void Dialog::on_pushButton_clicked()
    2. {
    3. ui->lineEdit->setText("later"+QString::number(i));
    4. i++; // declare it in your header
    5. emit sendText(ui->lineEdit->text()); // declare this signal in your header under signals
    6. }
    To copy to clipboard, switch view to plain text mode 

    and in your MainWindow add a slot
    Qt Code:
    1. void MainWindow::setMainWindowText(QString str)
    2. {
    3. ui->lineEdit->setText(str);
    4. }
    To copy to clipboard, switch view to plain text mode 

    and of course add this under your old connect statement :
    Qt Code:
    1. connect(mDialog,SIGNAL(sendText(QString)),this,SLOT(setMainWindowText(QString)));
    To copy to clipboard, switch view to plain text mode 
    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.

  5. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Connecting from a dialog to mainWindow

    You can forward the signal from the Dialog's line edit
    Qt Code:
    1. class Dialog : public QDialog
    2. {
    3. Q_OBJECT
    4. public:
    5. explicit Dialog(QWidget *parent = 0);
    6.  
    7. signals:
    8. void textChanged(const QString &text);
    9. };
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. Dialog::Dialog(QWidget *parent)
    2. : QDialog(parent)
    3. , ui(....)
    4. {
    5. connect(ui->lineEdit, SIGNAL(textChanged(QString)), this, SIGNAL(textChanged(QString)));
    6. }
    To copy to clipboard, switch view to plain text mode 
    And then connect that signal in your main window to whatever slot you want.

    Cheers,
    _

  6. #6
    Join Date
    Dec 2012
    Posts
    197
    Thanks
    25
    Thanked 41 Times in 33 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Connecting from a dialog to mainWindow

    Is there any advantage behind forwarding the signal to another signal
    Qt Code:
    1. connect(ui->lineEdit, SIGNAL(textChanged(QString)), this, SIGNAL(textChanged(QString)));
    To copy to clipboard, switch view to plain text mode 
    over just emitting a new signal such as ?
    Qt Code:
    1. emit sendText(ui->lineEdit->text());
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Nov 2006
    Location
    indonesia
    Posts
    55
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Connecting from a dialog to mainWindow

    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

  8. #8
    Join Date
    Dec 2012
    Posts
    197
    Thanks
    25
    Thanked 41 Times in 33 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Connecting from a dialog to mainWindow

    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.

  9. #9
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Connecting from a dialog to mainWindow

    Quote Originally Posted by toufic.dbouk View Post
    Is there any advantage behind forwarding the signal to another signal
    Qt Code:
    1. connect(ui->lineEdit, SIGNAL(textChanged(QString)), this, SIGNAL(textChanged(QString)));
    To copy to clipboard, switch view to plain text mode 
    over just emitting a new signal such as ?
    Qt Code:
    1. emit sendText(ui->lineEdit->text());
    To copy to clipboard, switch view to plain text mode 
    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,
    _

  10. The following user says thank you to anda_skoa for this useful post:

    toufic.dbouk (11th October 2013)

  11. #10
    Join Date
    Dec 2012
    Posts
    197
    Thanks
    25
    Thanked 41 Times in 33 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Connecting from a dialog to mainWindow

    Alright got it anda_skoa.
    Thanks for the explanation.

Similar Threads

  1. Using Variables From Dialog In MainWindow
    By steadi in forum Newbie
    Replies: 2
    Last Post: 8th October 2012, 00:18
  2. How to call a dialog from a mainwindow
    By luiz4um in forum Qt Programming
    Replies: 26
    Last Post: 29th June 2010, 11:41
  3. Open Dialog from MainWindow.
    By halvors in forum Qt Programming
    Replies: 8
    Last Post: 1st April 2010, 02:09
  4. Communication between MainWindow and a dialog
    By Backslash in forum Newbie
    Replies: 9
    Last Post: 3rd August 2007, 05:27
  5. Replies: 2
    Last Post: 23rd May 2007, 04:51

Tags for this Thread

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.