Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Threading don't creating buttons in frame
Forum Updated to NodeBB v4.3 + New Features

Threading don't creating buttons in frame

Scheduled Pinned Locked Moved Solved Qt for Python
11 Posts 4 Posters 1.2k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Black CatB Offline
    Black CatB Offline
    Black Cat
    wrote on last edited by
    #1

    I created a function to create a button, but, when I call it by python thread doesn't work. btw, when I call it directly it works.

    Is possible to create buttons/items using a Threading?

    import sys
    from PySide2 import QtCore, QtGui, QtWidgets
    from PySide2.QtCore import *
    from PySide2.QtGui import *
    from PySide2.QtWidgets import *
    import threading
    
    
    class Ui_MainWindow(object):
        def setupUi(self, MainWindow):
            if not MainWindow.objectName():
                MainWindow.setObjectName(u"MainWindow")
            MainWindow.resize(800, 600)
            self.centralwidget = QWidget(MainWindow)
            self.centralwidget.setObjectName(u"centralwidget")    
            MainWindow.setCentralWidget(self.centralwidget)
            self.menubar = QMenuBar(MainWindow)
            self.menubar.setObjectName(u"menubar")
            self.menubar.setGeometry(QRect(0, 0, 800, 21))
            MainWindow.setMenuBar(self.menubar)
            self.statusbar = QStatusBar(MainWindow)
            self.statusbar.setObjectName(u"statusbar")
            MainWindow.setStatusBar(self.statusbar)
            QMetaObject.connectSlotsByName(MainWindow)
            MainWindow.setWindowTitle(QCoreApplication.translate("MainWindow", u"MainWindow", None))
    
            # call thread
            self.run()
    
        # starting thread and calling another function
        def run(self):
            print('run?')
            thread = threading.Thread(target=self.execth, args=())
            thread.daemon = True
            thread.start()
    
        # creating the button
        def execth(self):
            print('execth?')
            self.pushButton_2 = QPushButton(self.centralwidget)
            self.pushButton_2.setObjectName(u"pushButton_2")
            self.pushButton_2.setGeometry(QRect(340, 270, 111, 23))
            self.pushButton_2.setText(QCoreApplication.translate("MainWindow", u"background", None))
    
    
    class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
        def __init__(self):
            super(MainWindow, self).__init__()
            self.setupUi(self)
    
    
    if __name__ == "__main__":
        app = QtWidgets.QApplication(sys.argv)
        window = MainWindow()
        window.show()
        sys.exit(app.exec_())
    
    1 Reply Last reply
    0
    • eyllanescE Offline
      eyllanescE Offline
      eyllanesc
      wrote on last edited by eyllanesc
      #2

      @Black-Cat QObjects are not thread-safe, and even more so, QWidgets (which inherit from QObject) should not be created or modified in a thread other than the main thread. On the other hand I don't see any advantage of creating GUI in another thread. What are the benefits of using threads to create GUI elements?

      So: Is it possible to create buttons/items using a Threading? No, you can't and shouldn't.

      If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

      Black CatB 1 Reply Last reply
      1
      • eyllanescE eyllanesc

        @Black-Cat QObjects are not thread-safe, and even more so, QWidgets (which inherit from QObject) should not be created or modified in a thread other than the main thread. On the other hand I don't see any advantage of creating GUI in another thread. What are the benefits of using threads to create GUI elements?

        So: Is it possible to create buttons/items using a Threading? No, you can't and shouldn't.

        Black CatB Offline
        Black CatB Offline
        Black Cat
        wrote on last edited by
        #3

        @eyllanesc because in my original application I have a function that creation some buttons and when the function is turn on my app freezes by some second

        JonBJ eyllanescE 2 Replies Last reply
        0
        • Black CatB Black Cat

          @eyllanesc because in my original application I have a function that creation some buttons and when the function is turn on my app freezes by some second

          JonBJ Online
          JonBJ Online
          JonB
          wrote on last edited by
          #4

          @Black-Cat
          Try not to use threads, and if you do they cannot create or do any UI stuff.

          If you really have to use them, emit a signal for the UI to create widgets if necessary.

          1 Reply Last reply
          1
          • Black CatB Black Cat

            @eyllanesc because in my original application I have a function that creation some buttons and when the function is turn on my app freezes by some second

            eyllanescE Offline
            eyllanescE Offline
            eyllanesc
            wrote on last edited by
            #5

            @Black-Cat Creating some elements of the GUI should not freeze the application so there are 2 options: 1) You are creating many buttons How many buttons are you creating? 2) There is another part of your application that is not creating buttons that is freezing the window.

            If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

            Black CatB 1 Reply Last reply
            0
            • eyllanescE eyllanesc

              @Black-Cat Creating some elements of the GUI should not freeze the application so there are 2 options: 1) You are creating many buttons How many buttons are you creating? 2) There is another part of your application that is not creating buttons that is freezing the window.

              Black CatB Offline
              Black CatB Offline
              Black Cat
              wrote on last edited by
              #6

              @eyllanesc

              only freeze in this process, basically:
              1 - connect in a socket
              2 - if socket response create a green button
              3 - if socket not response create a red button

              (have 1 socket)

              eyllanescE jsulmJ 2 Replies Last reply
              0
              • Black CatB Black Cat

                @eyllanesc

                only freeze in this process, basically:
                1 - connect in a socket
                2 - if socket response create a green button
                3 - if socket not response create a red button

                (have 1 socket)

                eyllanescE Offline
                eyllanescE Offline
                eyllanesc
                wrote on last edited by
                #7

                @Black-Cat So what is freezing the application is the sockets, not the creation of buttons. So you have the following options:

                • Execute the sockets in the secondary thread and use the signals to update the GUI in the main thread.
                • Use QTcpSocket (or similar class) since its methods are asynchronous.

                If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

                1 Reply Last reply
                0
                • Black CatB Black Cat

                  @eyllanesc

                  only freeze in this process, basically:
                  1 - connect in a socket
                  2 - if socket response create a green button
                  3 - if socket not response create a red button

                  (have 1 socket)

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @Black-Cat How do you connect using sockets? I hope you do not use any blocking API? Qt sockets are asynchronous, so nothing should block if you use them properly.

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  Black CatB 1 Reply Last reply
                  1
                  • jsulmJ jsulm

                    @Black-Cat How do you connect using sockets? I hope you do not use any blocking API? Qt sockets are asynchronous, so nothing should block if you use them properly.

                    Black CatB Offline
                    Black CatB Offline
                    Black Cat
                    wrote on last edited by Black Cat
                    #9

                    @jsulm I don't using the QT socket, currently i'm using the socket of python... The freeze was because when a server socket is down it try much of 6 attemps to connect, and it freeze my software.

                    Btw I created the QTitens in normal mode and put the connections of socket in thread..... and don't freeze anymore

                    JonBJ eyllanescE 2 Replies Last reply
                    0
                    • Black CatB Black Cat

                      @jsulm I don't using the QT socket, currently i'm using the socket of python... The freeze was because when a server socket is down it try much of 6 attemps to connect, and it freeze my software.

                      Btw I created the QTitens in normal mode and put the connections of socket in thread..... and don't freeze anymore

                      JonBJ Online
                      JonBJ Online
                      JonB
                      wrote on last edited by JonB
                      #10

                      @Black-Cat
                      It sounds quite possible that the only reason you need threads is because you are choosing to use Python sockets (blocking?) in the first place! If that is the case, you should reconsider.

                      1 Reply Last reply
                      0
                      • Black CatB Black Cat

                        @jsulm I don't using the QT socket, currently i'm using the socket of python... The freeze was because when a server socket is down it try much of 6 attemps to connect, and it freeze my software.

                        Btw I created the QTitens in normal mode and put the connections of socket in thread..... and don't freeze anymore

                        eyllanescE Offline
                        eyllanescE Offline
                        eyllanesc
                        wrote on last edited by
                        #11

                        @Black-Cat See this example https://stackoverflow.com/questions/50413628/using-qlineedit-settext-freezes-the-window-but-background-tasks-works-fine/50415394#50415394

                        If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

                        1 Reply Last reply
                        0

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved