Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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 ;)

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top