English 中文(简体)
Fewer connections in a Qt calculator
原标题:

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.

最佳回答

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.

问题回答

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.





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签