Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Verify internet connection from QThread
Forum Updated to NodeBB v4.3 + New Features

Verify internet connection from QThread

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 242 Views 1 Watching
  • 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.
  • J Offline
    J Offline
    Jalkhov
    wrote on last edited by
    #1

    I have this small application to which I want to implement the threads, since the interface freezes every time it makes the verification, I was investigating and I managed to apply the threads without error but the interface continues freezing every time it makes the verification, the program works with a timer that executes the verification every 5 seconds.

    from PyQt5.QtWidgets import QWidget, QPushButton, QApplication, QListWidget, QGridLayout, QLabel
    from PyQt5.QtCore import QTimer, QDateTime, QThread
    import requests
    import sys
    class CheckThread(QThread):
        def __init__(self):
            super().__init__()
    
        def check(self):
            try:
                request = requests.get("http://www.google.com", timeout=5)
                return True
            except (requests.ConnectionError, requests.Timeout) as exception:
                return False
    
    
    class WinForm(QWidget):
        def __init__(self, parent=None):
            super(WinForm, self).__init__(parent)
            self.setWindowTitle('QTimer example')
    
            self.listFile = QListWidget()
            self.label = QLabel('Label')
            self.startBtn = QPushButton('Start')
            self.endBtn = QPushButton('Stop')
            self.count = 0
    
            layout = QGridLayout()
    
            self.timer = QTimer()
            self.timer.timeout.connect(self.showTime)
    
            layout.addWidget(self.label, 0, 0, 1, 2)
            layout.addWidget(self.startBtn, 1, 0)
            layout.addWidget(self.endBtn, 1, 1)
    
            self.startBtn.clicked.connect(self.startTimer)
            self.endBtn.clicked.connect(self.endTimer)
    
            self.setLayout(layout)
    
        def showTime(self):
            thread = CheckThread()
            self.count += 1
            time = self.count / 10
            self.label.setText(str(time))
    
            if time % 5 == 0:
                print(thread.check())
    
        def startTimer(self):
            self.start = True
            self.timer.start(100)
            self.startBtn.setEnabled(False)
            self.endBtn.setEnabled(True)
    
        def endTimer(self):
            self.count = 0
            self.timer.stop()
            self.startBtn.setEnabled(True)
            self.endBtn.setEnabled(False)
            self.label.setText("0")
    
    
    if __name__ == '__main__':
        app = QApplication(sys.argv)
        form = WinForm()
        form.show()
        sys.exit(app.exec_())
    

    🧠 Learning...

    eyllanescE 1 Reply Last reply
    0
    • J Jalkhov

      I have this small application to which I want to implement the threads, since the interface freezes every time it makes the verification, I was investigating and I managed to apply the threads without error but the interface continues freezing every time it makes the verification, the program works with a timer that executes the verification every 5 seconds.

      from PyQt5.QtWidgets import QWidget, QPushButton, QApplication, QListWidget, QGridLayout, QLabel
      from PyQt5.QtCore import QTimer, QDateTime, QThread
      import requests
      import sys
      class CheckThread(QThread):
          def __init__(self):
              super().__init__()
      
          def check(self):
              try:
                  request = requests.get("http://www.google.com", timeout=5)
                  return True
              except (requests.ConnectionError, requests.Timeout) as exception:
                  return False
      
      
      class WinForm(QWidget):
          def __init__(self, parent=None):
              super(WinForm, self).__init__(parent)
              self.setWindowTitle('QTimer example')
      
              self.listFile = QListWidget()
              self.label = QLabel('Label')
              self.startBtn = QPushButton('Start')
              self.endBtn = QPushButton('Stop')
              self.count = 0
      
              layout = QGridLayout()
      
              self.timer = QTimer()
              self.timer.timeout.connect(self.showTime)
      
              layout.addWidget(self.label, 0, 0, 1, 2)
              layout.addWidget(self.startBtn, 1, 0)
              layout.addWidget(self.endBtn, 1, 1)
      
              self.startBtn.clicked.connect(self.startTimer)
              self.endBtn.clicked.connect(self.endTimer)
      
              self.setLayout(layout)
      
          def showTime(self):
              thread = CheckThread()
              self.count += 1
              time = self.count / 10
              self.label.setText(str(time))
      
              if time % 5 == 0:
                  print(thread.check())
      
          def startTimer(self):
              self.start = True
              self.timer.start(100)
              self.startBtn.setEnabled(False)
              self.endBtn.setEnabled(True)
      
          def endTimer(self):
              self.count = 0
              self.timer.stop()
              self.startBtn.setEnabled(True)
              self.endBtn.setEnabled(False)
              self.label.setText("0")
      
      
      if __name__ == '__main__':
          app = QApplication(sys.argv)
          form = WinForm()
          form.show()
          sys.exit(app.exec_())
      
      eyllanescE Offline
      eyllanescE Offline
      eyllanesc
      wrote on last edited by eyllanesc
      #2

      @Jalkhov Use Qt Signals:

      class CheckThread(QThread):
          statusChanged = pyqtSignal(bool)
      
          def run(self):
              try:
                  request = requests.get("http://www.google.com", timeout=5)
                  self.statusChanged.emit(True)
              except (requests.ConnectionError, requests.Timeout) as exception:
                  self.statusChanged.emit(False)
      
      class WinForm(QWidget):
          def __init__(self, parent=None):
              super(WinForm, self).__init__(parent)
              self.setWindowTitle("QTimer example")
      
              self.startBtn = QPushButton("Start")
              layout = QGridLayout(self)
      
              layout.addWidget(self.label)
              layout.addWidget(self.startBtn)
      
              self.check_thread = CheckThread()
              self.check_thread.statusChanged.connect(self.handle_status_changed)
      
              self.startBtn.clicked.connect(self.start_thread)
      
          def start_thread(self):
              self.check_thread.start()
      
          def handle_status_changed(self, status):
              print(status)
      

      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
      1
      • J Offline
        J Offline
        Jalkhov
        wrote on last edited by Jalkhov
        #3

        Ok, I have adapted the code, it would look like this?

        from PyQt5.QtWidgets import QWidget, QPushButton, QApplication, QListWidget, QGridLayout, QLabel
        from PyQt5.QtCore import QTimer, QDateTime, QThread, pyqtSignal
        import requests
        import sys
        class CheckThread(QThread):
            statusChanged = pyqtSignal(bool)
        
            def run(self):
                try:
                    request = requests.get("http://www.google.com", timeout=5)
                    self.statusChanged.emit(True)
                except (requests.ConnectionError, requests.Timeout) as exception:
                    self.statusChanged.emit(False)
        
        
        class WinForm(QWidget):
            def __init__(self, parent=None):
                super(WinForm, self).__init__(parent)
                self.setWindowTitle('QTimer example')
        
                self.listFile = QListWidget()
                self.label = QLabel('Label')
                self.startBtn = QPushButton('Start')
                self.endBtn = QPushButton('Stop')
                self.count = 0
        
                layout = QGridLayout()
        
                self.timer = QTimer()
                self.timer.timeout.connect(self.showTime)
        
                layout.addWidget(self.label, 0, 0, 1, 2)
                layout.addWidget(self.startBtn, 1, 0)
                layout.addWidget(self.endBtn, 1, 1)
        
                # self.startBtn.clicked.connect(self.startTimer)
                self.endBtn.clicked.connect(self.endTimer)
        
                self.setLayout(layout)
        
                self.check_thread = CheckThread()
                self.check_thread.statusChanged.connect(self.handle_status_changed)
        
                self.startBtn.clicked.connect(self.start_thread)
        
            def start_thread(self):
        
                self.start = True
                self.timer.start(100)
                self.startBtn.setEnabled(False)
                self.endBtn.setEnabled(True)
        
            def handle_status_changed(self, status):
                print(status)
        
            def showTime(self):
                self.count += 1
                time = self.count / 10
                self.label.setText(str(time))
                if time % 5 == 0:
                    self.check_thread.start()
        
            def endTimer(self):
                self.count = 0
                self.timer.stop()
                self.startBtn.setEnabled(True)
                self.endBtn.setEnabled(False)
                self.label.setText("0")
        
        if __name__ == '__main__':
            app = QApplication(sys.argv)
            form = WinForm()
            form.show()
            sys.exit(app.exec_())
        

        🧠 Learning...

        J 1 Reply Last reply
        0
        • J Jalkhov

          Ok, I have adapted the code, it would look like this?

          from PyQt5.QtWidgets import QWidget, QPushButton, QApplication, QListWidget, QGridLayout, QLabel
          from PyQt5.QtCore import QTimer, QDateTime, QThread, pyqtSignal
          import requests
          import sys
          class CheckThread(QThread):
              statusChanged = pyqtSignal(bool)
          
              def run(self):
                  try:
                      request = requests.get("http://www.google.com", timeout=5)
                      self.statusChanged.emit(True)
                  except (requests.ConnectionError, requests.Timeout) as exception:
                      self.statusChanged.emit(False)
          
          
          class WinForm(QWidget):
              def __init__(self, parent=None):
                  super(WinForm, self).__init__(parent)
                  self.setWindowTitle('QTimer example')
          
                  self.listFile = QListWidget()
                  self.label = QLabel('Label')
                  self.startBtn = QPushButton('Start')
                  self.endBtn = QPushButton('Stop')
                  self.count = 0
          
                  layout = QGridLayout()
          
                  self.timer = QTimer()
                  self.timer.timeout.connect(self.showTime)
          
                  layout.addWidget(self.label, 0, 0, 1, 2)
                  layout.addWidget(self.startBtn, 1, 0)
                  layout.addWidget(self.endBtn, 1, 1)
          
                  # self.startBtn.clicked.connect(self.startTimer)
                  self.endBtn.clicked.connect(self.endTimer)
          
                  self.setLayout(layout)
          
                  self.check_thread = CheckThread()
                  self.check_thread.statusChanged.connect(self.handle_status_changed)
          
                  self.startBtn.clicked.connect(self.start_thread)
          
              def start_thread(self):
          
                  self.start = True
                  self.timer.start(100)
                  self.startBtn.setEnabled(False)
                  self.endBtn.setEnabled(True)
          
              def handle_status_changed(self, status):
                  print(status)
          
              def showTime(self):
                  self.count += 1
                  time = self.count / 10
                  self.label.setText(str(time))
                  if time % 5 == 0:
                      self.check_thread.start()
          
              def endTimer(self):
                  self.count = 0
                  self.timer.stop()
                  self.startBtn.setEnabled(True)
                  self.endBtn.setEnabled(False)
                  self.label.setText("0")
          
          if __name__ == '__main__':
              app = QApplication(sys.argv)
              form = WinForm()
              form.show()
              sys.exit(app.exec_())
          
          J Offline
          J Offline
          Jalkhov
          wrote on last edited by
          #4

          @Jalkhov said in Verify internet connection from QThread:

          Ok, I have adapted the code, it would look like this?

          from PyQt5.QtWidgets import QWidget, QPushButton, QApplication, QListWidget, QGridLayout, QLabel
          from PyQt5.QtCore import QTimer, QDateTime, QThread, pyqtSignal
          import requests
          import sys
          class CheckThread(QThread):
              statusChanged = pyqtSignal(bool)
          
              def run(self):
                  try:
                      request = requests.get("http://www.google.com", timeout=5)
                      self.statusChanged.emit(True)
                  except (requests.ConnectionError, requests.Timeout) as exception:
                      self.statusChanged.emit(False)
          
          
          class WinForm(QWidget):
              def __init__(self, parent=None):
                  super(WinForm, self).__init__(parent)
                  self.setWindowTitle('QTimer example')
          
                  self.listFile = QListWidget()
                  self.label = QLabel('Label')
                  self.startBtn = QPushButton('Start')
                  self.endBtn = QPushButton('Stop')
                  self.count = 0
          
                  layout = QGridLayout()
          
                  self.timer = QTimer()
                  self.timer.timeout.connect(self.showTime)
          
                  layout.addWidget(self.label, 0, 0, 1, 2)
                  layout.addWidget(self.startBtn, 1, 0)
                  layout.addWidget(self.endBtn, 1, 1)
          
                  # self.startBtn.clicked.connect(self.startTimer)
                  self.endBtn.clicked.connect(self.endTimer)
          
                  self.setLayout(layout)
          
                  self.check_thread = CheckThread()
                  self.check_thread.statusChanged.connect(self.handle_status_changed)
          
                  self.startBtn.clicked.connect(self.start_thread)
          
              def start_thread(self):
          
                  self.start = True
                  self.timer.start(100)
                  self.startBtn.setEnabled(False)
                  self.endBtn.setEnabled(True)
          
              def handle_status_changed(self, status):
                  print(status)
          
              def showTime(self):
                  self.count += 1
                  time = self.count / 10
                  self.label.setText(str(time))
                  if time % 5 == 0:
                      self.check_thread.start()
          
              def endTimer(self):
                  self.count = 0
                  self.timer.stop()
                  self.startBtn.setEnabled(True)
                  self.endBtn.setEnabled(False)
                  self.label.setText("0")
          
          if __name__ == '__main__':
              app = QApplication(sys.argv)
              form = WinForm()
              form.show()
              sys.exit(app.exec_())
          

          I placed

          self.check_thread.start()
          

          inside the showtime function because that way it is shown at the time interval indicated by the if, I don't know if it is right like that.

          🧠 Learning...

          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