PDA

View Full Version : Learning how to send SIGNALS from a QDialog to a SLOT QT5



chasapis.christos
8th November 2013, 17:35
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::Dialog *ui;
};



which has been Qdesigner

Thank you.

stampede
8th November 2013, 18:03
You have many options here. For example, you can expose the "ui" object of your class, and connect to it in main():


class Dialog : public QDialog
{
Q_OBJECT

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

Ui::Dialog *ui;
};

// and in main..
QObject::connect(w.ui->pushButton, SIGNAL(clicked()), &out, SLOT(value()));


But that's not a proper OO design, yes ?:D
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():


class Dialog : public QDialog
{
Q_OBJECT

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

signals:
void buttonClicked();
private:
Ui::Dialog *ui;
};

// Dialog constructor:
Dialog::Dialog(QWidget * parent) : QDialog(parent)
{
ui = new Ui... //something
ui->setupUi(this);
connect(ui->pushButton, SIGNAL(clicked()), this, SIGNAL(buttonClicked()));
}

// in main
QObject::connect(&w, SIGNAL(buttonClicked()), &out, SLOT(value()));



nice explanation of signals & slots can be found here : link (http://qt-project.org/doc/qt-5.0/qtcore/signalsandslots.html)

chasapis.christos
12th November 2013, 18:19
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!

stampede
12th November 2013, 18:41
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 :)

chasapis.christos
13th November 2013, 10:15
Thanks!
Namespace confusion and miss notice... :-(