How to pass object reference to slot?



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



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



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.