All Projects → d1vanov → PyQt5-reorderable-list-model

d1vanov / PyQt5-reorderable-list-model

Licence: GPL-3.0 license
Example of reorderable via drag-n-drop list model in PyQt5

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to PyQt5-reorderable-list-model

python-kaynaklari
Python Türkiye Facebook sayfasında başlatılan projenin yeni sayfası
Stars: ✭ 30 (+25%)
Mutual labels:  pyqt5
NotEnoughAV1Encodes-Qt
Linux GUI for AV1 Encoders
Stars: ✭ 27 (+12.5%)
Mutual labels:  pyqt5
pyqt5-custom-widgets
More useful widgets for PyQt5
Stars: ✭ 199 (+729.17%)
Mutual labels:  pyqt5
tablexplore
Table analysis and plotting application written in PySide2/PyQt5
Stars: ✭ 89 (+270.83%)
Mutual labels:  pyqt5
KustomPyper
Get amazing wallpapers from reddit, unsplash, bing and wallhaven for your Desktop
Stars: ✭ 24 (+0%)
Mutual labels:  pyqt5
Grafatko
An app for creating and visualizing graphs and graph-related algorithms.
Stars: ✭ 22 (-8.33%)
Mutual labels:  pyqt5
food-and-grocery-automatization
Python application that automatizes your food and grocery orders using Selenium library.
Stars: ✭ 15 (-37.5%)
Mutual labels:  pyqt5
Browthon-Reborn
Latest version of Browthon. A clean version in Python with PyQt5
Stars: ✭ 14 (-41.67%)
Mutual labels:  pyqt5
gaitutils
Extract and visualize gait data
Stars: ✭ 28 (+16.67%)
Mutual labels:  pyqt5
pynocchio
🚀 A minimalist comic reader
Stars: ✭ 72 (+200%)
Mutual labels:  pyqt5
Handwritten Digit Recognition
手写数字识别。利用Tensorflow和MNIST实现的一个简单的手写数字识别程序。
Stars: ✭ 22 (-8.33%)
Mutual labels:  pyqt5
PyTunes
A lightweight music player made in Python
Stars: ✭ 38 (+58.33%)
Mutual labels:  pyqt5
15-minute-apps
15 minute (small) desktop apps built with PyQt
Stars: ✭ 3,469 (+14354.17%)
Mutual labels:  pyqt5
tws async
Make the Python IB API from Interactive Brokers run inside an event loop
Stars: ✭ 79 (+229.17%)
Mutual labels:  pyqt5
BankCard-Recognizer
Identifying numbers from bankcard, based on Deep Learning with Keras [China Software Cup 2019]
Stars: ✭ 74 (+208.33%)
Mutual labels:  pyqt5
HealthApp
A desktop application to fetch Wikipedia,Google,Disease results and save them as text file,in database.Have a Section to search details about doctors in location
Stars: ✭ 23 (-4.17%)
Mutual labels:  pyqt5
Imfusion
Image fusion using Discrete Wavelet Transformation
Stars: ✭ 23 (-4.17%)
Mutual labels:  pyqt5
QtPyConvert
An automatic Python Qt binding transpiler to the Qt.py abstraction layer.
Stars: ✭ 66 (+175%)
Mutual labels:  pyqt5
Hand-Digits-Recognition
Recognize your own handwritten digits with Tensorflow, embedded in a PyQT5 GUI. The Neural Network was trained on MNIST.
Stars: ✭ 11 (-54.17%)
Mutual labels:  pyqt5
gopem
GUI for OPEM library
Stars: ✭ 20 (-16.67%)
Mutual labels:  pyqt5

PyQt5-reorderable-list-model

Example of reorderable via drag-n-drop list model in PyQt5

What's this

This small script is a demo of how one can implement a model within Qt's model-view framework which items can be reordered via drag-n-drop.

It is a continuation of work started in the answer to this question on Stack Overflow. Now this demo uses PyQt5. It was tested with Python 3.4.3.

How it works

As long as Qt's model-view framework's drag-n-drop requires the serialization of some data about the items being repositioned, the reordering via drag-n-drop can be implemented in the following way:

  • On drag we serialize some info about the dragged items
  • On drop we:
    • deserialize this info back
    • use this info to locate each dropped item's original position in the model - we need it to determine where to put the dropped item - before the item onto which we dropped or after it; the former option is useful if we drop onto the very first item and the latter option is useful in all other cases
    • insert the dropped item into the appropriate position within the model
    • if our model properly implements the removeRows method and the view's dragDropOverwriteMode property is set to false (as it is by default in QListView and QTreeView but not in QTableView), Qt would call it automatically after dropMimeData method if the latter one returns true thus confirming the drop was processed successfully.

From the model's perspective that wraps up the whole drag-n-drop story. However, there's one more important aspect: the selection. The selection in Qt's model-view framework belongs to the view part and thus the model knows nothing about the selection and how to update it properly. So we need to tinker with the view part as well if we want it to behave nicely.

Perhaps the simplest and most intuitive behaviour would be to have the dropped items selected and one of them to be set as the current item (selected and current are two different concepts within QItemSelectionModel) after the reordering is finished. I haven't found a way to achieve this with standard QItemSelectionModel so I implemented a custom subclass of it and added a couple of things to the model to provide some useful info to the selection model:

  • The model now stores the list of last dropped items updated near the end of dropMimeData method
  • The model also sets the boolean flag in the end of dropMimeData method thus indicating that the next call to removeRows should be treated as the call performed by Qt automatically which indicates the end of drag-n-drop events sequence. So in the end of removeRows the model checks this flag and if it's set, it drops the flag back and emits a custom signal dragDropFinished which intends to informs whoever is listening about the fact that the whole drag-n-drop sequence is finished from the model's perspective.
  • The selection model connects a special slot to this model's signal in which it fetches the list of last dropped items from the model, figures out which rows these items now correspond to (using another custom method within the model - rowForItem) and selects these rows. It also sets the last of these rows as the current one.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].