Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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:

    #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?


  • Lifetime Qt Champion

    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:

    ss.JPG

    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:

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


  • 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