Results 1 to 5 of 5

Thread: Passing a object between QDialog and QMainWindow

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    May 2013
    Posts
    8
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11

    Default Passing a object between QDialog and QMainWindow

    Can a QPushButton inside a QDialog create an object and pass it back to a QWidget inside of QMainWindow?

    A dialog pops up with data that needs to be used to create a device and then this device gets monitored by the mainwindow.

    A rough online example or a chapter to read in a book or a class info would be a great kick starter.

  2. #2
    Join Date
    Dec 2012
    Posts
    90
    Thanks
    5
    Thanked 20 Times in 18 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Passing a object between QDialog and QMainWindow

    I can think of two possible ways, the first way you show your dialog, then after it closed with Ok status you can call dedicated dialog method to get your configured device.
    Qt Code:
    1. class DialogV2 {
    2. public:
    3. YourObject *getConfiguredObject () {
    4. return new YourObject;
    5. };
    6. };
    To copy to clipboard, switch view to plain text mode 
    A slightly alternative approach would be to return configuration structure with all necessary parameters and then create the device in your main window class (like QFileOpenDialog returning file name and then you create a QFile out of it).

    Another way is to using signals and slots
    Qt Code:
    1. class MainWindow {
    2. public slots:
    3. void objectCreated(YourObject *o);
    4. };
    5.  
    6. class Dialog {
    7. private slots:
    8. void onCreateClicked () {
    9. YourObject *obj = new YourObject;
    10. emit objectCreated (obj);
    11. };
    12. signals:
    13. void objectCreated (YourObject *o);
    14. };
    To copy to clipboard, switch view to plain text mode 
    But it has two downsides that iI can think of:
    1. You need to trace the lifetime of your object, and don't get it lost when signal is unconnected.
    2. Object creation and corresponding slot will be called while dialog is still active, which can be undesirable behaviour.

    Generally I suggest first solution, second solution is better if you generate lightweight value-type objects and don't really care about them.

  3. #3
    Join Date
    May 2013
    Posts
    8
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Passing a object between QDialog and QMainWindow

    Quote Originally Posted by lanz View Post
    I can think of two possible ways, the first way you show your dialog, then after it closed with Ok status you can call dedicated dialog method to get your configured device.
    Qt Code:
    1. class DialogV2 {
    2. public:
    3. YourObject *getConfiguredObject () {
    4. return new YourObject;
    5. };
    6. };
    To copy to clipboard, switch view to plain text mode 
    I am not sure how this would work as that the data would be destroyed when the QDialog closes. How would the QDialog save the data to be returned when my QWidget in QMainWindow calls
    myDevice = myDialog->getConfiguredObject;

    Maybe I just need some more caffeine to understand what you are trying to tell me.

    Quote Originally Posted by lanz View Post
    A slightly alternative approach would be to return configuration structure with all necessary parameters and then create the device in your main window class (like QFileOpenDialog returning file name and then you create a QFile out of it).
    I understand this approach.


    Quote Originally Posted by lanz View Post
    Another way is to using signals and slots
    Qt Code:
    1. class MainWindow {
    2. public slots:
    3. void objectCreated(YourObject *o);
    4. };
    5.  
    6. class Dialog {
    7. private slots:
    8. void onCreateClicked () {
    9. YourObject *obj = new YourObject;
    10. emit objectCreated (obj);
    11. };
    12. signals:
    13. void objectCreated (YourObject *o);
    14. };
    To copy to clipboard, switch view to plain text mode 
    But it has two downsides that iI can think of:
    1. You need to trace the lifetime of your object, and don't get it lost when signal is unconnected.
    2. Object creation and corresponding slot will be called while dialog is still active, which can be undesirable behaviour.

    Generally I suggest first solution, second solution is better if you generate lightweight value-type objects and don't really care about them.
    I started reading about slots and signals last night and this might be what I was looking for. I'll mock something up to see how the downsides affect behavior. I would still like to understand what you are telling me on the first approach so that I can mock something up and put it in my tool box, so to speak.

  4. #4
    Join Date
    Dec 2012
    Posts
    90
    Thanks
    5
    Thanked 20 Times in 18 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Passing a object between QDialog and QMainWindow

    I am not sure how this would work as that the data would be destroyed when the QDialog closes. How would the QDialog save the data to be returned when my QWidget in QMainWindow calls
    myDevice = myDialog->getConfiguredObject;
    Oh, I need more caffeine too, didn't get your point at first
    Qt Code:
    1. class MainWindow {
    2. public:
    3. void onCreateDeviceClick () {
    4. DialogV2 *dlg = new DialogV2;
    5. if (QDialog::Accepted == dlg->exec ()) {
    6. //user accepts
    7. // dialog here is closed, but not destroyed
    8. // all it's data is in reach
    9. // so we can do anything with the data
    10. // think again usage pattern of the QFileOpenDialog
    11. this->myDevice = dlg->getConfiguredObject ();
    12. } else {
    13. // user cancels out
    14. };
    15. delete dlg;
    16.  
    17. };
    18. };
    To copy to clipboard, switch view to plain text mode 
    Hope this clarifies it a bit.

  5. #5
    Join Date
    May 2013
    Posts
    8
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Passing a object between QDialog and QMainWindow

    Yes, it does. I can do good things with that info.

Similar Threads

  1. Replies: 1
    Last Post: 26th March 2013, 02:04
  2. Replies: 0
    Last Post: 22nd September 2011, 10:31
  3. Replies: 3
    Last Post: 22nd September 2011, 05:47
  4. Replies: 4
    Last Post: 15th July 2011, 18:31
  5. Open a QMainWindow Object in QDialog Object
    By chuengchuenghq in forum Qt Programming
    Replies: 1
    Last Post: 13th June 2008, 06:33

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
  •  
Qt is a trademark of The Qt Company.