简体   繁体   English

QTreeWidget关闭选择

[英]QTreeWidget turn off selection

默认情况下, QTreeWidget管理行的选择( 当您单击一行时,它突出显示它,当您单击另一行时,它突出显示它并取消选择上一行 ),我不希望这样做并且无法弄清楚如何将其关闭。

you can use setSelectionMode of the QAbstractItemView class (which QTreeWidget is inherited from) to set no selection mode to the component. 您可以使用QAbstractItemView类(继承自QTreeWidget )的setSelectionMode来为组件设置任何选择模式。 Something like this (sorry, code in C++): 这样的东西(抱歉,C ++中的代码):

yourtreeView->setSelectionMode(QAbstractItemView::NoSelection);

In this case items would not get selected but you still will see focus rectangle around them. 在这种情况下,不会选择项目,但您仍将在它们周围看到焦点矩形。 To fix this you can set your widget to not accept focus by calling: 要解决此问题,您可以通过以下方法将小部件设置为不接受焦点:

yourtreeView->setFocusPolicy(Qt::NoFocus);

if your tree widget has to accept focus but should not be drawing focus rectangles you can use custom item delegate and remove State_HasFocus state from the item's state before drawing it. 如果树小部件必须接受焦点但不应绘制焦点矩形,则可以使用自定义项目委托并在绘制项目之前从项目状态中删除State_HasFocus状态。 Something like this: 像这样:

class NoFocusDelegate : public QStyledItemDelegate
{
protected:
    void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const;
};

void NoFocusDelegate::paint(QPainter* painter, const QStyleOptionViewItem & option, const QModelIndex &index) const
{
    QStyleOptionViewItem itemOption(option);
    if (itemOption.state & QStyle::State_HasFocus)
        itemOption.state = itemOption.state ^ QStyle::State_HasFocus;
    QStyledItemDelegate::paint(painter, itemOption, index);
}

....

NoFocusDelegate* delegate = new NoFocusDelegate();
yourtreeView->setItemDelegate(delegate);

Thanks for the answer above, I think the Python version is (^ ^): 感谢上面的回答,我认为Python版本是(^ ^):

yourtreeView.setSelectionMode(QAbstractItemView.NoSelection)
yourtreeView.setFocusPolicy(QtCore.Qt.NoFocus)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM