Python 无法在QTreeWidget内添加QTextEdit
我正在尝试为Python 无法在QTreeWidget内添加QTextEdit,python,qt,pyqt,Python,Qt,Pyqt,我正在尝试为QTreeWidget添加一个选项以进行多行编辑,我认为这将需要QTextEdit。问题是我在网上找到的例子根本不起作用 所有人都指向使用tree.setItemWidget(项、列、小部件),但是如果我添加了那一行,窗口根本就不会出现。在这种情况下,我做错了什么 下面是我遇到问题的示例代码: import sys from Qt import QtWidgets, QtCore class MainWindow(QtWidgets.QMainWindow): def __
QTreeWidget
添加一个选项以进行多行编辑,我认为这将需要QTextEdit
。问题是我在网上找到的例子根本不起作用
所有人都指向使用tree.setItemWidget(项、列、小部件)
,但是如果我添加了那一行,窗口根本就不会出现。在这种情况下,我做错了什么
下面是我遇到问题的示例代码:
import sys
from Qt import QtWidgets, QtCore
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None, **kwargs):
super(MainWindow, self).__init__(parent, **kwargs)
#Add tree widget to window
tree = QtWidgets.QTreeWidget()
tree.setHeaderItem(QtWidgets.QTreeWidgetItem(['col1', 'col2']))
self.setCentralWidget(tree)
#Create items
topLevelButton = QtWidgets.QPushButton('button')
topLevelItem = QtWidgets.QTreeWidgetItem(['test button', 'line edit'])
topLevelItem.setFlags(topLevelItem.flags() | QtCore.Qt.ItemIsEditable)
#Add items to tree widget
tree.addTopLevelItem(topLevelItem)
tree.setItemWidget(topLevelItem, 0, topLevelButton) #the window will not load if this line is not commented out
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = MainWindow()
app.setActiveWindow(window)
window.show()
sys.exit(app.exec_())
我在PySide(2.7)
和PySide2(3.7)
中尝试过它
编辑:至少对于Python3来说,PySide2似乎存在问题,强制PyQt5以某种方式修复了它。我仍然无法使用Python 2启动,因为我无法真正安装PyQt4
编辑2:如果您在使用PySide的程序(如
Nuke
)中使用它,它实际上会导致崩溃。如果我不能从这个问题中找出原因,我可能需要问一个更具体的问题。对不起,PyQt5正在工作
import sys
#from Qt import QtWidgets, QtCore
from PyQt5 import QtWidgets, QtCore # <---
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None, **kwargs):
super(MainWindow, self).__init__(parent, **kwargs)
# Add tree widget to window
tree = QtWidgets.QTreeWidget()
tree.setHeaderItem(QtWidgets.QTreeWidgetItem(['col1', 'col2']))
self.setCentralWidget(tree)
# Create items
topLevelButton = QtWidgets.QPushButton('button')
topLevelItem = QtWidgets.QTreeWidgetItem(['test button', 'line edit'])
topLevelItem.setFlags(topLevelItem.flags() | QtCore.Qt.ItemIsEditable)
# Add items to tree widget
tree.addTopLevelItem(topLevelItem)
tree.setItemWidget(topLevelItem, 0, topLevelButton) # ??? the window will not load if this line is not commented out
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = MainWindow()
app.setActiveWindow(window) # ???
window.show()
sys.exit(app.exec_())
导入系统
#从Qt导入QtWidgets,QtCore
从PyQt5导入qtwidget,QtCore#我刚刚尝试了您的代码片段,但做了一些小改动。我需要PyQt5导入QtWidgets、QtCore中的
,而不是Qt导入QtWidgets、QtCore
中的。无论是否有行树.setItemWidget(topLevelItem,0,topLevelButton)
,都会出现该窗口。(唯一的区别是按钮是否出现,但这当然可以理解,因为这行被注释掉了。)嗯,PySide
可能出现了一些问题,然后在Python3上使用PyQt5尝试它似乎启动了它,现在它可以使用PySide2。我很好奇,如果有人安装了PyQt4,是否可以在Python2上试用它。嗯,谢谢,似乎在PySide2上强制使用PyQt5最初使它在Python3中工作,然后PySide2就可以使用它了。但是在Python2中仍然没有运气,这很奇怪,因为函数可以追溯到PyQt4。