PDA

View Full Version : twice execution of click on pushbutton?



rambo83
10th November 2009, 11:03
Hello,

I'm very confused about the fact, that if I click (left mouse button) on a pushbutton, then the function connected via SLOT to the SIGNAL of pushbuttons is executed twice. Why? I want it to be executed only one time! How to avoid this behaviour?

I have created my GUI Framework by means of Qt Designer 4.5.1 and then connect the SLOTs automatically by the command "QMetaObject::connectSlotsByName(this);"

Here is the part of cpp code:


#include <iostream>
#include <time.h>

#include <QtUiTools>
#include <QtGui>

#include "mainframe.h"
#include "Vertex.h"
#include "graphicscene.h"

using namespace std;

mainFrame::mainFrame(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);

ui_pushButton_START = qFindChild<QPushButton*>(this, "pushButton_START");
ui_pushButton_Select = qFindChild<QPushButton*>(this, "pushButton_Select");
ui_pushButton_Recombine = qFindChild<QPushButton*>(this, "pushButton_Recombine");

/*
connect(ui_pushButton_START, SIGNAL(clicked()), this, SLOT(on_pushButton_Recombine_clicked()));
connect(ui_pushButton_Select, SIGNAL(clicked()), this, SLOT(on_pushButton_Select_clicked()));
connect(ui_pushButton_Recombine, SIGNAL(clicked()), this, SLOT(on_pushButton_Recombine_clicked()));
*/

QMetaObject::connectSlotsByName(this);

and here a part of header file:


class mainFrame : public QMainWindow
{
Q_OBJECT

public:
mainFrame(QWidget *parent = 0);
~mainFrame();



// here the slots are declared, which are kind of sink for connected objects
private slots:
void on_pushButton_Quit_clicked();
void updateParameters();
void on_pushButton_START_clicked();
void on_pushButton_Recombine_clicked();
void on_pushButton_Select_clicked();

Help me please to avoid this failure function. Thank you.

best regards,

Vitali

yogeshgokul
10th November 2009, 11:20
Please comment this line and see the results.

QMetaObject::connectSlotsByName(this);

Goldmmbr
10th November 2009, 11:30
i am guessing it is because of:



ui_pushButton_START = qFindChild<QPushButton*>(this, "pushButton_START");
ui_pushButton_Select = qFindChild<QPushButton*>(this, "pushButton_Select");
ui_pushButton_Recombine = qFindChild<QPushButton*>(this, "pushButton_Recombine");


i think it's causing connectSlotsByName to connect the signal/slot for each button twice because you have an aditional pointer ...
i may be wrong though...

spirit
10th November 2009, 11:30
notice: QMetaObject::connectSlotsByName(this); is also used in generated ui_classname.h file.

rambo83
10th November 2009, 11:41
You are right, now it works! But why? If I don't connect the SLOTs and SIGNALs automatically or manually, the click on button should not call the corresponding function at all, but it does now. Why? :)

yogeshgokul
10th November 2009, 11:47
You are right, now it works! But why? If I don't connect the SLOTs and SIGNALs automatically or manually, the click on button should not call the corresponding function at all, but it does now. Why? :)
Because you named them(slots) like that.
If you have a QPushButton named btn. And you created a slot named on_btn_clicked().
It will automatically get connected. ;)
QT ROCKS :D

spirit
10th November 2009, 11:48
because of this "notice: QMetaObject::connectSlotsByName(this); is also used in generated ui_classname.h file."
read this QMetaObject::connectSlotsByName.

squidge
10th November 2009, 12:06
You are right, now it works! But why? If I don't connect the SLOTs and SIGNALs automatically or manually, the click on button should not call the corresponding function at all, but it does now. Why? :)

setupUi() calls connectSlotsByName. You are then calling it yourself too, thus you get two function call per click :)

rambo83
10th November 2009, 12:21
Thank you very much for the explanations. They were very helpful.