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

查看:484
本文介绍了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);
}

以下代码显示了如何使用QSqlQueryModel为QTableView加载表数据库。

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天全站免登陆