如何与复选框操作进行交互? (带QStandardItemModel的QTableView)

时间:2010-05-07 00:27:57

标签: c++ qt qt4 checkbox qtableview

我正在使用QTableView和QStandardItemModel来显示一些数据。

对于每一行,都有一个带有复选框的列,此复选框由setItem插入,代码如下:

int rowNum;
QStandardItemModel *tableModel;
QStandardItem* __tmpItem = new QStandardItem();

__tmpItem->setCheckable(true);
__tmpItem->setCheckState(Qt::Unchecked);

tableModel->setItem(rowNum,0,__tmpItem);

现在我想与复选框进行交互。如果复选框由用户更改其状态(从已选中更改为未选中,反之亦然),我想在相应的数据行上执行某些操作。

我知道我可以使用信号槽来捕捉复选框的变化,但由于有很多数据行,我不想逐一连接每一行。

有没有更有效地与支票行动互动?谢谢:))

4 个答案:

答案 0 :(得分:4)

我没有处理QTableView + QStandardItemModel,但下面的示例可以帮助您:

1)。 table.h文件:

#ifndef TABLE__H
#define TABLE__H

#include <QtGui>

class ItemDelegate : public QItemDelegate
{
public:
    ItemDelegate(QObject *parent = 0)
        : QItemDelegate(parent)
    {
    }
    virtual void drawCheck(QPainter *painter, const QStyleOptionViewItem &option,
        const QRect &, Qt::CheckState state) const
    {
        const int textMargin = QApplication::style()->pixelMetric(QStyle::PM_FocusFrameHMargin) + 1;

        QRect checkRect = QStyle::alignedRect(option.direction, Qt::AlignCenter,
            check(option, option.rect, Qt::Checked).size(),
            QRect(option.rect.x() + textMargin, option.rect.y(),
            option.rect.width() - (textMargin * 2), option.rect.height()));
        QItemDelegate::drawCheck(painter, option, checkRect, state);
    }
    virtual bool editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option,
        const QModelIndex &index)
    {
        Q_ASSERT(event);
        Q_ASSERT(model);

        // make sure that the item is checkable
        Qt::ItemFlags flags = model->flags(index);
        if (!(flags & Qt::ItemIsUserCheckable) || !(flags & Qt::ItemIsEnabled))
            return false;
        // make sure that we have a check state
        QVariant value = index.data(Qt::CheckStateRole);
        if (!value.isValid())
            return false;
        // make sure that we have the right event type
        if (event->type() == QEvent::MouseButtonRelease) {
            const int textMargin = QApplication::style()->pixelMetric(QStyle::PM_FocusFrameHMargin) + 1;
            QRect checkRect = QStyle::alignedRect(option.direction, Qt::AlignCenter,
                check(option, option.rect, Qt::Checked).size(),
                QRect(option.rect.x() + textMargin, option.rect.y(),
                option.rect.width() - (2 * textMargin), option.rect.height()));
            if (!checkRect.contains(static_cast<QMouseEvent*>(event)->pos()))
                return false;
        } else if (event->type() == QEvent::KeyPress) {
            if (static_cast<QKeyEvent*>(event)->key() != Qt::Key_Space
                && static_cast<QKeyEvent*>(event)->key() != Qt::Key_Select)
                return false;
        } else {
            return false;
        }
        Qt::CheckState state = (static_cast<Qt::CheckState>(value.toInt()) == Qt::Checked
            ? Qt::Unchecked : Qt::Checked);

        QMessageBox::information(0,
            QString((state == Qt::Checked) ? "Qt::Checked" : "Qt::Unchecked"),
            QString("[%1/%2]").arg(index.row()).arg(index.column()));

        return model->setData(index, state, Qt::CheckStateRole);
    }
    virtual void drawFocus(QPainter *painter, const QStyleOptionViewItem &option, const QRect &rect) const
    {
        QItemDelegate::drawFocus(painter, option, option.rect);
    }
};


static int ROWS = 3;
static int COLS = 1;
class Table : public QTableWidget
{
    Q_OBJECT
public:
    Table(QWidget *parent = 0)
        : QTableWidget(ROWS, COLS, parent)
    {
        setItemDelegate(new ItemDelegate(this));
        QTableWidgetItem *item = 0;
        for (int i=0; i<rowCount(); ++i) {
            for (int j=0; j<columnCount(); ++j) {
                setItem(i, j, item = new QTableWidgetItem);
                item->setFlags(Qt::ItemIsEnabled|Qt::ItemIsUserCheckable);
                item->setCheckState((i+j) % 2 == 0 ? Qt::Checked : Qt::Unchecked);
            }
        }
    }
};

#endif

2)。 main.cpp文件:

#include <QApplication>

#include "table.h"

int main(int argc, char **argv)
{
    QApplication a(argc, argv);
    Table w;
    w.show();
    return a.exec();
}
祝你好运。

PS:这是原始text

答案 1 :(得分:3)

处理click事件,在那里你将获得modelindex,获取数据并修改相同的

如果您要插入多个文字或图标,则需要为列表视图设置委托

答案 2 :(得分:1)

以下是 mosg 使用 QStyleItemDelegate 建议的示例的另一个类似概念。 http://developer.qt.nokia.com/faq/answer/how_can_i_align_the_checkboxes_in_a_view

答案 3 :(得分:0)

在点击的插槽和 index.data(Qt::CheckStateRole) 上使用:

void MainWindow::on_tableView_clicked(const QModelIndex &index)
{
    if(index.column() == 2){
        if(index.data(Qt::CheckStateRole) == Qt::Checked){
             //your code 
        }else if(index.data(Qt::CheckStateRole) == Qt::Unchecked){
             //your code
        }
    }
    //get other infos
    QVariant value = index.sibling(index.row(),0).data();
    QString selectedMessageName = value.toString();
}