Important: Please read the Qt Code of Conduct -

How to pass object reference to slot?

  • I need to pass object reference to slot function, something like this:

    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.

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

  • @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.

  • 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?

  • @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.

  • WidgetWithTableView
    public slots:
        void onIndexEntered(const QModelIndex & index);
        void onButtonClicked(bool);
        QModelIndex currentIndex;
        connect(button,SIGNAL(entered(const QModelIndex & index)),SLOT(onIndexEntered(const QModelIndex & index));
    WidgetWithTableView::onIndexEntered(const QModelIndex & index)
        this->currentIndex = index;
        //if we need index we use  this->currentIndex

    Is it clear right now?

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

Log in to reply