Signals & Slots Question



  • Hi all,

    I have a simple class like this

    #include <QObject>
    
    class vitem : public QObject
    {
        Q_OBJECT
    public:
        explicit vitem(QObject *parent = 0);
        void setboolvalue(bool value);
        void setintvalue(int ideger);
    private:
        bool b_value = false;
        int i_value = 0;
    
    signals:
        void boolvaluechanged(bool value);
        void intvaluechanged(int ideger);
    public slots:
    
    };
    
    vitem::vitem(QObject *parent) : QObject(parent)
    {
    
    }
    
    void vitem::setboolvalue(bool value)
    {
       if(value != b_value){
           b_value = value;
           emit boolvaluechanged(value);
       }
    }
    
    void vitem::setintvalue(int ideger)
    {
        if(ideger != i_value){
            i_value = ideger;
            emit intvaluechanged(ideger);
        }
    }
    
    

    and I want to connect the boolvaluechanged(bool) signal to a a forms button::setflat true;

    and I do this

    namespace Ui {
    class MainWindow;
    }
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit MainWindow(QWidget *parent = 0);
        ~MainWindow();
    
    private slots:
    
    
        void on_btnDENE_clicked(bool checked);
    
    private:
        Ui::MainWindow *ui;
        vitem *theItem;
    };
    ui->setupUi(this);
        theItem = new vitem;
        connect(theItem,SIGNAL(boolvaluechanged(bool)),this->ui->vlfESTERQUAD_2 ,SLOT(setFlat(bool)));
    
    

    I could not connect vitems boolvaluechanged to forms button?
    I triad many variations but no success, what am I doing wrong ?

    any help??


  • Lifetime Qt Champion

    Hi,

    How do you know it's not connected ?

    When are you changing the value of your vitem object ?



  • sorry..

    it says

    QObject::connect: No such slot QPushButton::setFlat(bool) in ..\Multi_proc_dene\mainwindow.cpp:10
    QObject::connect: (receiver name: 'vlfESTERQUAD_2')

    and

    I put another button and whan clicked

    void MainWindow::on_btnDENE_clicked(bool checked)
    {
    theItem->setboolvalue(checked);
    }



  • @zeroptr
    QPushButton does not have a slot named setFlat, setFlat is a member function of QPushButton

    if you want to use signal and slot to set the flat property of QPushButton, maybe you can use lambda expression



  • Hi @zeroptr
    setFlat is an member fuction of QPushButton. so In your case you could have to do like this

    connect(theItem,vitem::boolvaluechanged(bool),this->ui->vlfESTERQUAD_2 ,QPushButton::setFlat(bool));



  • @Venkatesh-V said in Signals & Slots Question:

    connect(theItem,vitem::boolvaluechanged(bool),this->ui->vlfESTERQUAD_2 ,QPushButton::setFlat(bool));

    E:\VikingUretim\Multi_proc_dene\Multi_proc_dene\mainwindow.cpp:11: error: expected primary-expression before 'bool'
    connect(theItem,vitem::boolvaluechanged(bool),this->ui->vlfESTERQUAD_2 ,QPushButton::setFlat(bool));
    ^

    gives error like this..



  • Hi @zeroptr

    Sorry try this,

    connect(theItem,&vitem::boolvaluechanged,this->ui->vlfESTERQUAD_2 ,&QPushButton::setFlat);

    reply quote 0



  • @Venkatesh-V

    Thank's a lot solved :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.