Results 1 to 5 of 5

Thread: Learning how to send SIGNALS from a QDialog to a SLOT QT5

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

    Default Learning how to send SIGNALS from a QDialog to a SLOT QT5

    Hi i am started to learn Qt5, it is strange to me because i came from GTK+.

    So my question has with how to emmit a signal from an QDialog object.
    The QDialog's pushButton ( the form has only one pushButton ) every time that i press the pushButton to call a method from another class.

    The second class has a SLOT as :

    public slots :
    void value();



    how do i use the connect method at main.cpp?
    main has as :

    #include "dialog.h"
    #include <QApplication>

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    Dialog w;
    Coutput out;

    // QObject::connect(&w, SIGNAL(clicked()), &out, SLOT(value()));
    /* what i should filled as a connect's source ? */


    w.show();

    return a.exec();
    }


    I refresh that the w is a Dialog object from class
    namespace Ui {
    class Dialog;
    }

    class Dialog : public QDialog
    {
    Q_OBJECT

    public:
    explicit Dialog(QWidget *parent = 0);
    ~Dialog();

    private:
    Ui:ialog *ui;
    };



    which has been Qdesigner

    Thank you.

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

    Default Re: Learning how to send SIGNALS from a QDialog to a SLOT QT5

    You have many options here. For example, you can expose the "ui" object of your class, and connect to it in main():
    Qt Code:
    1. class Dialog : public QDialog
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. explicit Dialog(QWidget *parent = 0);
    7. ~Dialog();
    8.  
    9. Ui::Dialog *ui;
    10. };
    11.  
    12. // and in main..
    13. QObject::connect(w.ui->pushButton, SIGNAL(clicked()), &out, SLOT(value()));
    To copy to clipboard, switch view to plain text mode 
    But that's not a proper OO design, yes ?
    Another option is to define new signal in your dialog class, let's say "buttonClicked()", connect a button to this signal, and then use it in main():
    Qt Code:
    1. class Dialog : public QDialog
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. explicit Dialog(QWidget *parent = 0);
    7. ~Dialog();
    8.  
    9. signals:
    10. void buttonClicked();
    11. private:
    12. Ui::Dialog *ui;
    13. };
    14.  
    15. // Dialog constructor:
    16. Dialog::Dialog(QWidget * parent) : QDialog(parent)
    17. {
    18. ui = new Ui... //something
    19. ui->setupUi(this);
    20. connect(ui->pushButton, SIGNAL(clicked()), this, SIGNAL(buttonClicked()));
    21. }
    22.  
    23. // in main
    24. QObject::connect(&w, SIGNAL(buttonClicked()), &out, SLOT(value()));
    To copy to clipboard, switch view to plain text mode 

    nice explanation of signals & slots can be found here : link

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

    chasapis.christos (12th November 2013)

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

    Default Re: Learning how to send SIGNALS from a QDialog to a SLOT QT5

    Dear stampede,
    the header file (generated by QT Design) has as :

    1.] class Dialog : public QDialog{
    2.] Q_OBJECT
    3.]public:
    4.] explicit Dialog(QWidget *parent = 0);
    5.] ~Dialog();

    6.]private:
    7.] Ui::Dialog *ui;
    };





    at line 1 : creates a class named Dialog witch inherits the class QDialog.
    at line 2 : Has the Q_OBJECT macro
    at line 4 : declares as explicit the constractor method of the class with parameter a pointer of a QWidjet object
    at line 5 : declares the constructor of the class
    and finaly at the private sector of the class (line 7) creates a private pointer of Dialog class into the NameSpace Ui. So he has a pointer of class of the same class

    The namespace of the class has been defined as Ui
    namespace Ui {
    class Dialog;
    }

    the header file.


    Now. At the cpp he has as bodies of methods the followings :

    1.]Dialog::Dialog(QWidget *parent) : QDialog(parent), ui(new Ui::Dialog){
    2.] ui->setupUi(this);
    3.]}

    Dialog::~Dialog()
    {
    delete ui;
    }


    could you explain me what the line 1 does?
    he declare the method Dialog(QWidget *parent) as is declared at the header file and then does multiple inheritence by passing the parent pointer to QDialog method method (parent object) and to the parent ui method as parameter pass a new UI oject that points to the Dialog??

    And finaly he use the local (i mean the private ui) pointer to call the function setupUi of this object...



    could you make me that clear?


    Thanks!

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

    Default Re: Learning how to send SIGNALS from a QDialog to a SLOT QT5

    So he has a pointer of class of the same class
    Well, you have some unfortunate naming scheme here.
    class Dialog from line 1 (which inherits QDialog) is not the same as class Dialog forward declared in the Ui namespace.
    Ui::Dialog refers to the class which is automatically generated by uic from the *.ui file, in order to translate your visual design into c++ code needed to setup the interface (btw. this Ui:: class will have the same name as the main object in your .ui form).
    So it's important to understand the difference - Dialog is a class which implements the logic of QDialog, but Ui::Dialog is a class which implements the user interface setup. Those two are not the same class.
    When you call ui->setupUi(widget), you are telling the ui object to apply the interface setup on a given widget. It makes the widget look like the stuff you edited with designer, basically.
    Here, a this pointer is passed, which means to setup the look of current object.
    This is all quite simple, but I understand the confusion caused by the Dialog name.
    I hope it's more clear for you now.

    he declare the method Dialog(QWidget *parent) as is declared at the header file and then does multiple inheritence by passing the parent pointer to QDialog method method (parent object) and to the parent ui method as parameter pass a new UI oject that points to the Dialog??
    Sorry but now I'm confused

  6. The following user says thank you to stampede for this useful post:

    chasapis.christos (13th November 2013)

  7. #5
    Join Date
    Oct 2013
    Posts
    3
    Thanks
    2
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Learning how to send SIGNALS from a QDialog to a SLOT QT5

    Thanks!
    Namespace confusion and miss notice... :-(

Similar Threads

  1. live time of objects send via signals
    By tuli in forum Qt Programming
    Replies: 7
    Last Post: 28th November 2012, 09:36
  2. Problems with QNetworkRequest, it does not send signals.
    By nilhcraiv in forum Qt Programming
    Replies: 5
    Last Post: 21st October 2011, 20:00
  3. how to you send signals to a graphics item?
    By technoViking in forum Qt Programming
    Replies: 4
    Last Post: 6th November 2009, 19:40
  4. Replies: 7
    Last Post: 29th May 2009, 08:58
  5. send OS signals to apps on all platforms
    By Morea in forum Qt Programming
    Replies: 3
    Last Post: 21st November 2008, 22:00

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.