Jean-Luc
Jean-Luc

Reputation: 3613

Qt SLOTS parameters (Beginner)

I'm a beginner to Qt and am making (or at least trying to make) a basic calculator. If I understand correctly, when doing this:

connect(my_button_4, SIGNAL(clicked()), this, SLOT(writeNumberLbl("4")));

The "4" is not accessible (rather, only its type is) in writeNumberLbl. Basically, I would like so that when the button is clicked, the label sets its text to "4". However, I have the numbers 0 to 9, so I wanted to do:

connect(my_button_0, SIGNAL(clicked()), this, SLOT(writeNumberLbl("0")));
connect(my_button_1, SIGNAL(clicked()), this, SLOT(writeNumberLbl("1")));
...
connect(my_button_9, SIGNAL(clicked()), this, SLOT(writeNumberLbl("9")));

My writeNumberLbl function is:

void Calculator::preWriteVal(QChar val)
{
    QString curVal = ui.lbl_output->text();
    curVal += val;
    ui.lbl_output->setText(curVal);
}

However, I can see that this will not work due to the parameter, 'val'. Could someone please point me in the right direction? Thank you. I did look to see if this question had already been answered and couldn't find anything. If it has, please provide me a link.

Also, is it possible, using Qt Designer 4, to connect a widget to a custom slot?

Thank you.

Upvotes: 1

Views: 4591

Answers (3)

user297171
user297171

Reputation:

The number of parameters in Slot can not exit those in Signal? and pressed() has none. You have two choices (three, counting the dumb one):

  1. Use QSignalMapper. Its help is self-explanatory.
  2. Connect all your buttons to single slot. In it, find out what button has been pressed. QObject::sender() function helps.

There are even more ways, but more complicated.

Upvotes: 0

user957121
user957121

Reputation: 3076

As far as I know,Qt's signal/slot system requires that the number of parameters of signal function is not less than that of slots function. In your example,the signal function click() have zero parameters and slot function writeNumberLbl("4") have one parameter,this will not be accepted by the Qt's signal/slot system and if you run your program in debug mode of Qt creator,the qDebug will show you a message like 'Incompatiable signal/slot' blalbalba~. To solve this problem, just read the article given by Arnold Spence. It is quite clear.

Upvotes: 1

Arnold Spence
Arnold Spence

Reputation: 22292

There are a number of ways to tackle this problem and they are outlined very nicely here. Although that page is a bit old, I think it is still quite valid. I would recommend using a signal mapper.

For your second question, yes. You can connect signals and slots using Qt Designer by setting the designer in "Edit Signals/Slots" mode. Once in this mode, for example, you can drag a connection line from a button to the form. A dialog will open up allowing you to choose the signal and slot to connect. If you haven't already implemented a slot in code, you can specify the name of a slot and then add the code for it afterward.

Upvotes: 0

Related Questions