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. How to pass parameters to a SLOT function?
Forum Updated to NodeBB v4.3 + New Features

How to pass parameters to a SLOT function?

Scheduled Pinned Locked Moved General and Desktop
15 Posts 3 Posters 58.1k Views 2 Watching
  • 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.
  • sierdzioS Offline
    sierdzioS Offline
    sierdzio
    Moderators
    wrote on last edited by
    #2

    @
    connect(buttonOne, SIGNAL(clicked()), this, SLOT(doSomething(double *)));
    @

    This should work. But clicked() will not pass any data to your slot, so the pointer will be dangling. Plus you probably put the connect statement in a wrong place: it should be shown earlier, not on button click (but that depends on your design. Typically, connect statements are put in base widget constructors).

    (Z(:^

    mit_cruzeM 1 Reply Last reply
    0
    • A Offline
      A Offline
      ArchGabe
      wrote on last edited by
      #3

      Hello! Thank you for your reply!

      Actually, I am coding a button that creates another buttons and the same function is set to each of these buttons.

      I didn't understand what you mean, sorry! Can you please explain in more details?

      Thank you again,

      1 Reply Last reply
      0
      • sierdzioS Offline
        sierdzioS Offline
        sierdzio
        Moderators
        wrote on last edited by
        #4

        Always check "the docs":http://qt-project.org/doc/qt-4.8/signalsandslots.html first.

        Ok, so what I meant was this:
        When you have a signal-slot connection, and the signal is emitted, it will "carry" the argument it's invoked with (well, so to speak). For example, QAbstractButton has a signal named toggled(bool) - it will always be sent together with this boolean information. If your corresponding slot has a use for it (example: mySlot(bool isToggled)) this value will be passed to it.

        In your example, however, the clicked() signal does not come with any parameter - so it will not pass any data to doSomething(double *pointer) - and in truth, MOC will not allow such a connection (again - see the documentation, it's all explained there).

        (Z(:^

        1 Reply Last reply
        0
        • A Offline
          A Offline
          ArchGabe
          wrote on last edited by
          #5

          Actually, I just did like this:
          @
          signals:
          void clicked(QPushButton*);
          public slots:
          void doSomething(QPushButton*);
          @
          .
          @
          connect(buttonOne, SIGNAL(clicked(QPushButton*)), this, SLOT(doSomething(buttonTwo)));
          @
          .
          But still getting the same error message.

          1 Reply Last reply
          0
          • sierdzioS Offline
            sierdzioS Offline
            sierdzio
            Moderators
            wrote on last edited by
            #6

            Does the buttonOne contain your custom signal? Have you rerun moc (do a full rebuild of your project if in doubt. That is: clean, run qmake, build)?

            (Z(:^

            1 Reply Last reply
            0
            • A Offline
              A Offline
              ArchGabe
              wrote on last edited by
              #7

              Oops! Sorry!

              buttonOne and buttonTwo are the same! Sorry! Please, read:

              @
              connect(buttonOne, SIGNAL(clicked(QPushButton*)), this, SLOT(doSomething(buttonOne)));
              @

              The idea is, when the button is clicked, it will change its icon.

              1 Reply Last reply
              0
              • sierdzioS Offline
                sierdzioS Offline
                sierdzio
                Moderators
                wrote on last edited by
                #8

                You are doing it wrong. Please, read the documentation first, I've already linked you to it.

                (Z(:^

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  ArchGabe
                  wrote on last edited by
                  #9

                  Sorry, but I guess that I am doing what is written in the documentation. If I am doing it wrong I guess it means that I didn't really understand what is in the documentation... So if someone here doens't mind, please, provide another exemple.

                  1 Reply Last reply
                  0
                  • sierdzioS Offline
                    sierdzioS Offline
                    sierdzio
                    Moderators
                    wrote on last edited by
                    #10

                    You cannot control what will be sent through the signal in the connect statement - only the sender itself uses "emit" keyword (well, can use it) and provides concrete argument (QPushButton pointer in your case).

                    Also, connect cannot control the receiving end either, so you can't put a concrete object there (buttonOne in SLOT is wrong). Connect statement acts only as a connecting cord, it has no influence on the information apart from providing QObject->sender() and - optionally - queuing the connection.

                    And since I don't know where you declare your signal, where does "this" point to in your code, I am mostly guessing your setup anyway.

                    (Z(:^

                    1 Reply Last reply
                    0
                    • A Offline
                      A Offline
                      ArchGabe
                      wrote on last edited by
                      #11

                      Hello! Thank you for your reply.

                      I’ve found this post, that I guess “answer” part of my problem:

                      http://www.qtcentre.org/archive/index.php/t-33960.html

                      But now, what I need is connect an array of buttons, each one of them is created by the user (meaning that they are stored in a list – or QList).

                      I created an update function as follow:
                      @
                      void MainWindow::update(){
                      signalMapper = new QSignalMapper(this);

                          for (int ii = 0; ii < btnList.size(); ii++){
                              connect(btnList[ii].returnButton(), SIGNAL(clicked()), signalMapper, SLOT(map()));
                              signalMapper->setMapping(btnList[ii].returnButton(), id);
                              id++;
                          }
                       
                      }
                      

                      @
                      Where btnList is a linked list containing all the buttons created by the GUI user, and returnButton() returns QPushButton*.

                      But it is still not working… Do you have any idea?

                      1 Reply Last reply
                      0
                      • A Offline
                        A Offline
                        ArchGabe
                        wrote on last edited by
                        #12

                        Also, I created a class AddButton, in order to encapsulate all the info that I need for my code.

                        @
                        class AddButton
                        {
                        public:
                        AddButton();
                        AddButton(QString, QIcon);
                        QPushButton* returnButton(void);
                        QIcon returnIcon(void) {return *icon;}
                        void setGeometry();
                        void setID(int id1) {this->id = id1;}
                        QString returnName() {return this->btnName;}
                        int returnID() {return this->id;}
                        ~AddButton() {delete newButton; delete icon;}

                        protected:
                        QPushButton* newButton;
                        QIcon *icon;
                        QString btnName;
                        int id;
                        };
                        @

                        And the "New Button" code is:

                        @
                        void MainWindow::on_btnNew_clicked()
                        {
                        QIcon *icon = new QIcon("/home/gabe/GGW.jpg");
                        AddButton *newButton = new AddButton(QString("Teste"), *icon);

                        newButton->setGeometry();
                        ui->layoutGrid->addWidget(newButton->returnButton(), returnRow(), returnCol());
                        increment();
                        
                        btnList.push_back(*newButton);
                        update();
                        QString qName = newButton->returnName();
                        connect(signalMapper, SIGNAL(mapped(QString)), this, SLOT(displayImg(qName)));
                        

                        }
                        @

                        1 Reply Last reply
                        0
                        • sierdzioS sierdzio

                          @
                          connect(buttonOne, SIGNAL(clicked()), this, SLOT(doSomething(double *)));
                          @

                          This should work. But clicked() will not pass any data to your slot, so the pointer will be dangling. Plus you probably put the connect statement in a wrong place: it should be shown earlier, not on button click (but that depends on your design. Typically, connect statements are put in base widget constructors).

                          mit_cruzeM Offline
                          mit_cruzeM Offline
                          mit_cruze
                          wrote on last edited by
                          #13

                          @sierdzio

                          hi I am having same problem.

                          for(int i=0;i<2;i++)
                          {
                          for(int j=0;j<2,j++)
                          {
                                      snapshot_action[k]=new QAction(this);
                                      snapshot_action[k]->setIconText("Snapshot");
                                      control_menu[k]->addAction(snapshot_action[k]);
                                      connect(snapshot_action[k], &QAction::triggered, this, &SimplePlayer::snap_fun);
                          }
                          }
                          

                          With initially k=0;
                          I have omitted code which lays out four views each with snapshot action. ( for simplicity)

                          Now the problem is when I press snapshot action button of any view I should know somehow that snapshot action button of THAT particular window is pressed.

                          How can I ?
                          How would I pass value of to my snap function?
                          Do I need to store values of 'k' in some array? (some bad idea)

                          sierdzioS 1 Reply Last reply
                          0
                          • mit_cruzeM mit_cruze

                            @sierdzio

                            hi I am having same problem.

                            for(int i=0;i<2;i++)
                            {
                            for(int j=0;j<2,j++)
                            {
                                        snapshot_action[k]=new QAction(this);
                                        snapshot_action[k]->setIconText("Snapshot");
                                        control_menu[k]->addAction(snapshot_action[k]);
                                        connect(snapshot_action[k], &QAction::triggered, this, &SimplePlayer::snap_fun);
                            }
                            }
                            

                            With initially k=0;
                            I have omitted code which lays out four views each with snapshot action. ( for simplicity)

                            Now the problem is when I press snapshot action button of any view I should know somehow that snapshot action button of THAT particular window is pressed.

                            How can I ?
                            How would I pass value of to my snap function?
                            Do I need to store values of 'k' in some array? (some bad idea)

                            sierdzioS Offline
                            sierdzioS Offline
                            sierdzio
                            Moderators
                            wrote on last edited by
                            #14

                            @mit_cruze said in How to pass parameters to a SLOT function?:

                            How would I pass value of to my snap function?

                            Use QObject::sender() in the slot.

                            (Z(:^

                            mit_cruzeM 1 Reply Last reply
                            2
                            • sierdzioS sierdzio

                              @mit_cruze said in How to pass parameters to a SLOT function?:

                              How would I pass value of to my snap function?

                              Use QObject::sender() in the slot.

                              mit_cruzeM Offline
                              mit_cruzeM Offline
                              mit_cruze
                              wrote on last edited by
                              #15

                              @sierdzio thanks

                              1 Reply Last reply
                              0

                              • Login

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