I'm trying to do a program which makes some operations with sounds. My question is that I have 3 Play pushbutton and 3 label. I want that whichever I click on the Play button, the sound whose name is in the label that is near the pushbutton should play. I have a play slot without any parameter. So, how can I connect to every play button with every label respectively? Actually, I can write 3 different play function, but since I have some other functions, it will be too long and confusing. Also, I need 3 play button because I want to play 3 sounds simultaneously.
相关问题
- Sorting 3 numbers without branching [closed]
- QML: Cannot read property 'xxx' of undefin
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
相关文章
- ubuntu20.4中c#通过c++库调用python脚本
- Qt槽函数自动执行多遍
- Class layout in C++: Why are members sometimes ord
- How to mock methods return object with deleted cop
- Which is the best way to multiply a large and spar
- C++ default constructor does not initialize pointe
- Web Test recorder does not allow me to record a te
- Selecting only the first few characters in a strin
You can also assign the three buttons to a
QButtonGroup
and use its signalvoid QButtonGroup::buttonClicked(int id)
, which gives you the id of a pushbutton which was clicked. Just make sure to set the IDs of each button beforehand(for instance in the constructor or some init function). By doing so, you don't have to deal with pointers.https://doc.qt.io/archives/qt-4.8/qbuttongroup.html#buttonClicked-2 https://doc.qt.io/archives/qt-4.8/qbuttongroup.html#setId
For example by connecting the clicked signal from all the buttons to a slot, and then use QObject::sender() to find out which button it was.
Let's assume your buttons are named pushButton, pushButton_2 and pushButton_3 and labels label, label_2 and label_3 respectively.
In modern code, you should use the Qt 5's
connect
syntax and simply connect to a functor when your action is trivial, like yours is: