Removing dotted border without setting NoFocus in Windows PyQt

前端 未结 5 1751
猫巷女王i
猫巷女王i 2021-02-04 02:18

There are a few questions on SO about this, all of which seem to say that the only way to remove the dotted border is to set the focusPolicy on widget/item in question to NoFocu

5条回答
  •  眼角桃花
    2021-02-04 02:52

    The dotted border actually annoy me too. I google it many times, try about hundred times to solve it but with less success. Now I want to summary three typical way maybe you already know it, but let's make it more clear so you can understand what you truly need.

    First QSS
    Qss was claimed as most simply way to solve the problem.
    Actually it work quiet well under the non root privilege, but under root it broke.

    table.setStyleSheet("QTableView:{outline: 0;}")
    

    non root privilege
    non root privilege

    root privilege
    root privilege

    So if your application need a root privilege to run, the QSS maybe doesn't suit your need.

    Second it's FrameSheet/FrameShape
    It looks will be effective, but it just work well under non root privilege like the above method.

    table.setStyleSheet("QTableView:{outline: 0}")
    table.setFrameShape(QtWidgets.QFrame.NoFrame)
    

    Third NoFocusDelegate inheritance.
    This is a great method, it solve the problem privilege irrelevant

    class NoFocusDelegate(QtWidgets.QStyledItemDelegate):
        def paint(self, QPainter, QStyleOptionViewItem, QModelIndex):
            if QStyleOptionViewItem.state & QtWidgets.QStyle.State_HasFocus:
                QStyleOptionViewItem.state = QStyleOptionViewItem.state ^ QtWidgets.QStyle.State_HasFocus
            super().paint(QPainter, QStyleOptionViewItem, QModelIndex)
    
    table.setItemDelegate(NoFocusDelegate())
    

    This method help me to get rid of the dotted border mystery, I hope it will be helpful to you too.

提交回复
热议问题