SoFunction
Updated on 2024-10-30

python GUI library graphical interface development of PyQt5 drag and drop control example details

In this post, we learn about Drag and Drop controls in PyQt5 interface.

Drag and Drop Action

In GUI, drag and drop refers to the action of clicking on an object and dragging it to another object. For example, the Baidu Cloud PC client supports drag-and-drop to move files quickly:

Drag and drop actions can be very intuitive and easy to complete some very complex or cumbersome operations in the GUI program.

Implementing Drag and Drop in PyQt

In PyQt5, we can also easily use the drag and drop feature.

This can be done either using the Qt designer or using the API. Let's start by using the Qt designer to graphically design the GUI. Based on the previous GUI, we create a new tab.

We created a new tab and placed a LineEdit widget, a PushButton widget, and two ListWidget widgets inside.

For simple drag and drop effects, we can set them directly using the options in the Qt designer. For example, we can directly set the drag and drop functionality for the ListWidget widget using the dragEnable property, the dragDropOverwriteMode property, and the dragDropMode property:

Some slightly more complex drag-and-drop functionality will require writing Python logic processing code to accomplish.

We'll start by saving the UI file and converting it to a Python file.

pyuic5 -o conplex_window_drag.py conplex_window.ui

Then, create a new Python wenga and introduce the just converted Python file in the file:

# coding:utf-8
# Mr. State Python GUI Tutorials

from PyQt5 import QtCore,QtWidgets,QtGui
from GUI import conplex_window_drag
import sys
import time

class MainWindow(object):
  def __init__(self):
    app = ()
    MainWindow = ()
     = conplex_window_drag.Ui_MainWindow()
    (MainWindow)

    self.update_date()
    self.update_calendar()

    self.set_lcd()
    self.set_dial()

    self.update_progressbar()

    self.set_font()
    ()
    (app.exec_())

  # Modify date modifier values
  def update_date(self):
    (())

  # Calendar signal slots
  def update_calendar(self):
    (self.update_date)

  # Setting the LCD digits
  def set_lcd(self):
    (())

  # Signal slot for dial
  def set_dial(self):
    ['int'].connect(self.set_lcd)

  # Mr. State
  # Pushbutton signal slot
  def update_progressbar(self):
    (self.start_progressbar)
    .radioButton_2.(self.stop_progressbar)
    .radioButton_3.(self.reset_progressbar)
    self.progress_value = 0
    self.stop_progress = False

  def progressbar_counter(self, start_value=0):
    self.run_thread = RunThread(parent=None, counter_start=start_value)
    self.run_thread.start()
    self.run_thread.counter_value.connect(self.set_progressbar)

  def set_progressbar(self, counter):
    if not self.stop_progress:
      (counter)

  # Launch the progress bar
  def start_progressbar(self):
    self.stop_progress = False
    self.progress_value = ()
    self.progressbar_counter(self.progress_value)

  # Stop the progress bar
  def stop_progressbar(self):
    self.stop_progress = True
    try:
      self.run_thread.stop()
    except:
      pass
  # Reset progress bar
  def reset_progressbar(self):
    self.stop_progressbar()
    self.progress_value = 0
    ()
    self.stop_progress = False

  # Font Selection
  def set_font(self):
    ['QString'].connect()

class RunThread():
  # Define a new signal
  counter_value = (int)

  def __init__(self, parent=None, counter_start=0):
    super(RunThread, self).__init__(parent)
     = counter_start
    self.is_running = True

  def run(self):
    while  < 100 and self.is_running == True:
      (0.1)
       += 1
      print()
      # Signal a new value
      self.counter_value.emit()

  def stop(self):
    self.is_running = False
    print('Thread stopped in progress...')
    ()

if __name__ == "__main__":
  MainWindow()

Running the code works fine:

Next, we create a DragDropButton() class to handle the drag and drop of the button:

class DragDropButton():
  
  def __init__(self, text, parent):
    super().__init__(text, parent)    
    (True)
    
  def dragEnterEvent(self, event):
    if ().hasFormat('text/plain'):
      ()
    else:
      ()
      
  def dropEvent(self, event):
    (().text())

We use the setAcceptDrops property to set the button to receive drag and drop events, create a dragEnterEvent() method to set the drag event response, and create a dropEvent() method to set the drop event response.

Then we call it in the MainWindow() main class:

class MainWindow(object):
  def __init__(self):
    ……
    ()
     = DragDropButton("Drag and drop button",MainWindow)
    .gridLayout_5.addWidget(,0, 1, 1, 2)
    ……

Finally, run it and see:

In the program above, we were able to drag and drop text onto the button.

Well python GUI library graphical interface development in PyQt5 drag and drop control examples are these, more about python PyQt5 GUI library graphical interface development please check the following related links