PDA

View Full Version : Override a signal



daviddoria
9th February 2010, 14:14
I want to set the text of a label when I click a button.

I have a button called 'pushButton' and a label called 'label'.

My form class is like this (buttonform.h):




#ifndef BUTTONFORM_H
#define BUTTONFORM_H

#include "ui_buttonform.h"

class ButtonForm : public QWidget
{
Q_OBJECT

public:
ButtonForm(QWidget *parent = 0);

private slots:
//void pushButton_Clicked();

signals:
void pushButton_Clicked();
private:
Ui::ButtonForm ui;
};

#endif



In buttonform.cpp, I had this:



#include <QtGui>

#include "buttonform.h"
#include <iostream>

ButtonForm::ButtonForm(QWidget *parent)
: QWidget(parent)
{
ui.setupUi(this);
}

void ButtonForm::pushButton_Clicked()
{
ui.label->setText("hello");
}


but it tells me that pushButton_Clicked() is already defined in moc_buttonform.cxx.

How should I go about doing this?

Thanks!

Dave

Ginsengelf
9th February 2010, 14:59
Hi, you are implementing a signal, which is already done by the moc for you and therefore you get the error.
I think what you want is the line you commented: a slot to connect the button's clicked() signal.

Ginsengelf

daviddoria
9th February 2010, 15:43
Ok, I guess the reason I didn't know how to do that is that I didn't know how to associate the slot with a particular widget (the pushButton). I think the answer is that you don't - you just associate it with the form:

The problem now is that it says "No such slot ButtonForm::pushButton_SetLabelText(QString& txt)

Here is what I have:

buttonform.cpp



#include <QtGui>

#include "buttonform.h"
#include <iostream>

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


connect( this->ui.pushButton, SIGNAL( clicked() ), this, SLOT(pushButton_SetLabelText(QString& txt)) );

}

void ButtonForm::pushButton_SetLabelText(QString& txt)
{
this->ui.label->setText(txt);
}



buttonform.h



#ifndef BUTTONFORM_H
#define BUTTONFORM_H

#include "ui_buttonform.h"

class ButtonForm : public QWidget
{
Q_OBJECT

public:
ButtonForm(QWidget *parent = 0);

private slots:
void pushButton_SetLabelText(QString& txt);

signals:
//void pushButton_Clicked();
private:
Ui::ButtonForm ui;
};

#endif



main.cpp



#include <QApplication>

#include "buttonform.h"

int main(int argc, char *argv[])
{
QApplication app(argc, argv);
ButtonForm buttonform;

buttonform.show();
return app.exec();
}



I feel like this is very close!

Thanks for your help,

Dave

daviddoria
9th February 2010, 17:45
Ah, got it. The problem was the Clicked() signal did not pass a QString

I changed it to:


void pushButton_SetLabelText();


and




ButtonForm::ButtonForm(QWidget *parent)
: QWidget(parent)
{
ui.setupUi(this);
connect( this->ui.pushButton, SIGNAL( clicked() ), this, SLOT(pushButton_SetLabelText()) );



}

void ButtonForm::pushButton_SetLabelText()
{
this->ui.label->setText("hello");
}


I thought one of the main points of the Signal/Slot idea was to stop type problems like this (e.g. passing nothing to something expecting a QString)?

Dave

squidge
9th February 2010, 18:18
You don't need the connect call, just name your function like so:



void on_pushButton_clicked()
{
// foo
}


and everything is done for you :)

You only need the connect call if you want to call your function something different than the normal.