PDA

View Full Version : Wierd behaviour in a slot routine



koenig
28th January 2010, 13:09
Hi,

Firslty I'm very new to QT (as well as GUI programming) but quickly falling in love with it :)

Having said that however, I have hit my first wall which is probably a lack of understanding something. I am useing eclipse with QT integration (V4.6).

As a test I have crated a form using the designer with a simple text entry widget and a button

I then created a slot for the buttons clicked signal

In the routine processing the signal I read the text from the entry and set the text for the button label to reflect that.

Copiled and ran fine. I can enter text in the entry box and when I click on the button it's label changes to what I entered.

However the text in the entry box does not get cleared so I issued the clear method on it following the set text method on the button.

Now when I click on th button the text in the entry box gets cleared which is fine, but so does the label of the button which I don't get.

Below is the implementation of the slot function:


void qt_test::on_enterButton_clicked()
{
QString buttonName = ui.lineInput->displayText();
ui.enterButton->setText(buttonName);
ui.lineInput->clear();
}


ui.lineIput is of class QLineEdit
ui.enterButton is of class QPushButton

inially the push button has the text "Push" on it then when I push it it get cleared irrespective of what has been entered in the lineInput

The itneresting thing is that if I do this:


void qt_test::on_enterButton_clicked()
{
QString buttonName = ui.lineInput->displayText();
buttonName += "ABC";
ui.enterButton->setText(buttonName);
ui.lineInput->clear();
}

The push button text gets set to "ABC" again irrespective of what is entered in lineInput.

Any one able to tell me why this is so?

Thanks!

boudie
28th January 2010, 15:46
This works here without any problem:


void MainWindow::on_enterButton_clicked()
{
QString buttonName = ui->lineEdit->displayText();
ui->pushButton->setText(buttonName);
ui->lineEdit->clear();
}

I made the form with QtCreator 1.3.0.

koenig
28th January 2010, 22:30
Ok well that stumps me then :)

I guess one other bit of info is that I have been doing this on Windows 7, although can't think of any dependacy on the OS off the top of my head that would cause such a thing.

boudie
28th January 2010, 23:30
Ok, Let's change


QString buttonName = ui.lineInput->displayText();

into this


QString buttonName ( ui.lineInput->displayText());

koenig
29th January 2010, 00:05
Ok tried that, made no differance.

I also just updated to Qt 4.6.1 (changed all the paths appropriately too) that also made no differance

koenig
29th January 2010, 00:09
For completeness I'm posting all code from this project, maybe I've stuffed something elsewhere :)

main.cpp:


#include "qt_test.h"

#include <QtGui>
#include <QApplication>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
qt_test w;
w.show();
return a.exec();
}


ui_qt_test.h:


#ifndef UI_QT_TEST_H
#define UI_QT_TEST_H

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

QT_BEGIN_NAMESPACE

class Ui_qt_testClass
{
public:
QTextBrowser *textOut;
QLineEdit *lineInput;
QPushButton *enterButton;
QPushButton *quitButton;

void setupUi(QWidget *qt_testClass)
{
if (qt_testClass->objectName().isEmpty())
qt_testClass->setObjectName(QString::fromUtf8("qt_testClass"));
qt_testClass->resize(661, 407);
textOut = new QTextBrowser(qt_testClass);
textOut->setObjectName(QString::fromUtf8("textOut"));
textOut->setGeometry(QRect(80, 60, 511, 192));
lineInput = new QLineEdit(qt_testClass);
lineInput->setObjectName(QString::fromUtf8("lineInput"));
lineInput->setGeometry(QRect(80, 270, 291, 20));
enterButton = new QPushButton(qt_testClass);
enterButton->setObjectName(QString::fromUtf8("enterButton"));
enterButton->setGeometry(QRect(410, 270, 75, 23));
quitButton = new QPushButton(qt_testClass);
quitButton->setObjectName(QString::fromUtf8("quitButton"));
quitButton->setGeometry(QRect(410, 360, 75, 23));

retranslateUi(qt_testClass);
QObject::connect(quitButton, SIGNAL(clicked()), qt_testClass, SLOT(close()));

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

void retranslateUi(QWidget *qt_testClass)
{
qt_testClass->setWindowTitle(QApplication::translate("qt_testClass", "qt_test", 0, QApplication::UnicodeUTF8));
enterButton->setText(QApplication::translate("qt_testClass", "Enter", 0, QApplication::UnicodeUTF8));
quitButton->setText(QApplication::translate("qt_testClass", "Quit", 0, QApplication::UnicodeUTF8));
} // retranslateUi

};

namespace Ui {
class qt_testClass: public Ui_qt_testClass {};
} // namespace Ui

QT_END_NAMESPACE

#endif // UI_QT_TEST_H


qt_test.h:


#ifndef QT_TEST_H
#define QT_TEST_H

#include <QtGui/QWidget>
#include "ui_qt_test.h"

class qt_test : public QWidget
{
Q_OBJECT

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

public slots:
void on_enterButton_clicked();

private:
Ui::qt_testClass ui;
};

#endif // QT_TEST_H



qt_test.cpp:


#include "qt_test.h"
#include <QString>

qt_test::qt_test(QWidget *parent)
: QWidget(parent)
{
ui.setupUi(this);
connect(ui.enterButton, SIGNAL(clicked()), this, SLOT(on_enterButton_clicked()));
}

qt_test::~qt_test()
{

}

void qt_test::on_enterButton_clicked()
{
QString buttonName (ui.lineInput->displayText());
buttonName += "ABC";
ui.enterButton->setText(buttonName);
ui.lineInput->clear();


}

koenig
29th January 2010, 02:48
Oh I've figured it out!!!! :)

As I'm very new to this framework I got caught by the signal/slot connection method, I didn't realise that when I named the slot function on_enterButton_clicked() the framework would implicitly connect the the clicked signal for me. So I also issued an explicit connect statement in the constructor. As a result the slot function was being executed twice, and so the second time it exectued the text bas was already cleared and so the button label also got cleared.

D'OH!!!! :)

Anyway, thanks for the help, actually just knowing that it work for someone else made me dig in different directions, I was starting to loose my mind there a bit :)

boudie
29th January 2010, 07:27
LOL!
:D:D:D

And nobody noticed it...