简体   繁体   English

PyQt4表用于从剪贴板插入表 - tableWidget?

[英]PyQt4 table for inserting a table from the clipboard - tableWidget?

I am looking for a table that can be generated with the Qt Designer or directly with PyQt4, where one can insert a content, which was copied from an Excel spreadsheet. 我正在寻找一个可以使用Qt Designer或直接使用PyQt4生成的表,其中可以插入从Excel电子表格复制的内容。 So, for instance having someting like this in the clipboard: 所以,例如在剪贴板中有这样的东西:

1 5

2 2

3 2

And then paste it into something like this, such that every number is in a different cell: 然后将其粘贴到这样的东西中,这样每个数字都在不同的单元格中:

在此输入图像描述

I have the following code for that: 我有以下代码:

from PyQt4 import QtCore, QtGui
import sys


class Ui_MainWindow(QtGui.QMainWindow):
    def __init__(self):
        QtGui.QDialog.__init__(self)
        self.setupUi(self)

    def setupUi(self, MainWindow):
        MainWindow.resize(320, 120)

        self.centralwidget = QtGui.QWidget(MainWindow)
        self.tableWidget = QtGui.QTableWidget(self.centralwidget)

        self.tableWidget.setEnabled(True)
        self.tableWidget.setGeometry(QtCore.QRect(0, 0, 400, 200))
        self.tableWidget.setRowCount(3)
        self.tableWidget.setColumnCount(3)

        MainWindow.setCentralWidget(self.centralwidget)

    if __name__ == '__main__':
        app = QtGui.QApplication(sys.argv)
        ex = Ui_MainWindow()
        ex.show()
        sys.exit(app.exec_())

The problem now is that one has to chose every cell independently, and it is not possible to mark several cells with this widget. 现在的问题是必须独立选择每个单元格,并且不可能使用此小部件标记多个单元格。 Anyone having a suggestion for an alternative widget or an suitable adjustment of this widget? 有人建议使用替代小部件或适当调整此小部件吗?

You'll need to catch the Ctrl-V keyboard event by overriding the keyPressEvent method and processing the clipboard data manually. 您需要通过重写keyPressEvent方法并手动处理剪贴板数据来捕获Ctrl-V键盘事件。

class MyTable(QtGui.QTableWidget):

    def keyPressEvent(self, event):
        if event.key() == QtCore.Qt.Key_V and event.modifiers() == QtCore.Qt.ControlModifier:
            # Check to see if clipboard contains excel data.
            # Manually process clipboard data and insert 
            # it into the table starting from the selected tableitem
            ...
            return
        super(MyTable, self).keyPressEvent(event)

You can use QClipboard and QMimeData to get the excel data from the clipboard. 您可以使用QClipboardQMimeData从剪贴板中获取Excel数据。

from pprint import pprint
clip = QtGui.QApplication.clipboard()
mime = clip.mimeData()
pprint(mime.formats())

You should see something like this: 你应该看到这样的东西:

[u'text/html',  
 u'text/plain', 
 u'application/x-qt-windows-mime;value="Csv"',  
 ...

That last one is the excel data. 最后一个是excel数据。 Excel will place the data in several different formats into the clipbaord, you can check each one to see which format you like best, but CSV is probably the simplest to deal with if you don't care about formatting. Excel会将数据以多种不同的格式放入clipbaord中,您可以检查每个格式以查看您最喜欢的格式,但如果您不关心格式化,则CSV可能是最简单的处理方式。

data = mime.data('application/x-qt-windws-mime;value="Csv"')
# convert from QByteArray to python string
data = str(data.data())
print data

You should see the data you copied in csv format. 您应该看到以csv格式复制的数据。

a,b,c
d,e,f

Then just parse the data and insert it into the table. 然后只需解析数据并将其插入表中。

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

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