Qt World Summit: Submit your Presentation

Widget class customisation

  • Hi All,

    I have connected three widgets of the same class (CurrentButtonOne)to three different slots as shown below. Where the signal is getValue

    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 called value:

    #include <QWidget>
    class Timer;
    namespace Ui {
    class CurrentButtonOne;
    class CurrentButtonOne : public QWidget
        explicit CurrentButtonOne(QWidget *parent = nullptr);
    private slots:
        void buttonPressed_high();
        void buttonReleased_high();
        void buttonPressed_moreHigh();
        void buttonReleased_moreHigh();
        void getValue(unsigned int);
        Ui::CurrentButtonOne *ui;
        int value = 0 ;  //---> assign different values based on the slots which is connected

    Here I need to assign different values to value depending on which slot it is connected. Is it something possible?

  • Lifetime Qt Champion


    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 as signal , and connected to three slots.

    previously the starting value was 0. So assigned value as zero:

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

  • Lifetime Qt Champion

    Then you should configure each of these widgets explicitly.

    That will make both your code cleaner and clearer.

Log in to reply