相关疑难解决方法(0)

PyQt:将信号连接到插槽以启动后台操作

我有以下代码scan_value在更新ui(progress)中的进度条时执行后台操作().scan_value迭代某个值obj,value_changed每次更改值时发出signal().由于这里不相关的原因,我必须将它包装Scanner在另一个线程的object()中.当一个按钮扫描仪被称为scanclicked.这是我的问题...以下代码工作正常(即进度条按时更新).

# I am copying only the relevant code here.

def update_progress_bar(new, old):
    fraction = (new - start) / (stop - start)
    progress.setValue(fraction * 100)

obj.value_changed.connect(update_progress_bar)

class Scanner(QObject):

    def scan(self):
        scan_value(start, stop, step)
        progress.setValue(100)

thread = QThread()
scanner = Scanner()
scanner.moveToThread(thread)
thread.start()

scan.clicked.connect(scanner.scan)
Run Code Online (Sandbox Code Playgroud)

但如果我将最后一部分更改为:

thread = QThread()
scanner = Scanner()
scan.clicked.connect(scanner.scan) # This was at the end!
scanner.moveToThread(thread)
thread.start()
Run Code Online (Sandbox Code Playgroud)

进度条仅在最后更新(我的猜测是所有内容都在同一个线程上运行).如果在将对象接收对象移动到线程之前将信号连接到插槽,是否应该无关紧要.

python multithreading pyqt signals-slots qthread

9
推荐指数
1
解决办法
6999
查看次数

pyqtSlot装饰器为什么会导致“ TypeError:connect()失败”?

我有这个带有PyQt5的Python 3.5.1程序,以及一个从QtCreator ui文件创建的GUI,其中pyqtSlot装饰器导致“ TypeError:在textChanged(QString)和edited()之间发生connect()失败”。

在重现该问题的示例代码中,我有2个自定义类:MainApp和LineEditHandler。MainApp实例化主GUI(来自文件“ mainwindow.ui”),并且LineEditHandler处理QLineEdit对象。LineEditHandler存在的原因是将与QLineEdit对象主要相关的自定义方法集中在一个类中。它的构造函数需要QLineEdit对象和MainApp实例(在需要时访问其他对象/属性)。

在MainApp中,我将QLineEdit的textChanged信号连接到LineEditHandler.edited()。如果我不使用pyqtSlot()装饰LineEditHandler.edited(),则一切正常。如果我确实对方法使用@pyqtSlot(),则代码运行将失败,并显示“ TypeError:textChanged(QString)和edited()之间的connect()失败”。我在这里做错什么了吗?

您可以在以下位置获取mainwindow.ui文件:https ://drive.google.com/file/d/0B70NMOBg3HZtUktqYVduVEJBN2M/view

这是产生问题的示例代码:

import sys
from PyQt5 import uic
from PyQt5.QtWidgets import *
from PyQt5.QtCore import pyqtSlot


Ui_MainWindow, QtBaseClass = uic.loadUiType("mainwindow.ui")


class MainApp(QMainWindow, Ui_MainWindow):

    def __init__(self):
        # noinspection PyArgumentList
        QMainWindow.__init__(self)
        Ui_MainWindow.__init__(self)
        self.setupUi(self)

        # Instantiate the QLineEdit handler.
        self._line_edit_handler = LineEditHandler(self, self.lineEdit)
        # Let the QLineEdit handler deal with the QLineEdit textChanged signal.
        self.lineEdit.textChanged.connect(self._line_edit_handler.edited)


class LineEditHandler:

    def __init__(self, main_window, line_edit_obj):
        self._line_edit = line_edit_obj
        self._main_window = main_window

    # FIXME The pyqtSlot decorator causes …
Run Code Online (Sandbox Code Playgroud)

qstring connect typeerror python-3.x pyqt5

4
推荐指数
1
解决办法
3545
查看次数

pyqtSlot的功能

我刚刚从这里阅读了有关pyqt5按钮的教程。和代码如下。有一个关于button.clicked.connect(self.on_click)和的问题@pyqtSlot()。如果我@pyqtSlot()从代码中删除,它仍然可以工作。但是,如果我button.clicked.connect(self.on_click)从代码中删除,该按钮将不起作用。那么@pyqtSlot()这段代码的功能是什么?

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import pyqtSlot

class App(QWidget):

    def __init__(self):
        super().__init__()
        self.title = 'PyQt5 button - pythonspot.com'
        self.left = 10
        self.top = 10
        self.width = 320
        self.height = 200
        self.initUI()

    def initUI(self):
        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height)

        button = QPushButton('PyQt5 button', self)
        button.setToolTip('This is an example button')
        button.move(100,70) 
        button.clicked.connect(self.on_click)

        self.show()

    @pyqtSlot()
    def on_click(self):
        print('PyQt5 button click')

if …
Run Code Online (Sandbox Code Playgroud)

python pyqt pyqt5 qtcore

1
推荐指数
1
解决办法
8434
查看次数