通过PySide为QTreeWidget中的标题设置光标
问题描述:
我想为用户悬停在标题上时QTreeWidget的标题设置光标。我曾尝试通过self.header().setCursor(my_cursor)
设置课程中的标题,但到目前为止,当我将鼠标悬停在标题上时,光标未更改。我已经通过Google进行搜索,试图找出如何做到这一点,但到目前为止,我一无所获。我已经用PySide 1.2.0(Maya 2015)和1.2.2进行了测试。通过PySide为QTreeWidget中的标题设置光标
我做错了吗,还是有解决方法吗?下面是一些代码示例:
import sys
from PySide import QtCore, QtGui
class Tree(QtGui.QTreeWidget):
def __init__(self, parent = None):
super(Tree, self).__init__(parent = parent)
self.header().setCursor(QtGui.QCursor(QtCore.Qt.WaitCursor))
widget = Tree()
widget.show()
如果我设置上树部件本身的光标,然后按预期光标设置。
答
没有必要重置树的标题。
只需设置光标上的现有报头的视口:
self.header().viewport().setCursor(QtCore.Qt.WaitCursor)
答
哈克的方式,但你可以通过铸造self.header()
成QWidget
,因为setCursor()
做到这一点QWidget
类的方法。
import sys
from PySide import QtCore, QtGui
class Tree(QtGui.QTreeWidget):
def __init__(self, parent=None):
super(Tree, self).__init__(parent=parent)
QtGui.QWidget(self.header()).setCursor(QtCore.Qt.WaitCursor)
widget = Tree()
widget.show()
而且你真的没有投QtCore.Qt.WaitCursor
到QtGui.QCursor
对象。
希望是有用的。
答
添加到kartikg3的答案。这将允许使用标题的全长。
import sys
from PySide import QtCore, QtGui
class Tree(QtGui.QTreeWidget):
def __init__(self, parent=None):
super(Tree, self).__init__(parent=parent)
header_widget = QtGui.QWidget()
header_widget.setCursor(QtGui.QCursor(QtCore.Qt.WaitCursor))
header_layout = QtGui.QHBoxLayout()
header_layout.addWidget(header_widget)
self.header().setLayout(header_layout)
widget = Tree()
widget.show()
有两种方法唯一的问题迄今似乎是将小部件添加将删除更改列的大小的能力。