QPushButton in a QTableView



  • Hello,

    i want to put a QPushButton in each cell of the last column of my QTableView.
    I saw that we can't set a QWidget but we can use the delegate reimplementing the createEditor() method, but i would like to know if it's possible to display the QPushButton all the time, i.e not only when the user double-click on the cell.

    --Octani--



  • Hi,

    there are different options.

    use the edit triggers to open the editor also on other events then double click

    overwrite the delegates paint event

    use "setIndexWidget":http://qt-project.org/doc/qt-4.8/qabstractitemview.html#setIndexWidget



  • Ok thanks you.
    So I try to use the setIndexWidget() which works perfectly, the problem is I don't understand how to retrieve the line where the button is clicked. Because from the QPushButton it's not possible.

    So I use the void QAbstractItemView::clicked ( const QModelIndex & index ) [signal] method but it doesn't work. The slot is not called.



  • You should use a QSignalMapper object for that.



  • Hi,

    You need to create a slot and then connect that slot to the tableView . eg

    @connect(tableView,SLOT(clicked(QModelIndex)),this,SLOT(on_tableView_clicked(QModelIndex));@

    If you are using Qt designer then just right click on your tableView ---Go to Slot--clicked(QModelIndex)

    then in the function you can filter the row and column from the index

    @void MainWindow::on_tableView_clicked(const QModelIndex &index)
    {
    qDebug() << index.row() << " " << index.column();
    }@

    but while running the application if you Left Click on the pushbutton this slot is not called but if you press Right Click then this slot is called and it prints the row and column this is what i am getting.



  • Exactly, I didn't see that.
    And there's no way to emit this signal after a Left Click on the pushbutton? Because users will never find they must right click.

    edit : I never used QSignalMapper, how does this tool can help me?



  • You can connect a signal of an object with the signal mapper and send a signal from the mapper together with an ID. It's done exactly for those purposes.

    Have a look "at the docs":http://qt-project.org/doc/qt-4.8/qsignalmapper.html#details . you could use the setMapping(QObject*, int) part.



  • you could also use a custom delegate to create a button in the desired cell (like you suggested in the initial post). Then, in order to show them permanently, call "openPersistentEditor":http://qt-project.org/doc/qt-4.8/qabstractitemview.html#openPersistentEditor

    to find out which row or column the button is in, simply store them in the properties of the button when creating it.



  • Do you have an example of overloading the delegates paint event?


  • Lifetime Qt Champion



  • Hi SGaist,

    Thank you. I also added the tag for solved.


Log in to reply
 

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