Results 1 to 8 of 8

Thread: QDialog signal and slots

  1. #1
    Join Date
    Jan 2017
    Posts
    58
    Thanks
    2
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default QDialog signal and slots

    Hi, I'm new here, and I'm trying to understood how works signal and slots.
    I have:
    mainwindow.h
    Qt Code:
    1. ...
    2. signals:
    3. void setMismatchText(const QString &text);
    4. ...
    To copy to clipboard, switch view to plain text mode 

    mainwindows.cpp
    Qt Code:
    1. ...
    2. Dialog* dialog = new Dialog;
    3.  
    4. connect(this, SIGNAL(setMismatchText(QString)),
    5. dialog, SLOT(getMismatchText(QString)));
    6. ...
    7. this->setEnabled(false);
    8. Dialog *dialog = new Dialog();
    9. dialog->setModal(true);
    10. emit setMismatchText(error);
    11. if(dialog->exec() == QDialog::Accepted)
    12. msgBox.setText("Accepted!");
    13. else
    14. msgBox.setText("Denied!");
    15. msgBox.exec();
    16. ...
    To copy to clipboard, switch view to plain text mode 

    dialog.h
    Qt Code:
    1. ...
    2. public slots:
    3. void getMismatchText(const QString &text);
    4. ...
    To copy to clipboard, switch view to plain text mode 

    dialog.cpp
    Qt Code:
    1. ...
    2. void Dialog::getMismatchText(const QString &text)
    3. {
    4. ui->plainTextEdit->appendPlainText(text);
    5. }
    6. ...
    To copy to clipboard, switch view to plain text mode 

    In Dialog ui I have QPlayTextEdit Object (objectName - plainTextEdit).
    When I emit in mainwindow.cpp, the text in Dialog won't change (or append).

    Can you help me please what's wrong? Thanks!


    edit:
    OK, maybe I fix it.
    dialog.h:
    Qt Code:
    1. public:
    2. void setText(const QString &text);
    To copy to clipboard, switch view to plain text mode 

    mainwindow.cpp
    Qt Code:
    1. Dialog dialog;
    2. dialog.setText(error);
    3. dialog.exec();
    To copy to clipboard, switch view to plain text mode 

    But I'm not sure, if is it correct wayand still I want to know how works signals a slots.

    Sorry for wrong thread, please admin, is possible to change it to "Newbie"?
    Last edited by ado130; 20th January 2017 at 21:33.

  2. #2
    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: QDialog signal and slots

    Quote Originally Posted by ado130 View Post
    mainwindows.cpp
    Qt Code:
    1. ...
    2. Dialog* dialog = new Dialog;
    3.  
    4. connect(this, SIGNAL(setMismatchText(QString)),
    5. dialog, SLOT(getMismatchText(QString)));
    6. ...
    7. this->setEnabled(false);
    8. Dialog *dialog = new Dialog();
    9. dialog->setModal(true);
    10. emit setMismatchText(error);
    11. if(dialog->exec() == QDialog::Accepted)
    12. msgBox.setText("Accepted!");
    13. else
    14. msgBox.setText("Denied!");
    15. msgBox.exec();
    16. ...
    To copy to clipboard, switch view to plain text mode 
    You create a dialog and connect to its slot.
    Then you create another dialog and exec() it.

    The first dialog receives the signal but is not shown.

    Cheers,
    _

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

    ado130 (21st January 2017)

  4. #3
    Join Date
    Jan 2017
    Posts
    58
    Thanks
    2
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QDialog signal and slots

    Thanks a lot, I have not realize it.
    Can I keep this thread alive - unlocked (for new questions), or is better make a new thread?

  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: QDialog signal and slots

    Depends on the question.

    if it is related just add it here, if it is not, create a new thread.

    Cheers,
    _

  6. #5
    Join Date
    Jan 2017
    Posts
    58
    Thanks
    2
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QDialog signal and slots

    Hi,
    this works fine, when I emit signal from mainwindow to dialog, but now I need emit signal from Dialog to mainwindow. The code is almost same, I make this connect
    Qt Code:
    1. connect(dialog, SIGNAL(setAccessLevel(quint16)),
    2. this, SLOT(getAccessLevel(quint16)));
    To copy to clipboard, switch view to plain text mode 
    .. but when I call in dialog.cpp emit setAccessLevel(xyz), nothing happend.

    I am not sure, if I can make connect in mainwindow.cpp (see above) and then emit signal in dialog.cpp.

    Thanks!

    edit: Maybe I solved it. This code is in dialog.cpp, is it correct way?
    Qt Code:
    1. MainWindow mainWindow;
    2. connect(this, SIGNAL(setAccessLevel(quint8)), &mainWindow, SLOT(getAccessLevel(quint8)));
    3. emit setAccessLevel(xyz);
    To copy to clipboard, switch view to plain text mode 

    edit2: It's not working correctly. Correct me, I thinks, it will works if sending form opens received form. But I need open form from mainwindow and than from there send/emit signal to mainwindows (is still opened).
    I want to do login form. After start the program opens login form and after I successfully login emits a signal with quint8 (access level) and the program will continue.
    Last edited by ado130; 29th January 2017 at 17:25.

  7. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QDialog signal and slots

    MainWindow mainWindow;
    This is almost exactly the same as the problem pointed out by anda_skoa in his first answer: you are creating a new, second instance of MainWindow in this line, which has absolutely no relationship to the MainWindow instance you are using to create and display the dialog.

    You need to either:

    1 - Make the connection between the dialog's signal and the MainWindow's slot -in MainWindow- at the place where you create and display the dialog.

    Qt Code:
    1. // MainWindow.cpp
    2. void MainWindow::showDialog()
    3. {
    4. // Assumes MyDialog has a signal "setAccessLevel" and MainWindow has a slot "getAccessLevel"
    5. MyDialog dlg;
    6. connect( &dlg, SIGNAL(setAccessLevel(quint16)), this, SLOT(getAccessLevel(quint16)));
    7.  
    8. dlg.exec();
    9. }
    To copy to clipboard, switch view to plain text mode 

    or

    2 - Pass the pointer to the MainWindow instance to MyDialog and make the connection there:

    Qt Code:
    1. // MainWindow.cpp
    2. void MainWindow::showDialog()
    3. {
    4. MyDialog dlg( this );
    5. dlg.exec();
    6. }
    7.  
    8. // MyDialog.cpp
    9. #include "MainWindow.h"
    10.  
    11. MyDialog::MyDialog( MainWindow * pMain, QWidget * pParent /*= 0*/ )
    12. : QDialog( pParent )
    13. {
    14. // Assumes MyDialog has a signal "setAccessLevel" and MainWindow has a slot "getAccessLevel"
    15. connect( this, SIGNAL(setAccessLevel(quint16)), pMain, SLOT(getAccessLevel(quint16)));
    16. }
    To copy to clipboard, switch view to plain text mode 

    My personal preference is to do it the first way. MainWindow needs to know about MyDialog because it has to create it and show it, so it might as well connect to its signals. On the other hand, MyDialog doesn't need to know anything about who is creating and showing it. Its job is to simply tell anyone who is listening to its signals when something changes.
    <=== 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. #7
    Join Date
    Jan 2017
    Posts
    58
    Thanks
    2
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QDialog signal and slots

    Yeah, I got it. It works and I agree with you, the first solutions looks better.
    Thanks again!

    edit: One more question. This is fine solution if I make new form. But what if form exist?
    e.g.: After successfully loggin I save access level in private variable in MainWindow, but now in Settings tab I need this variable. What should I do? I'm trying make public function, sth like "quint8 getAccess(){return _access;}", but if I understand you helps, I can't do "MainWindow *mainWindow;" (or without pointer).
    Last edited by ado130; 30th January 2017 at 20:58.

  9. #8
    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: QDialog signal and slots

    There a multiple ways of getting access to data from different classes.

    A data object both classes have access to, for example, or one class passing data to the other, or, as you said, one class being accessible to the other and having a getter function.

    This is really not related to Qt at all, but basic C++.

    Cheers,
    _

Similar Threads

  1. Qt5/C++ - Signals and slots, QMainwindow to QDialog
    By jimbo in forum Qt Programming
    Replies: 6
    Last Post: 2nd October 2016, 13:40
  2. Replies: 2
    Last Post: 30th November 2013, 00:19
  3. Signal and Slots
    By waynew in forum Newbie
    Replies: 3
    Last Post: 20th November 2009, 03:50
  4. signal and slots
    By vermarajeev in forum Qt Programming
    Replies: 4
    Last Post: 16th October 2007, 08:31

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.