-1

I want to change the Window Title of my mainwindoThread. In debug mode it works but not when I start the program normal. I think i have to use Qmutex but I am very new in these topic. I tried it with Qmute but I dont know right how to lock and what I have to lock.

In my Mainwindow I execute this line:

self.menuprojects = class_menuprojects(self)

The class will be successfully creaed and on click_insertproject I will execute this:

def click_insertproject(self):

    thread = generate_insert_frame(self.MainWindow)  
    thread.start()

And now I want to change my mainthrea mainwindow title to "test"

class generate_insert_frame(QThread):
   def __init__(self, MainWindow):

    QThread.__init__(self)
    self.mutex = QMutex()
    self.MainWindow = MainWindow

   def run(self):
    self.mutex.lock()
    self.MainWindow.setWindowTitle("Test")
    self.mutex.unlock()
Robin
  • 13
  • 2
  • As a side note from what little you did share you are improperly using QThread as one should not sub-class the QThread due to how it works that and I do not see any Signals/Slots which is how one communicates properly across thread boundaries – Dennis Jensen Sep 19 '19 at 13:59

1 Answers1

0

Data is transferred from the external thread to the GUI using signals.

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

class generate_insert_frame(QThread):

    threadSignal = pyqtSignal(str)                             # <<---

    def __init__(self, num=1):
        super().__init__()
        self.num = num

    def run(self):
        self.threadSignal.emit("Test {}".format(self.num))     # <<---
        self.msleep(100)
        self.stop()

    def stop(self):
        self.quit()


class MyWindow(QWidget):   
    def __init__ (self):
        super().__init__ ()
        self.setWindowTitle("MyWindow")

        button = QPushButton("Start Thread")
        button.clicked.connect(self.click_insertproject)
        grid = QGridLayout(self)
        grid.addWidget(button)
        self.num = 1

    def click_insertproject(self):
        self.thread = generate_insert_frame(self.num) 
        self.thread.threadSignal.connect(self.setWindowTitle)    # <<---
        self.thread.start()
        self.num += 1

if __name__ == "__main__":
    import sys
    app = QApplication(sys.argv)
    window = MyWindow()
    window.resize(400, 200)
    window.show()
    sys.exit(app.exec_())      

enter image description here

S. Nick
  • 12,879
  • 8
  • 25
  • 33