A checkbox only column in QTableView

前端 未结 2 966
天命终不由人
天命终不由人 2021-01-14 03:34

I have a table in Sqlite database which I display using QTableview and QSqlQueryModel. The first column needs to have a header which is a checkbox and all the items in the c

相关标签:
2条回答
  • 2021-01-14 04:14

    The easiest way for displaying checkable items is using QStandardItemModel as QStandardItem can be set checkable. It is good for prototyping. The drawback however is, you had to fill the model manually.

    0 讨论(0)
  • 2021-01-14 04:15

    I have found this solution that does not uses delegates or anything like that. You will still have a problem centering the checkbox. That is up to you.

    The next code snippet will make a column filled with checkboxes:

    yourSqlQueryModel = new QSqlQueryModel();
    yourTableView = new QtableView();
            yourSqlQueryModel ->setQuery(yourQuery);
            yourSqlQueryModel ->insertColumn(0);//Insert column for checkboxes
            ui->yourTableView ->setModel(yourSqlQueryModel );
            ui->yourTableView ->resizeColumnsToContents();
    
            int p;
            for(p = 0;p<yourSqlQueryModel ->rowCount();p++)
            {
                ui->yourTableView ->setIndexWidget(yourSqlQueryModel ->index(p,0),new QCheckBox());
            }
    

    Please read carefully, the most important here is the setIndexWidget method, which allows you to insert the widget into the created column.

    0 讨论(0)
提交回复
热议问题