如何在线程中使用QDateTime?

问题描述 投票:0回答:1

我有一个线程,我需要执行一个重函数。

首先,我有一个函数从GUI获取两个QDateTime值并将它们转换为UNIX时间戳。其次,“重”功能使用这些值来执行任务。

这两个函数(function_task, time_converter_to_unix)都不属于任何类,所以据我所知我可以在线程中使用它们。

但不是参数,因为我无法访问QDateTime值。

错误:AttributeError:'TaskThread'对象没有属性'startTime'

如何访问QDateTime并从线程中读取内容?谢谢。

编辑:完整的代码。您还可以在以下链接中找到GUI的链接:interface.ui

import sys
import datetime
import time

from PyQt4 import QtCore, QtGui, uic
from PyQt4.QtCore import *
from PyQt4.QtGui import *

# Link to GUI
qtCreatorFile = "interface.ui"

Ui_MainWindow, QtBaseClass = uic.loadUiType(qtCreatorFile)

def time_converter_to_unix(start_datetime, end_datetime):
    # Convert QTimeEdit to UNIX Timestamp (int, msec included), and then to float
    start_datetime_unix_int = start_datetime.toMSecsSinceEpoch ()
    start_datetime_unix = (float(start_datetime_unix_int) / 1000)

    end_datetime_unix_int = end_datetime.toMSecsSinceEpoch ()
    end_datetime_unix = (float(end_datetime_unix_int) / 1000)

    return start_datetime_unix, end_datetime_unix

def dummy_function(self, start_datetime_unix, end_datetime_unix): 
    # Dummy function, just to simulate a task 
    result = start_datetime_unix + end_datetime_unix 
    print result    

class Tool(QtGui.QMainWindow, Ui_MainWindow):
    def __init__(self, parent = None):

        # Setting-ip UI
        QtGui.QMainWindow.__init__(self)
        Ui_MainWindow.__init__(self)
        self.setupUi(self)

        # Button Action
        self.runButton.clicked.connect(self.onStart)

        # Progress Bar and Label. At the begining, the bar is at 0
        self.progressBar.setValue(0)
        self.progressBar.setRange(0,100)
        self.resultLabel.setText("Waiting...")  

        #Thread
        self.myLongTask = TaskThread()
        self.myLongTask.taskFinished.connect(self.onFinished)        

    def onStart(self):      
        # Before running the thread, we set the progress bar in waiting mode
        self.progressBar.setRange(0,0)
        self.resultLabel.setText("In progress...")  

        print "Starting thread..."
        self.myLongTask.start()

    def onFinished(self):
        # Stop the pulsation when the thread has finished
        self.progressBar.setRange(0,1)
        self.progressBar.setValue(1)
        self.resultLabel.setText("Done")  

class TaskThread(QtCore.QThread):  
    taskFinished = QtCore.pyqtSignal()

    def __init__(self):
        QtCore.QThread.__init__(self)

    def __del__(self):
        self.wait()

    def run(self):  
        # First, we read the times from the QDateTime elements in the interface      
        print "Getting times..."
        start_datetime_unix, end_datetime_unix = time_converter_to_unix(self.startTime.dateTime(), self.endTime.dateTime())

        # Then, we put these values in my_function
        print "Executing function..."
        dummy_function(self, start_datetime_unix, end_datetime_unix) 

        # To finish, we execute onFinished.
        print "Finishing thread..."
        self.taskFinished.emit()

def main():
    app = QtGui.QApplication(sys.argv)
    window = Tool()
    window.show()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

如果你和.ui一起执行它,你会看到,一旦我们点击“运行”,进度条就会保持等待模式,并出现上面评论的错误。

python multithreading pyqt pyqt4 qthread
1个回答
0
投票

startTimeendTime属于GUI,而不是线程,这就是你得到这个错误的原因。

另一方面,建议不要从其他线程访问GUI,最好在启动线程之前获取值并将其设置为线程的属性,如下所示:

class Tool(QtGui.QMainWindow, Ui_MainWindow):
    ...       

    def onStart(self):      
        # Before running the thread, we set the progress bar in waiting mode
        self.progressBar.setRange(0,0)
        self.resultLabel.setText("In progress...")  
        self.myLongTask.start_dt = self.startTime.dateTime() # <----
        self.myLongTask.end_dt = self.endTime.dateTime()     # <----

        print "Starting thread..."
        self.myLongTask.start()

    ... 

class TaskThread(QtCore.QThread):  
    ...

    def run(self):  
        # First, we read the times from the QDateTime elements in the interface      
        print "Getting times..."
        start_datetime_unix, end_datetime_unix = time_converter_to_unix(self.start_dt, self.end_dt) # <----

        # Then, we put these values in my_function
        print "Executing function..."
        dummy_function(self, start_datetime_unix, end_datetime_unix) 

        # To finish, we execute onFinished.
        print "Finishing thread..."
        self.taskFinished.emit()
© www.soinside.com 2019 - 2024. All rights reserved.