How to interact with checkbox actions ? (QTableView with QStandardItemModel)

前端 未结 3 1486
有刺的猬
有刺的猬 2021-01-06 18:50

I\'m using QTableView and QStandardItemModel to show some data.

For each row, there is a column which has a check Box, this check box is inserted by setItem, the co

3条回答
  •  离开以前
    2021-01-06 19:51

    I don't deal with QTableView+QStandardItemModel, but may be example below will help you:

    1). table.h file:

    #ifndef TABLE__H
    #define TABLE__H
    
    #include 
    
    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(event)->pos()))
                    return false;
            } else if (event->type() == QEvent::KeyPress) {
                if (static_cast(event)->key() != Qt::Key_Space
                    && static_cast(event)->key() != Qt::Key_Select)
                    return false;
            } else {
                return false;
            }
            Qt::CheckState state = (static_cast(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; isetFlags(Qt::ItemIsEnabled|Qt::ItemIsUserCheckable);
                    item->setCheckState((i+j) % 2 == 0 ? Qt::Checked : Qt::Unchecked);
                }
            }
        }
    };
    
    #endif
    

    2). main.cpp file:

    #include 
    
    #include "table.h"
    
    int main(int argc, char **argv)
    {
        QApplication a(argc, argv);
        Table w;
        w.show();
        return a.exec();
    }
    

    Good luck.

    PS: here is the original text.

提交回复
热议问题