Issue
Why does my QTreeView, using PySide, have this little small empty boxed area that the user can click and get a dotted focus box around? How can I remove it? I only want a simple checkbox in the first column.
Code:
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()
Solution
The first column shows a focus-rectangle because you set its text to an empty string. So if you don't want that to happen, don't set any text all.
Alternatively, you could make the view show a focus-rectangle for the whole row, rather than separately for each column:
self.uiItems.setAllColumnsShowFocus(True)
Answered By - ekhumoro
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.