[SOLVED]setText not working
-
hello,
I have a rally noob question here, and its that setText is not working. for example"
connect(pushButton1, SIGNAL(clicked()), lineEdit, SLOT(setText(QString("1"))));
it then underlines the line red saying that it expectd a semicolon but got a parenthesis. I checked all the brackets and they seem to line up, and it happens to be the "1" in the setText that causes the problem. any help?
-
You've got a wrong idea of how a signal-slot connection works. The slot parameter is taken from a matching signal argument if it has one, not from you directly.
For example if there is a signal
somethingHappened(int foo)
then a slotdoSomething(int bar)
will receive the parameter from the signal:connect(senderObject, SIGNAL(somethingHappened(int)), receiverObject, SLOT(doSomething(int)))
.So in short you can't pass an argument (
QString("1")
) at a connection site.On the other hand there are a few more
connect()
overloads that don't take SIGNAL and SLOT macros. For example you can connect a lambda like this:connect(pushButton1, &QPushButton::clicked, [=]{ lineEdit->setText(QString("1"); });
-
but isn't setText a public slot of QlineEdit
Yes, a slot that takes a string parameter. The
clicked()
signal passes no parameters so there's no way to connect these two.
As I said the SLOT macro does not take parameters from you. It takes it from the signal you connect it to, so they need to match. -
is there any pushbutton signal that has a qstring parameter
No, buttons don't generate text. They generate clicks.
So you can either subclass a button and give it a signal that generates text (but that is really messed up conceptually) or simply connect the click to something that doesn't require a parameter (like the lambda example in my previous post).