Home > other >  PyQT drag and drop to reorder items
PyQT drag and drop to reorder items

Time:11-23

Im trying to make a small app that would help me rename pictures. Since i want to manually order them, i had the idea to simply show a window with thumbnails inside in a grid ( or small scale images, doesnt matter ), and then drag and drop reorder them as i see fit. Afterwards its just click a button and they get properly named acording to the order.

Is there any container or something that would allow its inside widgets to be moved around like that while also properly displaying the inside widgets?

The ways im thinking of currently since i cant find anything else, is to make the whole background a canvas, move x/y on drag/drop of the pictures and then calculate where im dropping it off and manually reorder the whole canvas again and keep redrawing.

Im open to different python solution if anyone has them, but after checking wxwidgets and tkinter, i havent found anything that would be a solution to this without a lot of manual code.

CodePudding user response:

After ekhumoro hint, i was able to solve it.

Heres a sample code that reads the current folder of its files, shows them as "thumbnails", and allows reordering.

#!/usr/bin/python

import sys, os
from PyQt5.QtWidgets import (QListWidget, QWidget, QMessageBox,
                             QApplication, QVBoxLayout,QAbstractItemView,QListWidgetItem )
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import QSize, Qt
from PyQt5.QtWidgets import QListView


class Example(QWidget):

    def __init__(self):
        super().__init__()
        self.icon_size = 200
        self.initUI()


    def loadImageItem(self, fajl,folder=None):
        icon = QIcon()
        item = QListWidgetItem()
        if folder is not None:
            pot = os.path.join(folder,fajl)
        else:
            pot = fajl
        icon.addFile(pot,size=QSize(self.icon_size,self.icon_size))
        item.setIcon(icon)
        item.setTextAlignment(Qt.AlignBottom)
        return item

    def initUI(self):

        vbox = QVBoxLayout(self)

        listWidget = QListWidget()
        #make it icons 
        listWidget.setDragDropMode(QAbstractItemView.InternalMove)
        listWidget.setFlow(QListView.LeftToRight)
        listWidget.setWrapping(True)
        listWidget.setResizeMode(QListView.Adjust)
        listWidget.setMovement(QListView.Snap)
        listWidget.setIconSize(QSize(200,200))

        folder = os.getcwd()
        #folder = "/mnt/Data/pictures/2022-10-30 Sveta Katarina/izbor/1"
        files = os.listdir(folder)
        files = [f for f in files if os.path.isfile(os.path.join(folder,f))]

        for foo in files:
            listWidget.addItem(self.loadImageItem(foo,folder=folder))

        vbox.addWidget(listWidget)
        self.setLayout(vbox)
        self.setGeometry(10, 10, 1260, 820)
        self.setWindowTitle('Image renamer')
        self.show()


def main():

    App = QApplication(sys.argv)
    ex = Example()
    sys.exit(App.exec())

if __name__ == '__main__':
    main()
  • Related