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 add QPushButton in QTableView
QtWS25 Last Chance

How to add QPushButton in QTableView

Scheduled Pinned Locked Moved General and Desktop
qpushbuttonqtableviewqstyleoptionbutstylesheet
5 Posts 5 Posters 20.1k 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.
  • S Offline
    S Offline
    Sidii
    wrote on 21 Apr 2015, 02:21 last edited by
    #1

    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

    1 Reply Last reply
    0
    • S Offline
      S Offline
      Sam
      wrote on 21 Apr 2015, 08:44 last edited by Sam
      #2

      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

      1 Reply Last reply
      0
      • H Offline
        H Offline
        Hamed
        wrote on 21 Apr 2015, 12:26 last edited by Hamed
        #3

        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.

        HamedBabaeyan@yahoo.com
        for more interesting stuff

        C 1 Reply Last reply 6 Sept 2017, 12:55
        1
        • A Offline
          A Offline
          alex_malyu
          wrote on 22 Apr 2015, 02:15 last edited by
          #4

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

          1 Reply Last reply
          0
          • H Hamed
            21 Apr 2015, 12:26

            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.

            C Offline
            C Offline
            Christian Spielberger
            wrote on 6 Sept 2017, 12:55 last edited by Christian Spielberger 9 Jun 2017, 14:20
            #5

            @Hamed I had the same question and found this old thread. After thinking a while about this I had this idea:
            Do it in an object oriented way! Define a sub-class of QPushButton which contains all the information you need to delete what you want. Add an "action" slot to your sub-class, connect this slot with the clicked() signal and do the delete in this
            slot. Then instead of adding a QPushButton to the model add an object of your sub-class to the model!

            1 Reply Last reply
            1

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved