Hi i was reading this article on how to drag and drop widget items which makes sense like
Qt's item views pass around items using the internal application/x-qabstractitemmodeldatalist MIME type
I also read this Question which mainly focuses on string model from Qlinewidget and QTreewidget.
But how can i get a QAbstractItemView.model()
of a QFrame which contains multple QtWidgets.
Bottom line Question is: How to move QFrame which contains multple QtWidgets, within QTreeWidget. Please see the example code below: Press the button to add childs and try dragging them in between other childs or the parent of the first level tree hierarchy
from PyQt5.QtWidgets import (QTreeWidget, QTreeWidgetItem, QPushButton, QLabel, QDialog, QVBoxLayout, QApplication, QLineEdit)
from PyQt5.QtWidgets import (QPushButton, QDialog, QTreeWidget,
QTreeWidgetItem, QVBoxLayout,
QHBoxLayout, QFrame, QLabel, QComboBox,
QApplication)
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
self.index=0
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
self.gridLayout.setObjectName("gridLayout")
self.treeWidget = QtWidgets.QTreeWidget(self.centralwidget)
self.treeWidget.setObjectName("treeWidget")
self.treeWidget.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.treeWidget.setFrameShadow(QtWidgets.QFrame.Sunken)
self.treeWidget.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAsNeeded)
self.treeWidget.setSizeAdjustPolicy(QtWidgets.QAbstractScrollArea.AdjustToContents)
self.treeWidget.setAutoScrollMargin(10)
self.treeWidget.setDragDropMode(QtWidgets.QAbstractItemView.InternalMove)
self.treeWidget.setDragEnabled(1)
self.treeWidget.setDefaultDropAction(QtCore.Qt.MoveAction)
self.treeWidget.setAnimated(True)
self.treeWidget.setWordWrap(False)
self.treeWidget.setExpandsOnDoubleClick(True)
self.treeWidget.setObjectName("treeWidget")
self.treeWidget.header().setVisible(False)
self.treeWidget.header().setHighlightSections(False)
self.treeWidget.header().setSortIndicatorShown(False)
self.treeWidget.header().setStretchLastSection(True)
self.gridLayout.addWidget(self.treeWidget, 0, 0, 1, 1)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
#initialize top level items
self.topLevelItem1 = QTreeWidgetItem(self.treeWidget)
#add those top level in treewidget
self.treeWidget.addTopLevelItem(self.topLevelItem1)
#create button
self.ButtonWidget=QtWidgets.QPushButton("Press")
self.ButtonWidget.clicked.connect(self.AddQFrame)
#add button to tree widget
self.treeWidget.setItemWidget(self.topLevelItem1, 0, self.ButtonWidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def AddQFrame(self):
#add combo box on button press
self.index=self.index+1
#create child item
self.ChildItem1 = QTreeWidgetItem()
self.topLevelItem1.addChild(self.ChildItem1)
#create frame & horizontal layout for that child item
self.ChildWidgetFrame=QFrame(self.treeWidget)
self.layoutChild=QHBoxLayout(self.ChildWidgetFrame)
#layout in qframe
self.QcomboWidget=QtWidgets.QComboBox()
self.QcomboWidget.addItem("item "+str(self.index))
self.QcomboWidget.addItem("CC")
self.QcomboWidget.addItem("CV")
self.spacerItem3 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Preferred)
#widgets in layout
self.layoutChild.addWidget(QLabel("#"+str(self.index)))
self.layoutChild.addWidget(self.QcomboWidget)
self.layoutChild.addItem(self.spacerItem3)
#display widget
self.treeWidget.setItemWidget(self.ChildItem1, 0, self.ChildWidgetFrame)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.treeWidget.setSortingEnabled(False)
self.treeWidget.headerItem().setText(0, _translate("MainWindow", "1"))
__sortingEnabled = self.treeWidget.isSortingEnabled()
self.treeWidget.setSortingEnabled(False)
self.treeWidget.setSortingEnabled(__sortingEnabled)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
EDIT
The question was confusing in terms of dragging of which treelevel. So i edited the question: I am only rearranging/moving childs/parents in first hierarchy . Thanks for your time and efforts!
EDIT2
Another edit to clarify the level of treewidget hierarchy.
This is what i am trying to achieve: Dnd Parent Qframe and its child upto first level of tree.