-2

In pyqt5, using qtreewidget, I'm trying to shift the arrows under the next header. Is this possible? I would like to add a thumbnail to the first column and have the arrows in the second.

This is what I have at the moment.

This is what I get when I shift everything along, the arrows stay under header 0

Mock up of what I want

Striped down example of what I tried.

import sys
from PyQt5.QtWidgets import (QVBoxLayout, QDialog, QTreeWidget, QApplication, QTreeWidgetItem)


class QuickExample(QDialog):
    def __init__(self, parent=None):
        super(QuickExample, self).__init__(parent)

        layout = QVBoxLayout()

        tree = QTreeWidget()
        tree.setHeaderLabels(["0", "1"])

        parent = QTreeWidgetItem()
        parent.setText(1, "parent")

        child = QTreeWidgetItem(parent)
        child.setText(1, "child")

        tree.addTopLevelItem(parent)

        layout.addWidget(tree)
        self.setLayout(layout)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    example = QuickExample()
    example.show()
    sys.exit(app.exec_())
Jeremy
  • 41
  • 4
  • How are you moving all elements? Please provide a [minimal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) of what you've done so far. Also, what is the reason for this? What are you going to show in the first column after "moving" the tree? – musicamante Mar 27 '20 at 07:34
  • Does [this answer](https://stackoverflow.com/questions/56690219/parent-a-qtreewidgetitem-to-an-existing-qtreewidgetitem-to-second-column-maintai/56722419#56722419) help? – Heike Mar 27 '20 at 08:42
  • @Heike Yes thanks! tree.header().swapSections(1,0) was the piece I needed. – Jeremy Mar 27 '20 at 08:48

1 Answers1

1

I ended up setting the items in the first column and swapping them with the second. e.g

import sys
from PyQt5.QtWidgets import (QVBoxLayout, QDialog, QTreeWidget, 
    QApplication, QTreeWidgetItem)

class QuickExample(QDialog):
    def __init__(self, parent=None):
        super(QuickExample, self).__init__(parent)

        layout = QVBoxLayout()

        tree = QTreeWidget()
        tree.setHeaderLabels(["Name", "Thumbnail"])

        # swapping the first with the second
        tree.header().swapSections(1, 0)

        parent = QTreeWidgetItem()
        parent.setText(0, "parent")

        child = QTreeWidgetItem(parent)
        child.setText(0, "child")

        tree.addTopLevelItem(parent)

        layout.addWidget(tree)
        self.setLayout(layout)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    example = QuickExample()
    example.show()
    sys.exit(app.exec_())

Now I'm free to add a thumbnail to the new first column.

Jeremy
  • 41
  • 4