QTreeView 复选框有空焦点

QTreeView checkbox has empty focus

为什么我的 QTreeView 使用 PySide,有这个小的空白框区域,用户可以单击它并在周围获得一个虚线焦点框?我怎样才能删除它?我只想在第一列中有一个简单的复选框。

代码:

import sys
from PySide import QtGui, QtCore

class SortModel(QtGui.QSortFilterProxyModel):

    def __init__(self, *args, **kwargs):
        super(SortModel, self).__init__(*args, **kwargs)

    def lessThan(self, left, right):
        leftData = self.sourceModel().data(left)
        rightData = self.sourceModel().data(right)

        if leftData:
            leftData = leftData.lower()
        if rightData:
            rightData = rightData.lower()

        print('L:', leftData, 'R:', rightData)
        return leftData < rightData


class Browser(QtGui.QDialog):
    def __init__(self, parent=None):
        super(Browser, self).__init__(parent)

        self.initUI()

    def initUI(self):
        self.resize(200, 300)
        self.setWindowTitle('Assets')
        self.setModal(True)

        self.results = ""

        self.uiItems = QtGui.QTreeView()
        self.uiItems.setAlternatingRowColors(True)
        self.uiItems.setSortingEnabled(True)
        self.uiItems.sortByColumn(1, QtCore.Qt.AscendingOrder)
        self.uiItems.setEditTriggers(QtGui.QAbstractItemView.NoEditTriggers)
        self.uiItems.header().setResizeMode(QtGui.QHeaderView.ResizeToContents)
        self.uiItems.setSelectionBehavior(QtGui.QAbstractItemView.SelectRows)
        self.uiItems.setSelectionMode(QtGui.QAbstractItemView.ExtendedSelection)
        self._model = self.create_model(self)
        self._spmodel = SortModel(self)
        self._spmodel.setSourceModel(self._model)
        self._spmodel.setDynamicSortFilter(True)
        self.uiItems.setModel(self._spmodel)

        grid = QtGui.QGridLayout()
        grid.setContentsMargins(0, 0, 0, 0)
        grid.addWidget(self.uiItems, 0, 0)
        self.setLayout(grid)

        self.setLayout(grid)
        self.update_asset_model()
        self.uiItems.doubleClicked.connect(self.doubleClickedItem)
        self.show()

    def doubleClickedItem(self, idx):
        if not idx.isValid():
            return
        print idx.parent(), idx.parent().isValid()
        model = idx.model()
        print model.index(idx.row(), 0, parent=idx.parent()).data()

    def create_model(self, parent):
        model = QtGui.QStandardItemModel()
        model.setHorizontalHeaderLabels([])
        return model

    def add_row_item(self, model, name, _type):
        model.insertRow(0)
        model.setData(model.index(0, 0), '')
        model.setData(model.index(0, 0), QtCore.Qt.Checked,
            role = QtCore.Qt.CheckStateRole
        )
        model.setData(model.index(0, 1), name)
        model.setData(model.index(0, 2), _type)
        model.setData(model.index(0, 2),
            QtGui.QBrush(QtGui.QColor(200, 140, 70, 255)),
            role=QtCore.Qt.ForegroundRole
        )

    def update_asset_model(self):
        model = self.uiItems.model().sourceModel()
        model.clear()
        model.setHorizontalHeaderLabels(['', 'Name', "Output"])

        items = {
            'Doug' : "C:/fire/cache_.jpeg",
            'Mike' : "C:/smoke/cache_.tga",
            'Kevin' : "C:/water/cache_.tif",
            'Curt' : "C:/steam/cache_.jpg",
            'Corey' : "C:/blood/cache_.png"
        }

        for n in items.keys():
            self.add_row_item(model, n, items[n])

    def showEvent(self, event):
        geom = self.frameGeometry()
        geom.moveCenter(QtGui.QCursor.pos())
        self.setGeometry(geom)
        super(Browser, self).showEvent(event)

    def keyPressEvent(self, event):
        if event.key() == QtCore.Qt.Key_Escape:
            # self.hide()
            self.close()
            event.accept()
        else:
            super(Browser, self).keyPressEvent(event)


def main():
    app = QtGui.QApplication(sys.argv)
    ex = Browser()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

第一列显示焦点矩形,因为您将其文本设置为空字符串。因此,如果您不希望这种情况发生,请不要全部设置任何文本。

或者,您可以让视图为整行显示焦点矩形,而不是为每一列单独显示:

    self.uiItems.setAllColumnsShowFocus(True)