在使用 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 。使用上面的代码也能够去掉选中虚框。



相关文章:

  • 2022-12-23
  • 2021-09-13
  • 2022-01-02
  • 2021-06-26
  • 2022-12-23
  • 2021-11-29
猜你喜欢
  • 2022-12-23
  • 2021-04-27
  • 2021-04-12
  • 2022-12-23
  • 2022-12-23
  • 2021-04-29
相关资源
相似解决方案