PYTHON3+PYQT5 implementing drag-and-drop functionality

Source: Internet
Author: User
This article is mainly for you to introduce the PYTHON3+PYQT5 implementation of drag-and-drop function, with a certain reference value, interested in small partners can refer to

This article is an example of "Python Qt GUI Quick Programming" in the 10th chapter drag and drop with python3+pyqt5 to rewrite, the chart list, tables and other drag and drop, the basic principle is identical, all using Setacceptdrops (True) and setdragenabled (True).

#!/usr/bin/env python3import osimport sysfrom pyqt5.qtcore import qtfrom pyqt5.qtwidgets import (QApplication, QDialog, Q Hboxlayout, Qlistwidget, Qlistwidgetitem, Qsplitter, qtablewidget) from Pyqt5.qtgui import Qiconclass Form (QDialog): def __init__ (self, Parent=none): Super (Form, self). __init__ (parent) Listwidget = Qlistwidget () listwidget.setacceptdrops ( True) listwidget.setdragenabled (true) path = Os.path.dirname (__file__) for image in sorted (Os.listdir (PAT H, "Images")): If Image.endswith (". png"): item = Qlistwidgetitem (Image.split (".") [0].capitalize ()) Item.seticon (Qicon (Os.path.join (Path, "images/{0}". Format (image))) Listwidget.additem (ite  m) Iconlistwidget = Qlistwidget () iconlistwidget.setacceptdrops (True) iconlistwidget.setdragenabled (True) Iconlistwidget.setviewmode (qlistwidget.iconmode) Tablewidget = Qtablewidget () tablewidget.setrowcount (5) TABLEWIDG Et.setcolumncount (2) tablewidget.sethorizontalheaderlabels (["Column #1", "COlumn #2 "]) Tablewidget.setacceptdrops (true) tablewidget.setdragenabled (true) splitter = Qsplitter (qt.horizontal) SPL Itter.addwidget (Listwidget) splitter.addwidget (iconlistwidget) splitter.addwidget (tableWidget) layout = QHBoxLayout  () Layout.addwidget (splitter) self.setlayout (layout) self.setwindowtitle ("Drag and Drop") if __name__ = = "__main__": App = Qapplication (sys.argv) Form = form () form.show () app.exec_ ()

Operation Result:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.