A checkbox only column in QTableView

淺唱寂寞╮ 提交于 2019-12-01 09:19:04

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!