LoginSignup
1
0

More than 1 year has passed since last update.

PyQt5のチュートリアルを動かす ➉ ~ドラッグ&ドロップ機能

Last updated at Posted at 2021-12-02

はじめに

PyQt5でのドラッグ&ドロップです

ドラッグ&ドロップ機能はユーザーにとって直感的で使いやすいため、多くのデスクトップアプリの機能に用意されています。

QDragクラスを使用するとそのような事ができるようです。

デモ

drag_drop.py
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *

class combo(QComboBox):
   def __init__(self, title, parent):
      super(combo, self).__init__( parent)
      self.setAcceptDrops(True)

   def dragEnterEvent(self, e):
      print (e)

      if e.mimeData().hasText():
         e.accept()
      else:
         e.ignore()

   def dropEvent(self, e):
      self.addItem(e.mimeData().text())

class Example(QWidget):
   def __init__(self):
      super(Example, self).__init__()

      self.initUI()

   def initUI(self):
      lo = QFormLayout()
      lo.addRow(QLabel("Type some text in textbox and drag it into combo box"))

      edit = QLineEdit()
      edit.setDragEnabled(True)
      com = combo("Button", self)
      lo.addRow(edit,com)
      self.setLayout(lo)
      self.setWindowTitle('Simple drag and drop')
def main():
   app = QApplication(sys.argv)
   ex = Example()
   ex.show()
   app.exec_()

if __name__ == '__main__':
   main()

実行すると以下のようなwindowが表示されます。

Screenshot from 2021-12-02 18-27-42.png

文字を選択しinputボックスにドラッグ&ドロップすると文字が表示されました。

Screenshot from 2021-12-02 18-29-01.png

参考

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0