Results 1 to 3 of 3

Thread: Passing data from a QDialog to a QMainWindow using signal/slots

  1. #1
    Join Date
    Oct 2013
    Posts
    3
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Passing data from a QDialog to a QMainWindow using signal/slots

    I've read in other posts some piece of code to transfer data from a QDialog to QMainWindow, but it didn't work
    Here is what I've done, and I don't know what's wrong of if maybe I shouldn't use signals/slots to do that...
    Any help will be very welcomed

    MainWindow.cpp:
    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::MainWindow)
    4. {
    5. ui->setupUi(this);
    6.  
    7. ui->lineEdit->setAlignment(Qt::AlignRight);
    8.  
    9. Dialog *dialog = new Dialog(this);
    10. QObject::connect(dialog, SIGNAL(validate(QString)), this, SLOT(validate(QString)));
    11.  
    12. }
    13.  
    14. MainWindow::~MainWindow()
    15. {
    16. delete ui;
    17. }
    18.  
    19. void MainWindow::on_Open_clicked()
    20. {
    21. Dialog *dialog = new Dialog;
    22. dialog->setModal(true);
    23. dialog->exec();
    24.  
    25. }
    26.  
    27. void MainWindow::validate(QString data){
    28. ui->lineEdit->setText(data);
    29.  
    30. }
    To copy to clipboard, switch view to plain text mode 

    MainWindow.h:
    Qt Code:
    1. namespace Ui {
    2. class MainWindow;
    3. }
    4.  
    5. class MainWindow : public QMainWindow
    6. {
    7. Q_OBJECT
    8.  
    9. public:
    10. explicit MainWindow(QWidget *parent = 0);
    11. ~MainWindow();
    12.  
    13. private slots:
    14. void on_Open_clicked();
    15. void validate(QString data);
    16.  
    17. private:
    18. Ui::MainWindow *ui;
    19. };
    To copy to clipboard, switch view to plain text mode 

    Dialog.cpp:

    Qt Code:
    1. Dialog::Dialog(QWidget *parent) :
    2. QDialog(parent),
    3. ui(new Ui::Dialog)
    4. {
    5. ui->setupUi(this);
    6.  
    7. ui->lineEdit->setAlignment(Qt::AlignRight);
    8. }
    9.  
    10. Dialog::~Dialog()
    11. {
    12. delete ui;
    13. }
    14.  
    15. void Dialog::on_Ok_clicked(){
    16. emit validate(ui->lineEdit->text());
    17. }
    To copy to clipboard, switch view to plain text mode 

    Dialog.h:

    Qt Code:
    1. class Dialog : public QDialog
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. explicit Dialog(QWidget *parent = 0);
    7. ~Dialog();
    8.  
    9. private slots:
    10. void on_Ok_clicked();
    11.  
    12. signals:
    13. void validate(QString data);
    14.  
    15. private:
    16. Ui::Dialog *ui;
    17. };
    To copy to clipboard, switch view to plain text mode 

    It's as if the signal and slot wasn't well coded, because when I debug it, it didn't go to the slot when the signal is emited...

    Thanks a lot in advance...

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Passing data from a QDialog to a QMainWindow using signal/slots

    You create a new dialog in main window's constructor and never exec it, it is useless.
    Remove it from the constructor and connect to instances created in on_Open_clicked() slot:
    Qt Code:
    1. void MainWindow::on_Open_clicked()
    2. {
    3. Dialog *dialog = new Dialog (this); // remember to set parent !
    4. dialog->setModal(true);
    5. connect(dialog, SIGNAL(validate(QString)), this, SLOT(validate(QString)));
    6. dialog->exec();
    7.  
    8. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Passing data from a QDialog to a QMainWindow using signal/slots

    QDialog::exec() always sets modality to true, so no need to do that explicitly

    Cheers,
    _

Similar Threads

  1. How to pass a member data from QMainWindow to QDialog
    By pollausen85 in forum Qt Programming
    Replies: 3
    Last Post: 11th October 2013, 10:40
  2. Passing a object between QDialog and QMainWindow
    By m_bishop in forum Qt Programming
    Replies: 4
    Last Post: 16th May 2013, 14:53
  3. Replies: 3
    Last Post: 22nd September 2011, 05:47
  4. Passing data via signal/slot
    By gib in forum Qt Programming
    Replies: 4
    Last Post: 1st November 2010, 05:49
  5. QDialog and QMainWindow Data Transfer
    By Ahmad in forum Qt Programming
    Replies: 12
    Last Post: 6th May 2007, 07:02

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.