How to add QPushButton in QTableView



  • Dear All,
    I am able to add a QStyleOptionButton using delegate inside the QTableView. But i want to add a normal QPushButton because QPushButton can have stylesheets. Kindly let me know how to add QPushButton in QTableView. Thanks



  • Hi @Sidii,

    You can use setIndexWidget(), but this will slow down the loading of tableView if there are too many rows. In such case one approach is to render a pushButton in the paint of delegate and use openPersistentEditor() / closePersistentEditor() when entering/leaving a cell.

    Regards
    Sam



  • this is a part of my old project. a table view that has delete button for every row :

    int i = 0;
    QPushButton *delButton;
    QStandardItemModel *model;
    void MainWindow::CreateRowBut()
    {
        model->appendRow(new QStandardItem(QString("")));
        delButton = new QPushButton();
        delButton->setText("Delete " + QString::number(i));
        ui->tableView->setIndexWidget(model->index(i , 7), delButton);
        connect(delButton , SIGNAL(clicked()) , this , SLOT(delete()));
        i++;
    }
    

    and the result will be :

    alt text

    still It's kind of impossible to doing this for deleting rows. I struggled with it 2 o 3 days and didn't work out! still if you need it for something else I hop it would work.



  • I would recommend to have delete button outside table and delete selected rows instead of having delete button per row.

    As for implementation if you really want to do anything including deleting with specific row it is simple.
    Part of the code you can see posted above by Hamed.

    Problem he is facing is related to the fact that you can't know from the slot called from button click which row (or rather cell index ) caller button belongs. The only way to do it is to iterate through every cell you could set cell widget to and compare it to caller pointer.

    Using delegates to display widgets would allow to avoid such overload.
    .



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