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 object reference to slot?
Qt 6.11 is out! See what's new in the release blog

How to pass object reference to slot?

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 2 Posters 2.8k 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.
  • V Offline
    V Offline
    Vladislav Lisovenko
    wrote on last edited by
    #1

    I need to pass object reference to slot function, something like this:
    connect(button,SIGNAL(clicked(true)),SLOT(onButtonClicked(MyObject));

    I know that it's wrong way, but I don't know how to manage it. I also know about QSignalMapper, but it works only with primitive types and doesn't work with references.

    Where there's a will, there is a way.

    1 Reply Last reply
    0
    • K Offline
      K Offline
      Kaluss
      wrote on last edited by
      #2

      Could You give some hints what's You want to do? Maybe is a better way to do that :)

      V 1 Reply Last reply
      1
      • K Kaluss

        Could You give some hints what's You want to do? Maybe is a better way to do that :)

        V Offline
        V Offline
        Vladislav Lisovenko
        wrote on last edited by
        #3

        @Kaluss I have a table view and QlineEdit with button in each cell, and I want to pass QModelIndex when button clicked for futher processing.

        Where there's a will, there is a way.

        1 Reply Last reply
        0
        • K Offline
          K Offline
          Kaluss
          wrote on last edited by
          #4

          Hmm, maybe You can use entered(const QModelIndex & index) signal which is emitted always when You enter particular item. You set internal member to this index and in slot onButtonClicked(bool) u use this member.
          What do You think?

          V 1 Reply Last reply
          1
          • K Kaluss

            Hmm, maybe You can use entered(const QModelIndex & index) signal which is emitted always when You enter particular item. You set internal member to this index and in slot onButtonClicked(bool) u use this member.
            What do You think?

            V Offline
            V Offline
            Vladislav Lisovenko
            wrote on last edited by
            #5

            @Kaluss Sorry, but I don't understand about internal member and how to link it with index? I also have another variant to solve this problem - just derive QPushButton and make constructor with QModelIndex argument, but I think it's wrong way, it should be simpler.

            Where there's a will, there is a way.

            1 Reply Last reply
            0
            • K Offline
              K Offline
              Kaluss
              wrote on last edited by
              #6
              WidgetWithTableView
              {
              public:
                  WidgetWithTableView();
              public slots:
                  void onIndexEntered(const QModelIndex & index);
                  void onButtonClicked(bool);
              private:
                  QModelIndex currentIndex;
              };
              ///////////////////////////////////////////////////////
              WidgetWithTableView::WidgetWithTableView()
              {
                   //stuff
                  connect(button,SIGNAL(clicked(bool)),SLOT(onButtonClicked(bool));
                  connect(button,SIGNAL(entered(const QModelIndex & index)),SLOT(onIndexEntered(const QModelIndex & index));
              }
              
              WidgetWithTableView::onIndexEntered(const QModelIndex & index)
              {
                  this->currentIndex = index;
              }
              
              WidgetWithTableView::onButtonClicked(bool)
              {
                  //stuff
                  //if we need index we use  this->currentIndex
              }
              
              

              Is it clear right now?

              V 1 Reply Last reply
              2
              • K Kaluss
                WidgetWithTableView
                {
                public:
                    WidgetWithTableView();
                public slots:
                    void onIndexEntered(const QModelIndex & index);
                    void onButtonClicked(bool);
                private:
                    QModelIndex currentIndex;
                };
                ///////////////////////////////////////////////////////
                WidgetWithTableView::WidgetWithTableView()
                {
                     //stuff
                    connect(button,SIGNAL(clicked(bool)),SLOT(onButtonClicked(bool));
                    connect(button,SIGNAL(entered(const QModelIndex & index)),SLOT(onIndexEntered(const QModelIndex & index));
                }
                
                WidgetWithTableView::onIndexEntered(const QModelIndex & index)
                {
                    this->currentIndex = index;
                }
                
                WidgetWithTableView::onButtonClicked(bool)
                {
                    //stuff
                    //if we need index we use  this->currentIndex
                }
                
                

                Is it clear right now?

                V Offline
                V Offline
                Vladislav Lisovenko
                wrote on last edited by
                #7

                @Kaluss Thanks Kaluss, it's exactly what I need, thank you very much.

                Where there's a will, there is a way.

                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