QTableView中的Qt复选框 [英] Qt checkboxes in QTableView

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

问题描述

我正在使用此代码查询sqlite,并将结果放入QTableView中。

I'm using this code to query sqlite and put the results in a QTableView.

//MainWindow.cpp
void MainWindow::on_pushButton_clicked()
{
    QSqlQueryModel * modal=new QSqlQueryModel();
    connOpen();
    QSqlQuery* qry=new QSqlQuery(mydb);

    qry->prepare("select * from database");
    qry->exec();

    modal->setQuery(*qry);

    //from stack
    modal->insertColumn(0);

    ui->tableView->setModel(modal);

    //from stack
    ui->tableView->resizeColumnsToContents();

    int p;
    for(p=0; p<modal->rowCount(); p++)
    {
        ui->tableView->setIndexWidget(modal->index(p,0),new QCheckBox());
    }

    connClose();
    qDebug() <<(modal->rowCount());
}

我已经看到了几个在网络上为复选框添加复选框的示例,但是我不太确定我的简单示例将使用什么。

I've seen several examples of the web for adding checkboxes to a column, but I'm not quite sure what to use for my simple example.


  • 答案提出了一些看起来不标准的行。

  • 还有更多示例,例如,它们似乎概述了我

  • This answer suggests a few lines that doesn't seem standard.
  • There are more examples like this and this one that appear to outline what I need, but it's unclear to where you place the code.

我打算做的是检查第1列。在下一次btn上,如果选中,则将那些数据行写入文件。

What I intend to do is to have column 1 checkable. On next btn press, If checked those rows of data get written to a file.

我仍然需要了解如何循环遍历所选数据,或者可能需要获取选中行的ID并执行另一个查询。

I still need to understand how to loop thru the selected data, or perhaps I need to get the ids of the checked rows and do another query.

问题:


  • 如何向QTableView中添加1列可编辑复选框

  • 如何遍历QTableView数据中的值,以便可以访问选中的行的值?

  • 如何检查所有/没有?

推荐答案

我认为拥有可检查单元格列的最佳方法是创建您的物品模型,例如通过将 QSqlQueryModel 子类化。
必须重新实现flags()方法以使单元格可检查。

I think the best way to have a column of checkable cells is to create your item model, e.g. by subclassing the QSqlQueryModel. You must reimplement the flags() method to make checkable the cells.

此外,您还需要重新实现 data()方法返回检查状态和 setData()方法并设置检查状态。您必须实现自己的逻辑以跟踪每行的检查状态(例如,使用Qt :: CheckState数组,当模型数据更改时必须初始化并调整大小)。

Also you need to reimplement the data() method to return the check state and the setData() method and to set the check state. You must implement your own logic to keep track of the check state of every rows (e.g. using an array of Qt::CheckState that you must initialize and resize when the model data changes).

Yu可以从这样的东西开始:

Yuo can start with something like this:

class MyModel : public QSqlQueryModel
{
public:

    Qt::ItemFlags flags(const QModelIndex & index) const
    {
        if(index.column() == 0)
             return QSqlQueryModel::flags(index) | Qt::ItemIsUserCheckable;
        return QSqlQueryModel::flags(index);
    }

    QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const 
    {
        if(index.column() == 0 && role == Qt::CheckStateRole)
        {
            //implement your logic to return the check state 
            //....
        }
        else
            return QSqlQueryModel::data(index, role);
    }

    bool setData(const QModelIndex & index, const QVariant & value, int role = Qt::EditRole)
    {
        if(index.column() == 0 && role == Qt::CheckStateRole)
        {
            //implement your logic to set the check state 
            //....
        }
        else
            QSqlQueryModel::setData(index, value, role);
    }
};

硒还:

  • Model Subclassing
  • QAbstractItemModel documentation

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

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