PDA

View Full Version : Where is the connection declared?



szisziszilvi
23rd August 2011, 09:43
Hi,

I've just learned from a tutorial that I can define my own slot for a pushbutton using the creator like this:
- put a QPushButton on the Gui in the built-in designer.
- right click on the button -> "Go to slot..."
- chose "clicked()"

And than the creator creates the new slot in the header and jumps to the implementation and I can work on it.

Now my question is just technical: Where is it declared that this signal-slot (namely clicked()-on_pushButton_clicked()) connection exists? I cannot see a connect(...) line anywhere, nor a footprint in the designer, but it is surely there somewhere as it works fine. Personally I like better to define the connections in the code becasure I can than better see what connections exist and what not, but this mechanism is really pretty so I may want to use it sometimes but I can't see why it works.

Rachol
23rd August 2011, 10:03
What I did:

Opened designer, new widget, added push button, added radioButton, used slot/editor to make the connection.

Output from Form->View code.. :


/************************************************** ******************************
** Form generated from reading UI file 'designergq4768.ui'
**
** Created: Tue 23. Aug 10:57:54 2011
** by: Qt User Interface Compiler version 4.7.0
**
** WARNING! All changes made in this file will be lost when recompiling UI file!
************************************************** ******************************/

#ifndef DESIGNERGQ4768_H
#define DESIGNERGQ4768_H

#include <QtCore/QVariant>
#include <QtGui/QAction>
#include <QtGui/QApplication>
#include <QtGui/QButtonGroup>
#include <QtGui/QHeaderView>
#include <QtGui/QPushButton>
#include <QtGui/QRadioButton>
#include <QtGui/QWidget>

QT_BEGIN_NAMESPACE

class Ui_Form
{
public:
QPushButton *pushButton;
QRadioButton *radioButton;

void setupUi(QWidget *Form)
{
if (Form->objectName().isEmpty())
Form->setObjectName(QString::fromUtf8("Form"));
Form->resize(400, 300);
pushButton = new QPushButton(Form);
pushButton->setObjectName(QString::fromUtf8("pushButton"));
pushButton->setGeometry(QRect(50, 90, 75, 23));
radioButton = new QRadioButton(Form);
radioButton->setObjectName(QString::fromUtf8("radioButton"));
radioButton->setGeometry(QRect(220, 90, 82, 17));

retranslateUi(Form);
QObject::connect(pushButton, SIGNAL(clicked()), radioButton, SLOT(toggle()));

QMetaObject::connectSlotsByName(Form);
} // setupUi

void retranslateUi(QWidget *Form)
{
Form->setWindowTitle(QApplication::translate("Form", "Form", 0, QApplication::UnicodeUTF8));
pushButton->setText(QApplication::translate("Form", "PushButton", 0, QApplication::UnicodeUTF8));
radioButton->setText(QApplication::translate("Form", "RadioButton", 0, QApplication::UnicodeUTF8));
} // retranslateUi

};

namespace Ui {
class Form: public Ui_Form {};
} // namespace Ui

QT_END_NAMESPACE

#endif // DESIGNERGQ4768_H


There you have one connection that has been made by using signal/slot editor and you have also a method that does automatic connections from matching signals of children objects to slots of that object, for example to on_pushButton_clicked(). That's the line:


QMetaObject::connectSlotsByName(Form);

szisziszilvi
23rd August 2011, 14:47
yeah, but that is generated when it is compiled. I'm sure about that, because it works even if I delete everithying from the xxx-build-desktop folder where it is going to be compiled.
Is it maybe an atomatic mechanism wheneve I name a slot with "on_objectnaem_clicked()"?

Rachol
23rd August 2011, 15:09
Yes and Yes.

high_flyer
23rd August 2011, 15:16
Is it maybe an atomatic mechanism wheneve I name a slot with "on_objectnaem_clicked()"?
the forms you design in designer are saved in *.ui files.
These are just XML representation of your gui.
Part of your make sequence calls uic (the ui compiler) which translated the *.ui files to C++.

marcvanriet
24th August 2011, 03:52
Where is it declared that this signal-slot (namely clicked()-on_pushButton_clicked()) connection exists? .... Personally I like better to define the connections in the code becasure I can than better see what connections exist and what not, but this mechanism is really pretty so I may want to use it sometimes but I can't see why it works.

See http://doc.qt.nokia.com/...automatic-connections (http://doc.qt.nokia.com/4.7/designer-using-a-ui-file.html#automatic-connections)

Die-hard Qt programmers prefer to write their own connect() statements instead of using that 'pretty' mechanism by just declaring their own slot, and use connect() in the constructor of their form to make the connection. But then again... they might also use vi ... :rolleyes:

Regards,
Marc

szisziszilvi
24th August 2011, 09:47
Thanks for the link. Now I see what is happening.

However a few coments on the topic. Whilst everybody is speaking about this line:

QMetaObject::connectSlotsByName(this);
actually this line is just nowhere in the project files. Can be pretty confusing that it appears in the ui_mainwindow.h because this header file does not even exist before compiling. So assuming we have only these files:
main.cpp
mainwindow.cpp
mainwindow.h
mainwindow.ui
myproject.pro

The mentioned line can not be found in these files. The tricky thing is that when generating ui_mainwindow.h the critical line is _always_added - no matter if I created any connection with the above described method or not. So I can also declare them by hand with the proper syntax and the magical "right click - go to slot - clicked()" mechanism does the very same and adds no additional clue to the code.

wysota
24th August 2011, 10:15
Whilst everybody is speaking about this line:

QMetaObject::connectSlotsByName(this);
actually this line is just nowhere in the project files.

It is in setupUi() that is generated by uic from .ui files.


The mentioned line can not be found in these files. The tricky thing is that when generating ui_mainwindow.h the critical line is _always_added - no matter if I created any connection with the above described method or not.
This mechanism does not depend on the way you create your UIs. Using QtCreator for that is not the only option. Don't assume everybody uses QtCreator's slot facilities only because you do.