Use "on_any_clicked()" Slot from a dinamic generated PushButton



  • Hello, I have generated a pushButton dinamically into an object
    //*****************************************************
    getmsg::getmsg(QWidget *parent) : QWidget(parent) {
    QPushButton *askB = new QPushButton;
    }

    The Question is, how can implement, the on_---_clicked() slot?
    I have tried with the code below, but it does not work.

    Thanks in advance

    //*****************************************************
    void getmsg::on_askB_clicked(){
    qDebug() << "Clicked";
    }


  • Qt Champions 2016

    Hi
    you just need to connect it

    qDebug() << "can connect:" << connect(askB, SLOT(clicked()), this,SLOT(on_askB_clicked));

    Ps, dont call it on_xx
    on_askB_clicked
    it will trigger the auto connect
    feature but it dont work since u add button later and
    it might show warnings.

    so rename to askB_clicked() if u get warning.



  • Hello and thank you.

    (The code is inside an Object) I tried

    //********************************************
    getmsg::getmsg(QWidget *parent) : QWidget(parent)
    {
    askB = new QPushButton(this);
    askB->resize(wAskB,hLine+2);
    askB->setText("Read");
    qDebug() << "can connect: " << connect(askB,SIGNAL(clicked()),this,SLOT(askB_clicked()));
    }

    //****************************************************
    void getmsg::askB_clicked(){
    qDebug() << "clicked";
    }
    //************************************************
    class getmsg : public QWidget
    {
    public:
    // ampel *circle;
    getmsg(QWidget *parent = 0);

    private:
    QPushButton *askB;

    public slots:
    void askB_clicked();

    };
    but it does not work :-(

    The console say:
    QObject::connect: No such slot QWidget::askB_clicked() in ..\newGui\getmsg.cpp:48
    can connect: false


  • Qt Champions 2016

    you must add Q_OBJECT to your class to use signals

    class getmsg : public QWidget
    {
    Q_OBJECT

    and after
    you must run qmake
    make sure its run else it will never work.
    if it still says "No such slot "
    clean your build folder completely.



  • Thank you very much, it works!!


  • Qt Champions 2016

    @yczo
    np. its easy to forget to add
    Q_OBJECT
    :)


Log in to reply
 

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