简体   繁体   English

PyQt5:如何将文本从 QTextEdit 拖到 QlistView 中?

[英]PyQt5: How to drag text from QTextEdit into a QlistView?

I'm currently building a GUI, to which I would like to add the possibility to drag a string from a QTextEdit, and drop it into a QListView so that the dropped string creates a new row.我目前正在构建一个 GUI,我想添加从 QTextEdit 中拖动字符串并将其放入 QListView 的可能性,以便放置的字符串创建一个新行。

But sadly I'm a bit confused with what I should be doing.但可悲的是,我对我应该做什么有点困惑。 There are tons of examples, but I can't see clearly what I should be doing.有很多例子,但我看不清楚我应该做什么。

From what I understand I have to reimplement functions but right now I'm more of an headless chicken.据我了解,我必须重新实现功能,但现在我更像是一只无头鸡。


from PyQt5 import QtCore, QtGui, QtWidgets
import testing_ground_lib, sys


class ListModel(QtGui.QStandardItemModel):
    def supportedDropActions(self):
        <MAYBE DO STUFF HERE>

    def dropMimeData(self, data, action, row, column, modelIndex):
        <MAYBE DO STUFF HERE>

class testing_ground(QtWidgets.QMainWindow, testing_ground_lib.Ui_TestWindow):

    def __init__(self, parent=None):
        QtWidgets.QMainWindow.__init__(self, parent)
        self.setupUi(self)

        self.list_model = ListModel(0, 1, self)
        self.inputs_list.setModel(self.list_model)



sys.argv = ['']
app = QtWidgets.QApplication(sys.argv)
testing_ground = testing_ground()
testing_ground.show()
app.exec_()

testing_ground_lib is just the UI generated through QtDesigner, in which we have self.inputs_list.setDragDropMode(QtWidgets.QAbstractItemView.DragDrop) and where my QTextEdit widget is defined ( self.req_editor ) testing_ground_lib只是通过 QtDesigner 生成的 UI,其中我们有self.inputs_list.setDragDropMode(QtWidgets.QAbstractItemView.DragDrop)和我的 QTextEdit 小部件被定义的地方( self.req_editor

In reimplemented dropMimeData, I tried to print stuff just to see how it works but it did not do anything when I was trying to drop my string within QListView.在重新实现的 dropMimeData 中,我试图打印一些东西只是为了看看它是如何工作的,但是当我试图将我的字符串放在 QListView 中时它没有做任何事情。

It is clear to me that I have to implement something that would append a row in ListView on drop of the string, and set the new item with the text from the string, I just can't get what is the starting point.我很清楚,我必须在删除字符串时在 ListView 中实现 append 一行,并使用字符串中的文本设置新项目,我只是不知道起点是什么。

EDIT : after further investigation, I managed to make the item acceptable by adding:编辑:经过进一步调查,我设法通过添加以下内容使该项目可以接受:

    def mimeTypes(self):
        return ['text/plain']

in my ListModel class.在我的 ListModel class 中。 Now I think I just have to properly reimplement dropMimeData and I think all will be good.现在我想我只需要正确地重新实现 dropMimeData 并且我认为一切都会好起来的。 I'll update this post if I manage to do so.如果我设法这样做,我会更新这篇文章。

Here is a solution to drag the highlighted text in a QTextEdit and drop it into a QListView, adding a new row with that text.这是一个解决方案,可以将 QTextEdit 中突出显示的文本拖放到 QListView 中,并使用该文本添加一个新行。 Subclass QListView and reimplement dragEnterEvent , dragMoveEvent , and dropEvent .子类 QListView 并重新实现dragEnterEventdragMoveEventdropEvent In the dropEvent , add the new item with QStandardItemModel.appendRow() .dropEvent中,使用QStandardItemModel.appendRow()添加新项目。

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

class ListView(QListView):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.setAcceptDrops(True)
        self.setModel(QStandardItemModel(0, 1))

    def dragEnterEvent(self, event):
        event.accept() if event.mimeData().hasText() else event.ignore()

    def dragMoveEvent(self, event):
        event.accept() if event.mimeData().hasText() else event.ignore()

    def dropEvent(self, event):
        if event.mimeData().hasText():
            event.setDropAction(Qt.CopyAction)
            self.model().appendRow(QStandardItem(event.mimeData().text()))
            event.accept()
        else:
            event.ignore()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = QWidget()
    hbox = QHBoxLayout(window)
    hbox.addWidget(QTextEdit())
    hbox.addWidget(ListView())
    window.show()
    sys.exit(app.exec_())

Result:结果:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM