Drag and drop for files in directory, path drag and drop order through to python?
-
wrote on 16 Aug 2021, 01:53 last edited by
I have a question about adding drag and drop for user input ordering. I want the user to select a directory and every PDF file from said directory will load into a drag and drop box on the application, based on there ordering when the create PDFs button is clicked an array of the file names will be sent through the QML file to Python so I can know which order the user put it in.
I have this setup with a .qml file and multiple python files which I will link below, I tried multiple things before coming here and none of them really worked. Where I really lack is fully understanding how to link the python and qml file to freely pass information between them. Any help would be great and I thank everyone for there time.
.qml file:
import Qt.labs.platform 1.1 import QtQuick 2.15 import QtQuick.Controls 2.15 import QtQuick.Controls.Material 2.15 import QtQuick.Window 2.15 ApplicationWindow { id: window width: 800 height: 500 visible: true title: qsTr("Darwill Roll Tool") // Set Flags flags: Qt.WindowCloseButtonHint | Qt.WindowMinimizeButtonHint | Qt.CustomizeWindowHint | Qt.MSWindowsFixedSizeDialogHint | Qt.WindowTitleHint // Set material Style Material.theme: Material.Dark Material.accent: Material.LightBlue Image { id: image width: 179 height: 47 source: "../imgs/darwill-logo-header-sml_0.png" anchors.left: parent.left anchors.top: parent.top anchors.margins: 30 } Button { text: "Browse" onClicked: folderDialog.open() anchors.right: parent.right anchors.top: parent.top anchors.rightMargin: 240 anchors.topMargin: 30 } Button { text: "Create PDFs" anchors.right: parent.right anchors.bottom: parent.bottom anchors.margins: 30 onClicked: { backend.foo(comBox.currentText, folderDialog.folder); } } TextField { id: pathField width: 300 text: folderDialog.folder selectByMouse: true placeholderText: qsTr("Path to PDF files") readOnly: true anchors.right: parent.right anchors.top: parent.top anchors.rightMargin: 320 anchors.topMargin: 36 } TextField { id: dividerAmount width: 90 selectByMouse: true placeholderText: qsTr("Divider Amount") anchors.right: parent.right anchors.bottom: parent.bottom anchors.rightMargin: 120 anchors.bottomMargin: 30 } Label { text: "<strong>V</strong> 1.0" anchors.left: parent.left anchors.bottom: parent.bottom anchors.margins: 10 } Label { text: "<strong>Created</strong>: jkuzera@darwill.com" anchors.right: parent.right anchors.bottom: parent.bottom anchors.margins: 10 } ComboBox { id: comBox width: 200 model: comModel anchors.right: parent.right anchors.top: parent.top anchors.margins: 30 } FolderDialog { id: folderDialog folder: StandardPaths.standardLocations(StandardPaths.PicturesLocation)[0] onAccepted: { console.log("You chose: " + folderDialog.folder); } } }
Python file 1 main:
import sys import os import Backend1 # Import Modules from PySide6.QtWidgets import QApplication from PySide6.QtQml import QQmlApplicationEngine from PySide6.QtGui import QIcon # Instance Class if __name__ == "__main__": app = QApplication(sys.argv) engine = QQmlApplicationEngine() app.setWindowIcon(QIcon("imgs/fav.png")); comportComboModel = [ "Roll Size 1", "Roll Size 2", "Roll Size 3", "Roll Size 4", "Roll Size 5", ] engine.rootContext().setContextProperty("comModel", comportComboModel) engine.rootContext().setContextProperty("backend", Backend1.Backend(app)) engine.load(os.path.join(os.path.dirname(__file__), "qml/main.qml")) # Close application if not engine.rootObjects(): sys.exit(-1) sys.exit(app.exec())
Python file 3 backend:
from PySide6.QtCore import QObject, Slot, QUrl class Backend(QObject): @Slot(str, QUrl) def foo(self, combo, folder): self.rollSize = combo self.path = folder.toLocalFile() print(combo, folder.toLocalFile()) self.test() def test(self): print(self.rollSize) print(self.path)
1/1