1

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.

Jean-Luc
  • 3,563
  • 8
  • 40
  • 78
  • Have a look at my answer to a similar question, here: http://stackoverflow.com/questions/5153157/passing-an-argument-to-a-slot/5153522#5153522 – TonyK Dec 03 '11 at 08:06
  • You can follow my blog for newbies of Qt application development: https://qtnoobies.blogspot.my/ – GeneCode Sep 25 '17 at 05:48

3 Answers3

1

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.

user957121
  • 2,946
  • 4
  • 24
  • 36
0

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.

Arnold Spence
  • 21,942
  • 7
  • 74
  • 67
  • Thanks for the advice; however, I'm aware of the "Edit Signals/Slots" mode, I guess what I was wondering was whether there is some way to drag, say, a pushbutton to a custom slot (a slot that I've made). – Jean-Luc Dec 03 '11 at 11:38
  • Yes. If you have already defined a slot, you can drag a connector to the object that has the slot and wire them up in the resulting dialog. – Arnold Spence Dec 03 '11 at 18:17
  • Thanks for the help. I think that I understand much better now. In the document it says this towards the end: "...if you were to implement a palette tool allowing the user to choose a color from a set of standard colors and needed to emit a colorSelected(const QColor &) signal, your best bet would be to use the sender() approach or the subclass approach described above." I was wondering if I would need a if-else statements to say, for instance: if(button1 == (*pusbutton) sender()) // change palette colour to blue else if(button2 == (*pusbutton) sender()) // change palette colour red etc. – Jean-Luc Dec 04 '11 at 11:15
0

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.