You are not logged in.

Dear visitor, welcome to QtForum.org. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

1

Saturday, July 24th 2010, 4:04pm

Changing QLineEdit text with QPushButton

Hey guys! I'm a qt noob so don't bash me :P

I'm trying to change the text of a line edit box with a button. I wrote the following program:

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <QWidget>
#include <QVBoxLayout>
#include <QPushButton>
#include <QLineEdit>

int main(int argc, char** argv)
{
    QApplication *app = new QApplication(argc, argv);
    QWidget *window = new QWidget;
    QVBoxLayout *layout = new QVBoxLayout;
    QPushButton *button = new QPushButton("Click Me!");
    QLineEdit *edit = new QLineEdit("My first Text Edit box!");
    window->setWindowTitle("My first window!");
    layout->addWidget(edit);
    layout->addWidget(button);
    window->setLayout(layout);
    QObject::connect(button, SIGNAL(clicked()), edit, SLOT(setText(QString)));
    edit->setText("Clicked!");
    window->show();
    return app->exec();
}

The problem is I keep getting the following error/warning message from QtCreator:
QObject::connect: Incompatible sender/receiver arguments
QPushButton::clicked() --> QLineEdit::setText(QString)

Any suggestions? Thanks in advance!

I'm running Qt 4.7 on Mac OS X

2

Saturday, July 24th 2010, 4:25pm

The connection doesn't work, because QPushButton's clicked() signal supplies no argument, whereas QLineEdit::setText( QString ) expects a QString. For a valid connection, the Slot is called with the arguments supplied to the Signal, so their argument lists must match.

Furthermore, you don't need the connection here, because you call edit->setText("Clicked!") manually. (If you had a working connection, this would not be necessary.)

To implement your first connection, change your connect line into:

Source code

1
QObject::connect(button, SIGNAL(clicked()), edit, SLOT(clear()));

and remove line 18.

3

Saturday, July 24th 2010, 5:01pm

Got it! Thanks rara!