qt - QTableView 中只有复选框的列

标签 qt checkbox delegates qtableview model-view

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

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

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 从数据库中加载表格。

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

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

最佳答案

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

下一个代码片段将使一列充满复选框:

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 方法,它允许您将小部件插入到创建的列中。

关于qt - QTableView 中只有复选框的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21409457/

相关文章:

ios - iBeacon:didRangeBeacons 停止被调用,必须重置设备才能再次工作

qt - 如何将文件内容读入 qmake 变量并将其传递给编译器?

Qt multipart post问题

php - 如果复选框被选中则回显已选中

javascript - CSS/JS Checked 复选框应该是 "line out"待办事项列表应用程序中的 <li> 元素

C# 编译器与委托(delegate)构造函数的奇怪之处

c++ - 在 C++ 中卸载 USB 驱动器

c++ - 为什么这个程序(有时)会失败?

ms-access - ms access - 基于文本框值的复选框

android - 委托(delegate)触摸事件