views:

287

answers:

3

I'm writing a simplified calculator using Qt with C++, for learning purposes. Each number is a QPushButton that uses the same slot to modify the text in a lineEdit widget being used as a display.

The slot uses the sender() method to figure out which button was pressed, so the correct number would be written on the display widget.

In order to have all the buttons working, I'd have to write a connection to each one of them, kinda like this:

connect(ui->button1, SIGNAL(clicked()), this, SLOT(writeNum()));

Since they all use the same slot, the only thing that changes is the button being used, so the next sender would be ui->button2, ui->button3, and so on. My question is, is there a way to reduce the number of defined connections?

Edit: Here's a useful link discussing precisely about this problem, in detail.

+1  A: 

I think you can try allocating the QPushButton in a array, something like this

QPushButton* numbers = new QPushButton[10];

And then, perform the connections using a for loop

for(size_t i = 0; i < 9; ++i)
{
  connect(numbers[i],SIGNAL(clicked()),this,SLOT(writeNum()));
}

But I don't think it's worth. Explicit connection, while making the code more verbose, make the connections more clear to the reader.

cake
Question was actually to reduce the connections, you just restructure it. I doubt there will be any difference in binary (if you enable basic optimizations).
penguinpower
@soxs060389 True. But evewn using things like QSignalMapper, probably the number of connections would remain the same, only with an extra layer of abstraction hiding it.
cake
you are right, but it is possible to hide n-1 of n connections from the user, if you create a nice class structure
penguinpower
+2  A: 

If you use QtDesigner or the form editor of QtCreator you can just drag lines between the 2 and it will fill in the code for you.

You could also keep all the buttons in a list structure, but I would use a QVector not a standard array.

You might also want to reconsider using the sender() method, it violates OOP design. Instead connect all the buttons to a QSignalMapper and then connect mapped() to your text box.

Adam W
Thanks for the tip on QSignalMapper, I kinda felt that there would be a better way to do it.
David McDavidson
+2  A: 

You should use an int in this case to identify the button which sent the signal to your slot. Essentially you use QSignalMapper for that task:

QSignalMapper sm;

QPushButton* one = new QPushButton(this);
QPushButton* two = new QPushButton(this);
QPushButton* three = new QPushButton(this);
//and so on...

sm.setMapping(one, 1);
sm.setMapping(two, 2);
sm.setMapping(three, 3);
//and so on...

connect(one,  SIGNAL(clicked()), &sm, SLOT(map()));
connect(two,  SIGNAL(clicked()), &sm, SLOT(map()));
connect(three,  SIGNAL(clicked()), &sm, SLOT(map()));
//and so on...



connect(&sm, SIGNAL(mapped(int)), this, SLOT(yourslothere(int)));

Note: QSignalMapper is VERY useful, keep that in mind ;)

penguinpower
thanks, +1 for showing how to use QSignalMapper
David McDavidson