Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct


    Qt World Summit: Early-Bird Tickets

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

    General and Desktop
    2
    6
    718
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • yczo
      yczo last edited by yczo

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

      1 Reply Last reply Reply Quote 0
      • mrjj
        mrjj Lifetime Qt Champion last edited by

        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.

        1 Reply Last reply Reply Quote 1
        • yczo
          yczo last edited by yczo

          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

          1 Reply Last reply Reply Quote 0
          • mrjj
            mrjj Lifetime Qt Champion last edited by mrjj

            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.

            1 Reply Last reply Reply Quote 1
            • yczo
              yczo last edited by

              Thank you very much, it works!!

              mrjj 1 Reply Last reply Reply Quote 1
              • mrjj
                mrjj Lifetime Qt Champion @yczo last edited by

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

                1 Reply Last reply Reply Quote 1
                • First post
                  Last post