QClipboard::dataChanged()的插槽被称为twi

2024-04-25 06:14:11 发布

您现在位置:Python中文网/ 问答频道 /正文

QClipboard::dataChanged()的slot detectclipboard url在这段代码中复制Google Chrome地址栏中的url时,有时会调用两次,在win732bit上用PyQt5.7、Python3.5测试,同样在Linux Mint 18上, 虽然我只需要调用插槽一次,但这是一个bug吗?有什么解决办法吗?在

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


class MainWindow(QTableView):

    def __init__(self, parent=None):
        super().__init__(parent)
        self.clipboard = QApplication.clipboard()
        self.clipboard.dataChanged.connect(self.detectClipboardUrl)  

    @pyqtSlot()
    def detectClipboardUrl(self):
        print(self.clipboard.text())



if __name__ == "__main__":
    app = QApplication(sys.argv)
    ui = MainWindow()
    ui.show()
    sys.exit(app.exec_())

Tags: fromimportselfappurlinitdefsys
1条回答
网友
1楼 · 发布于 2024-04-25 06:14:11

如果更改是重复的,可以执行以下操作:

class MainWindow(QTableView):
    def __init__(self, parent=None):
    self.clipboard = QApplication.clipboard()
    self._cb_last = hash(self.clipboard.text())
    self.clipboard.dataChanged.connect(self.detectClipboardUrl)  

    @pyqtSlot()
    def detectClipboardUrl(self):
        text = self.clipboard.text()
        cb_current = hash(text)
        if cb_current != self._cb_last:
            print('clipboard text changed:', text)
            self._cb_last = cb_current

使用hash的原因只是为了避免在内存中保留非常大的字符串。在

或者,如果这两个信号非常接近,您可以使用QTimer来阻止第一个信号在几毫秒内发生的任何更改。在

更新

正如我所怀疑的,这个问题是由Chromium中的一个bug引起的:参见Issue 173691。在

相关问题 更多 >