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. Passing slot as parameter in function to connect to a signal

Passing slot as parameter in function to connect to a signal

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 423 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.
  • J Offline
    J Offline
    JonexElectronic
    wrote on 21 Oct 2021, 10:45 last edited by JonexElectronic
    #1

    Hi,
    I'm trying to connect a signal to a slot that I have received as const char* in my function but I'm doing something wrong.
    This is the function that create a QPushButton and connect the signal click to my slot.

    void addPushButtonInTableCell(U2bYTES row, U2bYTES column, U2bYTES numberToPushButton, QTableWidget * pTableWidget , QWidget* parent, QString ruta, const char* slot)
    {
    	QWidget *pWidget = new QPushButton(QString::number(numberToPushButton),parent);	
    ...
    	QObject::connect(pWidget, SIGNAL(clicked()),parent, SLOT(slot));
    }
    

    them I invoke the function.

    addPushButtonInTableCell(0,15,pQ->u.cue.n, tableWidget , pMW->options, ":/ui/recursos/ExtrasInsert.png","on_pushButton_newJump_clicked()");
    

    It compile and give to me next warning message

    warning: unused parameter ‘slot’ [-Wunused-parameter]
     void addPushButtonInTableCell(U2bYTES row, U2bYTES column, U2bYTES numberToPushButton, QTableWidget * pTableWidget , QWidget* parent, QString ruta, const char* slot)
                                                                                                                                                                     ^
    

    when I execute it i received this message on qdebug

    QObject::connect: Parentheses expected, slot optionstouch::slot in ../motor/lib.cpp:26206
    QObject::connect:  (receiver name: 'optionstouch')
    
    J 1 Reply Last reply 21 Oct 2021, 11:04
    0
    • J JonexElectronic
      21 Oct 2021, 10:45

      Hi,
      I'm trying to connect a signal to a slot that I have received as const char* in my function but I'm doing something wrong.
      This is the function that create a QPushButton and connect the signal click to my slot.

      void addPushButtonInTableCell(U2bYTES row, U2bYTES column, U2bYTES numberToPushButton, QTableWidget * pTableWidget , QWidget* parent, QString ruta, const char* slot)
      {
      	QWidget *pWidget = new QPushButton(QString::number(numberToPushButton),parent);	
      ...
      	QObject::connect(pWidget, SIGNAL(clicked()),parent, SLOT(slot));
      }
      

      them I invoke the function.

      addPushButtonInTableCell(0,15,pQ->u.cue.n, tableWidget , pMW->options, ":/ui/recursos/ExtrasInsert.png","on_pushButton_newJump_clicked()");
      

      It compile and give to me next warning message

      warning: unused parameter ‘slot’ [-Wunused-parameter]
       void addPushButtonInTableCell(U2bYTES row, U2bYTES column, U2bYTES numberToPushButton, QTableWidget * pTableWidget , QWidget* parent, QString ruta, const char* slot)
                                                                                                                                                                       ^
      

      when I execute it i received this message on qdebug

      QObject::connect: Parentheses expected, slot optionstouch::slot in ../motor/lib.cpp:26206
      QObject::connect:  (receiver name: 'optionstouch')
      
      J Offline
      J Offline
      J.Hilk
      Moderators
      wrote on 21 Oct 2021, 11:04 last edited by J.Hilk
      #2

      @JonexElectronic I'm hesitant to help, because your approach smells like a fundamental problem in the setup of what you want to do.

      anyway, your slot function argument is already a const char*, so no need to make it one, with the SLOT macro.

      -> QObject::connect(pWidget, SIGNAL(clicked()),parent,slot);


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      R 1 Reply Last reply 21 Oct 2021, 11:07
      0
      • J J.Hilk
        21 Oct 2021, 11:04

        @JonexElectronic I'm hesitant to help, because your approach smells like a fundamental problem in the setup of what you want to do.

        anyway, your slot function argument is already a const char*, so no need to make it one, with the SLOT macro.

        -> QObject::connect(pWidget, SIGNAL(clicked()),parent,slot);

        R Offline
        R Offline
        raven-worx
        Moderators
        wrote on 21 Oct 2021, 11:07 last edited by
        #3

        @J-Hilk said in Passing slot as parameter in function to connect to a signal:

        so no need to make it one, with the SLOT macro

        the SLOT macro does a little bit more than just casting it to a const char*

        but i agree, connecting a slot on a signal invocation doesnt seem right

        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
        If you have a question please use the forum so others can benefit from the solution in the future

        J J 2 Replies Last reply 21 Oct 2021, 11:15
        0
        • R raven-worx
          21 Oct 2021, 11:07

          @J-Hilk said in Passing slot as parameter in function to connect to a signal:

          so no need to make it one, with the SLOT macro

          the SLOT macro does a little bit more than just casting it to a const char*

          but i agree, connecting a slot on a signal invocation doesnt seem right

          J Offline
          J Offline
          JonexElectronic
          wrote on 21 Oct 2021, 11:15 last edited by
          #4

          @J-Hilk @raven-worx If I do not use SLOT I received the next qDebug message.

          QObject::connect: Use the SLOT or SIGNAL macro to connect optionstouch::on_pushButton_newJump_clicked()
          

          I do not understand what do you mean with "because your approach smells like a fundamental problem in the setup of what you want to do."
          I just create a QPushButton and insert it in QTableWidget and I want to execute the funtion that I passed in my slot when the button is clicked.
          how should I do that? Is there a better way to do it?

          1 Reply Last reply
          0
          • R raven-worx
            21 Oct 2021, 11:07

            @J-Hilk said in Passing slot as parameter in function to connect to a signal:

            so no need to make it one, with the SLOT macro

            the SLOT macro does a little bit more than just casting it to a const char*

            but i agree, connecting a slot on a signal invocation doesnt seem right

            J Offline
            J Offline
            J.Hilk
            Moderators
            wrote on 21 Oct 2021, 11:31 last edited by
            #5

            @raven-worx said in Passing slot as parameter in function to connect to a signal:

            the SLOT macro does a little bit more than just casting it to a const char*

            really? 🤔 Probably moc, when its running, right ?

            I'm based mine purely on this:
            1c897f05-0ccd-421f-8663-29dd7ea88fcc-image.png

            Is there a better way to do it?

            probably, but to be honest, I'm unsure, what exactly you want to do:
            I want to execute the function that I passed in my slot when the button is clicked doesn't make much sense to me


            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


            Q: What's that?
            A: It's blue light.
            Q: What does it do?
            A: It turns blue.

            R 1 Reply Last reply 21 Oct 2021, 11:35
            0
            • J J.Hilk
              21 Oct 2021, 11:31

              @raven-worx said in Passing slot as parameter in function to connect to a signal:

              the SLOT macro does a little bit more than just casting it to a const char*

              really? 🤔 Probably moc, when its running, right ?

              I'm based mine purely on this:
              1c897f05-0ccd-421f-8663-29dd7ea88fcc-image.png

              Is there a better way to do it?

              probably, but to be honest, I'm unsure, what exactly you want to do:
              I want to execute the function that I passed in my slot when the button is clicked doesn't make much sense to me

              R Offline
              R Offline
              raven-worx
              Moderators
              wrote on 21 Oct 2021, 11:35 last edited by
              #6

              @J-Hilk said in Passing slot as parameter in function to connect to a signal:

              I'm based mine purely on this:

              exactly, and it adds "1" as a prefix. which is then used by the meta-object instance for lookup of the method and check if its really a slot.

              --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
              If you have a question please use the forum so others can benefit from the solution in the future

              1 Reply Last reply
              1
              • J Offline
                J Offline
                JonexElectronic
                wrote on 21 Oct 2021, 12:24 last edited by
                #7

                I have decided to change my funtion.

                QWidget* addPushButtonInTableCell(U2bYTES row, U2bYTES column, U2bYTES numberToPushButton, QTableWidget * pTableWidget , QWidget* parent, QString ruta, const char* slot)
                {
                	QWidget *pWidget = new QPushButton(QString::number(numberToPushButton),parent);	
                ...
                	return pWidget;
                }
                

                Invoke

                QWidget* pWidget = addPushButtonInTableCell(0,15,pQ->u.cue.n, tableWidget , pMW->options, ":/ui/recursos/ExtrasInsert.png");
                	QObject::connect(pWidget,SIGNAL(clicked()), pMW->options, SLOT(on_pushButton_newJump_clicked()));
                

                It works fine.

                1 Reply Last reply
                0

                6/7

                21 Oct 2021, 11:35

                • Login

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