Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Use "on_any_clicked()" Slot from a dinamic generated PushButton
QtWS25 Last Chance

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

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 2 Posters 977 Views
  • 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.
  • yczoY Offline
    yczoY Offline
    yczo
    wrote on last edited by yczo
    #1

    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
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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
      1
      • yczoY Offline
        yczoY Offline
        yczo
        wrote on last edited by yczo
        #3

        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
        0
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by mrjj
          #4

          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
          1
          • yczoY Offline
            yczoY Offline
            yczo
            wrote on last edited by
            #5

            Thank you very much, it works!!

            mrjjM 1 Reply Last reply
            1
            • yczoY yczo

              Thank you very much, it works!!

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

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

              1 Reply Last reply
              1

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved