从QClipboard复制/粘贴文本会冻结程序

时间:2019-07-29 19:07:40

标签: python pyqt qtablewidget pyside2 qclipboard

我有一个QTableWidget,当单击一行时,它将选择该行中的所有单元格。我试图添加“复制”功能,以便在选择行并将其粘贴到文本编辑器时可以^ ctrl-c。但是,使用我当前的代码,一旦我^ ctrl-c一行,我复制的行就会不断被复制。

我已经在方法“ read_clipboard”中实现了一条打印语句,以查看是否读取了复制的行,这就是我发现该行像在无限循环中一样不断被复制的原因。

以前关于PyQt / Qt和QClipboard的堆栈溢出问题都没有对我有效。

def __init__(self):
   super(MainWindow, self).__init__()
   self.setupUi(self)
   self.my_selector = self.my_tableWidget.selectionModel()

   # Where I detect the signal to call my "read_clipboard" method
   QtGui.QGuiApplication.clipboard().dataChanged.connect(self.read_clipboard)

   self.show()

def read_clipboard(self):
    selection = self.my_selector.selectedIndexes()
    if selection:
        print(selection)
        QtGui.QGuiApplication.clipboard().clear()
        QtGui.QGuiApplication.clipboard().setText(selection)


if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    mainWin = MainWindow()  # Creates MainWindow object
    ret = app.exec_()
    sys.exit(ret)

当我^ ctrl-c行时,程序会连续打印“选择”,就好像它处于无限循环中一样,我不知道在它只运行一次后如何停止它,所以我只能复制它一行。

1 个答案:

答案 0 :(得分:2)

您不应该以这种方式使用dataChanged信号,这有两个原因:

  • 每次剪贴板在整个系统中更改时都会调用它;
  • 清除剪贴板将明显改变其内容,从而导致对read_clipboard方法的递归调用;您显然可以按照@furas的建议暂时断开信号,但是第一个问题仍然存在。

而且,setText不能使用QItemSelectionModel,因为它需要一个字符串。

更好的解决方案是重写自定义QTableWidget类的keyPressEvent,以在默认实现对其执行之前捕获其“复制”操作:

class MyTableWidget(QtWidgets.QTableWidget):
    def keyPressEvent(self, event):
        if event == QtGui.QKeySequence.Copy:
            # set clipboard only if it's not a key repetition
            if not event.isAutoRepeat():
                QtWidgets.QApplication.clipboard().setText(', '.join(i.data() for i in self.selectedIndexes() if i.data()))
        else:
            super(MyTableWidget, self).keyPressEvent(event)

另一种类似的可能性是在表中安装事件过滤器并检查其关键事件:

class MyWindow(QtWidgets.QMainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.setupUi(self)
        self.my_tableWidget.installEventFilter(self)

    def eventFilter(self, source, event):
        if source == self.table and event.type() == QtCore.QEvent.KeyPress and event == QtGui.QKeySequence.Copy:
            if not event.isAutoRepeat():
                QtWidgets.QApplication.clipboard().setText(', '.join(i.data() for i in self.table.selectedIndexes() if i.data()))
            # return True to ensure that the event is not actually propagated
            # to the table widget, nor its parent(s)
            return True
        return super(MainWindow, self).eventFilter(source, event)