QTableView 中的仅复选框列 [英] A checkbox only column in QTableView

查看:27
本文介绍了QTableView 中的仅复选框列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Sqlite 数据库中有一个表,我使用 QTableview 和 QSqlQueryModel 显示它.第一列需要有一个作为复选框的标题,并且该列中的所有项目也需要是复选框.我已经将第一列标题实现为复选框,并且效果很好.

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 column need to be checkboxes too. I have implemented the first column header as a checkbox and it works perfectly.

由于列中的复选框需要居中,所以我使用了一个委托来绘制它.我使用以下代码绘制了复选框,但无法选中或取消选中它们.我不知道如何实现.

Since the checkboxes in the column need to be centered, I used a delegate to paint it. I have painted checkboxes using the following code, but they cannot be checked or unchecked. I do not know how to implement that.

static QRect CheckBoxRect(const QStyleOptionViewItem &view_item_style_options) {
   QStyleOptionButton check_box_style_option;
   QRect check_box_rect = QApplication::style()->subElementRect(
   QStyle::SE_CheckBoxIndicator,
   &check_box_style_option);
   QPoint check_box_point(view_item_style_options.rect.x() +
                     view_item_style_options.rect.width() / 2 -
                     check_box_rect.width() / 2,
                     view_item_style_options.rect.y() +
                     view_item_style_options.rect.height() / 2 -
                     check_box_rect.height() / 2);
   return QRect(check_box_point, check_box_rect.size());
}


CheckBoxDelegate::CheckBoxDelegate(QObject *parent) :
QStyledItemDelegate(parent)
{

}

void CheckBoxDelegate::paint(QPainter *painter,
                         const QStyleOptionViewItem &option,
                         const QModelIndex &index) const {
  bool checked = index.model()->data(index, Qt::DisplayRole).toBool();

  QStyleOptionButton check_box_style_option;
  check_box_style_option.state |= QStyle::State_Enabled;
  if (checked) {
    check_box_style_option.state |= QStyle::State_On;
  } else {
    check_box_style_option.state |= QStyle::State_Off;
  }
  check_box_style_option.rect = CheckBoxRect(option);

  QApplication::style()->drawControl(QStyle::CE_CheckBox,
                                 &check_box_style_option,
                                 painter);
}

以下代码显示了我如何使用 QTableView 的 QSqlQueryModel 从数据库中加载表.

The following code shows how I use the QSqlQueryModel for QTableView to load the table from the database.

//Load the tableview with the database table
QSqlQueryModel model = new QSqlQueryModel();

//Initializaton of the query
QSqlQuery *query = new QSqlQuery(dbm->db);

query->prepare("SELECT * FROM UserData");

if(query->exec())
{
    model->setQuery(*query);
    ui->tableView->setModel(model);

    //The header delegate to paint a checkbox on the header
    HeaderDelegate *myHeader = new HeaderDelegate(Qt::Horizontal, ui->tableView);
    ui->tableView->setHorizontalHeader(myHeader);

    int RowCount = model->rowCount();

    qDebug() << RowCount;

    CheckBoxDelegate *myCheckBoxDelegate = new CheckBoxDelegate();

    ui->tableView->setItemDelegateForColumn(0,myCheckBoxDelegate);

    ui->tableView->horizontalHeader()->setClickable(true);

    ui->tableView->setSortingEnabled(true);
}

你能告诉我怎么做吗?任何帮助表示赞赏.

Could you please tell me how to go about it? Any help is appreciated.

推荐答案

我发现这个解决方案不使用委托或类似的东西.您仍然会遇到使复选框居中的问题.这取决于你.

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());
        }

请仔细阅读,这里最重要的是 setIndexWidget 方法,它允许您将小部件插入到创建的列中.

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

这篇关于QTableView 中的仅复选框列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆