Important: Please read the Qt Code of Conduct -

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";

  • Lifetime Qt Champion

    you just need to connect it

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

    Ps, dont call it on_xx
    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);
    qDebug() << "can connect: " << connect(askB,SIGNAL(clicked()),this,SLOT(askB_clicked()));

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

    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

  • Lifetime Qt Champion

    you must add Q_OBJECT to your class to use signals

    class getmsg : public QWidget

    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!!

  • Lifetime Qt Champion

    np. its easy to forget to add

Log in to reply