Results 1 to 7 of 7

Thread: Qt5/C++ - Signals and slots, QMainwindow to QDialog

  1. #1
    Join Date
    Oct 2006
    Posts
    102
    Thanks
    13
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Qt5/C++ - Signals and slots, QMainwindow to QDialog

    Qt5 5.70
    Qt Creator 4.1.0

    Hello,

    I've got a modal QDialog on top of a QMainwindow.
    I start a mainwindow QProcess from the QDialog, this seems to work.
    I'd like to get the value from the QMainwindow/QProcess to show on on the QDialog QProgressBar.
    So far, no luck.
    I'm obviously missing sommething and would appreciate some help.
    Thanks in advance.

    Regards
    Qt Code:
    1. mainwinndow .cpp
    2. void MainWindow::readyReadStandardOutput4() //slot
    3. {
    4. QString temp = process->readAllStandardOutput();
    5. outputList = temp.split("per");
    6.  
    7. qDebug() << outputList.at(0).toInt();
    8. emit progressUpdate(outputList.at(0).toInt());
    9. }
    10.  
    11. mainwinndow .hpp
    12. signals:
    13. void progressUpdate(int);
    14.  
    15. QDebug output:
    16. 1
    17. 2
    18. ...
    19. ...
    20. 99
    21. 100
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. noobsForm .cpp
    2. QObject::connect(&mainwindow, SIGNAL(progressUpdate(int)),
    3. this, SLOT(updateProgress(int)));
    4.  
    5. void noobsForm::updateProgress(int value) //slot
    6. {
    7. ui->progressBar->setValue(value);
    8. qDebug() << "value " << value;
    9. }
    10.  
    11. noobsForm .hpp
    12. public slots:
    13. void updateProgress(int);
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,068
    Thanks
    35
    Thanked 1,462 Times in 1,416 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qt5/C++ - Signals and slots, QMainwindow to QDialog

    That looks ok, though one would usually do the connect from outside the dialog instead of passing a reference to the mainwindow.

    Since you have log output, which of those to you get?

    Cheers,
    _

  3. #3
    Join Date
    Oct 2006
    Posts
    102
    Thanks
    13
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qt5/C++ - Signals and slots, QMainwindow to QDialog

    Hello anda_skoa,

    Quote Originally Posted by anda_skoa View Post
    That looks ok, though one would usually do the connect from outside the dialog instead of passing a reference to the mainwindow.

    Since you have log output, which of those to you get?
    Thanks for your reply.
    What log output and where?
    I don't understamd the "from outside" bit.
    Any way the progress bar on the dialog does not update.

    Regards

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,068
    Thanks
    35
    Thanked 1,462 Times in 1,416 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qt5/C++ - Signals and slots, QMainwindow to QDialog

    Quote Originally Posted by jimbo View Post
    What log output and where?
    Your qDebug() statements.

    Quote Originally Posted by jimbo View Post
    I don't understamd the "from outside" bit.
    I assume you are creating the dialog somewhere in MainWindow and then you call exec() on that dialog object.
    "outside" here means outside of the dialog code, i.e. in MainWindow, e.g. before the call to exec().

    But passing the main window to the dialog for the dialog to set the connection up is OK too, just not as common.

    Quote Originally Posted by jimbo View Post
    Any way the progress bar on the dialog does not update.
    Hence the attempt to narrow it down by checking which of your outputs you get.

    Cheers,
    _

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

    jimbo (1st October 2016)

  6. #5
    Join Date
    Oct 2006
    Posts
    102
    Thanks
    13
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qt5/C++ - Signals and slots, QMainwindow to QDialog

    Hello anda_skoa,

    But passing the main window to the dialog for the dialog to set the connection up is OK too, just not as common.
    Qt Code:
    1. myNoobs = new noobsForm(driveDataList, file, ui->cboxDevice->currentText());
    2. QObject::connect(this, SIGNAL(progressUpdate(int)), myNoobs, SLOT(updateProgress(int)));
    3. myNoobs->exec();
    To copy to clipboard, switch view to plain text mode 
    Thats the way, it works fine now, many thanks.

    Regards.

  7. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    3,765
    Thanks
    216
    Thanked 582 Times in 572 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Qt5/C++ - Signals and slots, QMainwindow to QDialog

    That's the way, it works fine now, many thanks.
    If this dialog is being created and posted in a slot (say, in response to a button click or something), then the code you have written looks like it will result in a memory leak when the slot exits. If the dialog is meant to be transient (i.e. posted only for the duration of the slot's execution), then the more conventional way is to create the dialog on the stack instead of on the heap through new():

    Qt Code:
    1. void MainWindow::someSlot()
    2. {
    3. noobsForm myNoobs(driveDataList, file, ui->cboxDevice->currentText());
    4. QObject::connect( this, SIGNAL(progressUpdate(int)), &myNoobs, SLOT(updateProgress(int)));
    5. myNoobs.exec();
    6. }
    To copy to clipboard, switch view to plain text mode 

    In this way, the lifetime of the dialog is the same as the lifetime of the slot; when the slot exits, the "myNoobs" instance on the stack is automatically deleted.

    Alternatively, you could use your heap-based method, but add a call to
    Qt Code:
    1. myNoobs->setAttribute( Qt::WA_DeleteOnClose, true );
    To copy to clipboard, switch view to plain text mode 
    before calling exec(), which will cause Qt to delete the dialog instance when the dialog is closed.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  8. The following user says thank you to d_stranz for this useful post:

    jimbo (2nd October 2016)

  9. #7
    Join Date
    Oct 2006
    Posts
    102
    Thanks
    13
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qt5/C++ - Signals and slots, QMainwindow to QDialog

    Hello d_stranz,

    Thanks for a very good explanation of the methods.
    Both work fine.
    I've gone down the conventional route.

    Regards

Similar Threads

  1. Replies: 2
    Last Post: 30th November 2013, 00:19
  2. Replies: 2
    Last Post: 18th April 2013, 12:15
  3. Replies: 1
    Last Post: 26th March 2013, 02:04
  4. Replies: 0
    Last Post: 22nd September 2011, 10:31
  5. How to Call a QDialog in QMainWindow
    By mcht_z in forum Newbie
    Replies: 2
    Last Post: 13th April 2011, 06:43

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.