Solved Widget class customisation
-
Hi All,
I have connected three widgets of the same class (
CurrentButtonOne
)to three differentslots
as shown below. Where thesignal
isgetValue
connect(ui->widget_currentOne, &CurrentButtonOne::getValue, this, &stageProgram::setCurrOnChannelOne); connect(ui->widget_currentTwo, &CurrentButtonOne::getValue, this, &stageProgram::setCurrOnChannelTwo); connect(ui->widget_currentThree, &CurrentButtonOne::getValue, this, &stageProgram::setCurrOnChannelThree);
where in the
CurrentButtonOne
class , have a parameter calledvalue
:#ifndef CURRENTBUTTONONE_H #define CURRENTBUTTONONE_H #include <QWidget> class Timer; namespace Ui { class CurrentButtonOne; } class CurrentButtonOne : public QWidget { Q_OBJECT public: explicit CurrentButtonOne(QWidget *parent = nullptr); ~CurrentButtonOne(); private slots: void buttonPressed_high(); void buttonReleased_high(); void buttonPressed_moreHigh(); void buttonReleased_moreHigh(); signals: void getValue(unsigned int); private: Ui::CurrentButtonOne *ui; int value = 0 ; //---> assign different values based on the slots which is connected }; #endif // CURRENTBUTTONONE_H
Here I need to assign different values to
value
depending on which slot it is connected. Is it something possible? -
Hi,
No, and it raises a big question: why shall that value be different base on a slot ?
In normal operation, the object that emits a signal does not know nor should it care about what is connected to it. Sounds like you are trying to make that widget know way to much about what it interacts with.
Can you explain your use case ?
-
@SGaist Hi
Use case is as follows:
My widget (of class
CurrentButtonOne
) is inc/dec button looks like this, I've three of them:This widget is initially built to increment/decrement
value
send this assignal
, and connected to threeslots
.previously the starting value was
0
. So assignedvalue
as zero:private: Ui::CurrentButtonOne *ui; int value = 0 ;
But now I've got a situation in which the starting value can be different for each of the widgets and need to increment/decrement from that starting value (not zero).
-
Then you should configure each of these widgets explicitly.
That will make both your code cleaner and clearer.