Results 1 to 4 of 4

Thread: How to pass a member data from QMainWindow to QDialog

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

    Default How to pass a member data from QMainWindow to QDialog

    Hi everyone!

    I'm a new member of forum and I'm a beginner with Qt. I searched in forum if somebody else had the same problem, but I found the solution only for the reverse problem.

    My problem is the following:

    I have a QMainWindow (Qsem2D) with some member data and with a button which when is clicked open a QDialog (SettingSolver) window that is a child of QMainWindow.

    Qsem2D:

    Qsem2D.h
    Qt Code:
    1. #include <QMainWindow>
    2.  
    3. class Qsem2d : public QMainWindow
    4. {
    5. .....
    6. private:
    7. bool *m_isForward;
    8.  
    9. signals:
    10. void signalIsForward(bool i_isForward);
    11.  
    12. private slots:
    13. void on_pushButton_clicked();
    14.  
    15. void sendSignalToSettingSolver();
    16.  
    17. .....
    18. }
    To copy to clipboard, switch view to plain text mode 
    Qsem2D.cpp
    Qt Code:
    1. #include Qsem2D.h
    2. #include SolverSetting.h
    3. ....
    4.  
    5. void Qsem2D::on_pushButton_clicked()
    6. {
    7. sendSignalToSettingSolver();
    8. SolverSetting *newWindow = new SolverSetting;
    9. newWindow->show;
    10. }
    11.  
    12. void Qsem2D::sendSignalToSettingSolver()
    13. {
    14. emit signalIsForward(*m_isForward);
    15. }
    To copy to clipboard, switch view to plain text mode 
    SettingSolver:

    SettingSolver.h
    Qt Code:
    1. #include "Qsem2D.h"
    2. #include <QDialog>
    3.  
    4. class SettingSolver : public QDialog
    5. {
    6. public:
    7.  
    8. SettingSolver(Qsem2D *parent = 0);
    9. ~SettingSolver();
    10. .....
    11.  
    12. private:
    13. bool *m_isForward;
    14.  
    15. private slots:
    16.  
    17. void receiveSignalFromMainWindow(bool i_isForward);
    18. }
    To copy to clipboard, switch view to plain text mode 
    SettingSolver.cpp
    Qt Code:
    1. #include "SettingSolver.h"
    2. #include <QDialog>
    3.  
    4. SettingSolver::SettingSolver(Qsem2D *parent):
    5. QDialog(parent),
    6. ui(new Ui::SettingSolver),
    7. m_isForward(new bool)
    8. {
    9. .....
    10. connect(parent,SIGNAL(signalIsForward),this,SLOT(receiveSignalFromMainWindow)
    11. if(*m_isForward)
    12. {
    13. ....
    14. }
    15. .....
    16. }
    17.  
    18. void SolverSetting::receiveSignalFromMainWindow(bool i_isForward)
    19. {
    20. *m_isForward = i_isForward;
    21. }
    To copy to clipboard, switch view to plain text mode 

    The code could contain some errors because I don't have the original code handy but what I want understand is the concept.

    My need is to pass the member data Qsem2D::m_isForward from MainWindow to member data SolverSetting::m_isForward (QDialog). For how the code is made now when I call the constructor for SolverSetting the member data SolverSetting::m_isForward is a pointer that points to memory allocation with no sense information instead I wish that it contains the value of Qsem2D::m_isForward.

    What's wrong?

    Thanks.

    Regards.

  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: How to pass a member data from QMainWindow to QDialog

    Pass it through the constructor when you create your dialog's instance, that's one way. Just rewrite your dialog's constructor to take whatever parameter you want.
    The other way could be by making a getter in your dialog and call it after you create your dialog and before you show it for example depending on your need.
    Good Luck.

  3. The following user says thank you to toufic.dbouk for this useful post:

    pollausen85 (10th October 2013)

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

    Default Re: How to pass a member data from QMainWindow to QDialog

    Thanks toufic.dbouk, your solution to pass it through the constructor solved my problem.

    Bye!

  5. #4
    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: How to pass a member data from QMainWindow to QDialog

    The problem in your original code is that you do not pass a parent, thus the connect fails, and that you emit the signal before there is any receiver.

    Cheers,
    _

Similar Threads

  1. Replies: 1
    Last Post: 26th March 2013, 02:04
  2. Replies: 0
    Last Post: 22nd September 2011, 10:31
  3. Replies: 1
    Last Post: 29th May 2011, 08:00
  4. Replies: 5
    Last Post: 1st March 2010, 15:55
  5. QDialog and QMainWindow Data Transfer
    By Ahmad in forum Qt Programming
    Replies: 12
    Last Post: 6th May 2007, 07:02

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.