2014-11-24 86 views
1

我想爲用戶懸停在標題上時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() 

如果我設置上樹部件本身的光標,然後按預期光標設置。

回答

1

沒有必要重置樹的標題。

只需設置光標上的現有報頭的視口:

self.header().viewport().setCursor(QtCore.Qt.WaitCursor) 
1

哈克的方式,但你可以通過鑄造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.WaitCursorQtGui.QCursor對象。

希望是有用的。

0

添加到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() 

有兩種方法唯一的問題迄今似乎是將小部件添加將刪除更改列的大小的能力。