0

i am trying to use QThread() in my application but my problem is that i don't want to run all the method in the AnotherThread so when i call the AnotherThread methods the applcation freeze completely just like i am not using threading this is my AnotherThread class:

class AnotherThread(QtCore.QThread):
    def __init__(self, parent=None):
        super().__init__(parent)

    progresssignal = QtCore.pyqtSignal(int)
    datasignal = QtCore.pyqtSignal(str)

    def run(self):
        print('Thread Started')

    def startloop(self):
        for i in range(100):
            time.sleep(0.2)
            self.progresssignal.emit(i)

    def getdata(self):
        url = 'https://newsapi.org/v2/top-headlines?sources=fox-sports&apiKey=7aedc11bc60c4fbe83fe55e4f7c56370'
        news = requests.get(url).json()
        for new in news['articles']:
            if new['title']:
                self.datasignal.emit(new['title'] + '\n \n')

and this is my dialog class:

class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(450, 450)
        Dialog.setMinimumSize(QtCore.QSize(450, 450))
        Dialog.setMaximumSize(QtCore.QSize(450, 450))
        self.start_loop = QtWidgets.QPushButton(Dialog)
        self.start_loop.setGeometry(QtCore.QRect(10, 10, 200, 30))
        self.start_loop.setObjectName("start_loop")
        self.progress = QtWidgets.QProgressBar(Dialog)
        self.progress.setGeometry(QtCore.QRect(10, 410, 430, 30))
        self.progress.setProperty("value", 0)
        self.progress.setObjectName("progress")
        self.get_data = QtWidgets.QPushButton(Dialog)
        self.get_data.setGeometry(QtCore.QRect(240, 10, 200, 30))
        self.get_data.setObjectName("get_data")
        self.data_result = QtWidgets.QTextBrowser(Dialog)
        self.data_result.setGeometry(QtCore.QRect(10, 50, 430, 350))
        self.data_result.setObjectName("data_result")

        self.retranslateUi(Dialog)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "News Getter"))
        self.start_loop.setText(_translate("Dialog", "Start Loop"))
        self.get_data.setText(_translate("Dialog", "Get Data"))

    def ops(self):
        self.worker = AnotherThread()
        self.worker.start()
        self.start_loop.clicked.connect(lambda: self.worker.startloop())
        self.get_data.clicked.connect(lambda: self.worker.getdata())
        self.worker.progresssignal.connect(self.progress.setValue)
        self.worker.datasignal.connect(self.data_result.append)

I want to run methods from AnotherThread just when call them from Ui_Dialog class i dont want to run them when i call self.worker.start()

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Derar
  • 97
  • 14
  • What part do you want to run on the other thread and why? – eyllanesc Jul 01 '18 at 05:26
  • my idea is to start any method from AnotherThread class but just when i call it not when starting the thread but i want it to run in the other thread – Derar Jul 01 '18 at 05:39
  • 1
    If you want your thread to start, and respond when people do something in the UI (like push a button), see the answer I wrote on the possible duplicate I shared above. – three_pineapples Jul 01 '18 at 05:56

0 Answers0