3

QTableViewとQStandardItemModelを使用していくつかのデータを表示しています。

各行には、チェックボックスのある列があります。このチェックボックスはsetItemによって挿入され、コードは次のとおりです。

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

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

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

次に、チェックボックスを操作します。チェックボックスがユーザーによって状態を変更する場合(チェックされている状態からチェックされていない状態に、またはその逆)、対応するデータ行で何かを実行したいと思います。

チェックボックスの変更をキャッチするためにsignal-slotを使用できることは知っていますが、データ行がたくさんあるので、各行を1つずつ接続したくありません。

チェックアクションをより効果的に操作する方法はありますか?ありがとう :)

4

4 に答える 4

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: ここに原文があります。

于 2010-05-07T07:29:04.073 に答える
3

クリックイベントを処理し、そこでモデルインデックスを取得し、データを取得して同じものを変更します

複数のテキストまたはアイコンを挿入する場合は、リストビューのデリゲートを設定する必要があります

于 2010-05-07T04:14:11.230 に答える
1

これは、代わりにQStyleItemDelegateを使用してmosgによって提案された例の別の同様のアイデアです。http://developer.qt.nokia.com/faq/answer/how_can_i_align_the_checkboxes_in_a_view

于 2010-12-15T15:02:19.747 に答える