zl程序教程

您现在的位置是:首页 >  移动开发

当前栏目

Qt on Android:QTableView不显示选中虚框

AndroidQtOn 显示 选中 QTableView
2023-09-14 09:10:09 时间

    在使用 QTableView 或 QTableWidget 时。有时我们不想要选中虚框,能够实现一个 ItemDelegate ,重写 drawFocus() 和 drawCheck()  两个虚函数,然后调用 QAbstractItemView 的 setItemDelegate() 把自己定义的 itemDelegate 对象传递给 QTableView 就可以。须要注意的是,QAbstractItemView 不会删除你设置给它的 ItemDelegate ,须要开发人员自己在合适的时候删除它。

   以下是一个演示样例, RowDelegate 的代码:

#include <QItemDelegate>

class RowDelegate : public QItemDelegate
{
public:
    RowDelegate(QObject * parent = 0) :QItemDelegate(parent)
    {
    }

    virtual void drawFocus(QPainter *painter, const QStyleOptionViewItem &option,
                           const QRect &rect) const
    {
    }

    virtual void drawCheck(QPainter *painter, const QStyleOptionViewItem &option,
                           const QRect &rect, Qt::CheckState state) const
    {
    }
};

    如你所见,RowDelegate 类的 drawFocus() 和 drawCheck() 嘛事不干,这样就达到了目的。

    对于 QListView 或 QListWidget 。使用上面的代码也能够去掉选中虚框。