0

I'm trying to add an option for a QTreeWidget to have multi line editing, which I would assume will require a QTextEdit. The problem is that the examples I've found online just do not work.

The answers I've found have all pointed to using tree.setItemWidget(item, column, widget), but If I add that line, the window just doesn't appear at all. What am I doing wrong in this case?

Here is my example code that has the issue:

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_())

I've tried it in PySide (2.7) and PySide2 (3.7).

Edit: For Python 3 at least, it seemed to be an issue with PySide2, where forcing PyQt5 somehow fixed whatever it was. I'm still unable to launch with Python 2 as I can't really install PyQt4.

Edit 2: It actually causes a crash if you use it in a program such as Nuke that uses PySide, I may need to ask a more specific question if I can't figure it out from this one.

Peter
  • 3,186
  • 3
  • 26
  • 59
  • I just tried your code-snippet with minor changes. I needed `from PyQt5 import QtWidgets, QtCore` instead of `from Qt import QtWidgets, QtCore`. With or without the line `tree.setItemWidget(topLevelItem, 0, topLevelButton)`, the window appeared. _(The only difference was if the button appeared or not, but that is of course understandable with the line commented out.)_ – Duck Dodgers Jan 14 '19 at 15:03
  • Hmm, it seems it could have been some issue with `PySide` then, trying it on Python 3 with PyQt5 seemed to kick start it, and it now works with PySide2. I'd be curious if anyone with a PyQt4 installation could try it with Python 2. – Peter Jan 14 '19 at 15:11

1 Answers1

1

Sorry, PyQt5 is working.

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_())

enter image description here

S. Nick
  • 12,879
  • 8
  • 25
  • 33
  • Hmm thanks, it seems that forcing PyQt5 over PySide2 initially got it working in Python 3, then PySide2 was able to pick it up. Still no luck in Python 2 though, which is weird as the function dates back to PyQt4. – Peter Jan 14 '19 at 15:12