Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Language Bindings
  4. [Solved]problem of making a Timer with QLCDNumber
Forum Updated to NodeBB v4.3 + New Features

[Solved]problem of making a Timer with QLCDNumber

Scheduled Pinned Locked Moved Language Bindings
10 Posts 3 Posters 7.4k 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.
  • R Offline
    R Offline
    redstoneleo
    wrote on 15 Mar 2013, 03:01 last edited by
    #1

    the following code just makes a Timer with QLCDNumber

    what I want to do is to show a message dialog when 4s has elapsed since the startup of this program .however when the message dialog pops up , the time on QLCDNumber doesn’t change ,what should I do to make this program still timing even when the message dialog pops up ?
    how to write the code ?
    @
    import sys
    from PyQt4.QtGui import *
    from PyQt4.QtCore import *

    class MainWindow(QWidget):
    def init(self, parent=None):
    super(MainWindow, self).init(parent)
    self.resize(800,600)

        self.lcdNumber = QLCDNumber()
        self.lcdNumber.setNumDigits(8)
        
        layout =  QVBoxLayout(self)
        layout.addWidget(self.lcdNumber)
        
        self.currentTime = QTime(0,0,0)
        self.lcdNumber.display(self.currentTime.toString('hh:mm:ss'))
        
        self.timer = QTimer(self)
        self.timer.timeout.connect(self.updateLcdNumberContent)
        self.timer.start(1000)
        
    def updateLcdNumberContent(self):
        
        self.currentTime = self.currentTime.addSecs(1)
        self.lcdNumber.display(self.currentTime.toString('hh:mm:ss'))
    
    
        if self.currentTime == QTime(0,0,4) :
            msgBox = QMessageBox()
            msgBox.setWindowTitle('iTimer')
            msgBox.setIcon (QMessageBox.Information)
            msgBox.setText("Time Out !!")
            
            stopButton = msgBox.addButton("Stop", QMessageBox.ActionRole)
            ignoreButton = msgBox.addButton(QMessageBox.Ignore)
            
            stopButton.clicked.connect(self.timer.stop)
          
    
            msgBox.show()
    

    msgBox.exec_()

    if name == 'main':
    app =QApplication(sys.argv)
    frame = MainWindow()
    frame.show()
    sys.exit(app.exec_())

    @

    1 Reply Last reply
    0
    • J Offline
      J Offline
      jazzycamel
      wrote on 15 Mar 2013, 09:06 last edited by
      #2

      The lcd stops updating when you open the dialog with exec_() because the dialogs event loop blocks the main event loop. Therefore you need to use show() and give the QMessageBox your QMainWindow as a parent. Also, calling raise_() will make your dialog the active window (necessary on Mac OSX at least).

      Working example:

      @
      from PyQt4.QtGui import *
      from PyQt4.QtCore import *

      class MainWindow(QWidget):
      def init(self, parent=None):
      QMainWindow.init(self, parent)

          self.resize(800,600)
         
          self.lcdNumber=QLCDNumber(self)
          self.lcdNumber.setNumDigits(8)
         
          QVBoxLayout(self).addWidget(self.lcdNumber)
         
          self.currentTime=QTime(0,0,0)
          self.lcdNumber.display(self.currentTime.toString('hh:mm:ss'))
          self.timerId=self.startTimer(1000)
      
      def timerEvent(self, event):
          if not event.timerId()==self.timerId: return
      
          self.currentTime=self.currentTime.addSecs(1)
          self.lcdNumber.display(self.currentTime.toString('hh:mm:ss'))        
      
          if self.currentTime==QTime(0,0,4):
              msgBox=QMessageBox(
                  QMessageBox.Information,
                  "iTimer",
                  "Time Out!",
                  parent=self)
             
              stopButton=msgBox.addButton("Stop", QMessageBox.ActionRole)
              ignoreButton=msgBox.addButton(QMessageBox.Ignore)
              stopButton.clicked.connect(lambda: self.killTimer(self.timerId))
            
              msgBox.show()
              msgBox.raise_()
      

      if name == 'main':
      from sys import argv, exit

      app=QApplication(argv)
      frame=MainWindow()
      frame.show()
      frame.raise_()
      exit(app.exec_())
      

      @

      Hope this helps ;o)

      For the avoidance of doubt:

      1. All my code samples (C++ or Python) are tested before posting
      2. As of 23/03/20, my Python code is formatted to PEP-8 standards using black from the PSF (https://github.com/psf/black)
      1 Reply Last reply
      0
      • R Offline
        R Offline
        redstoneleo
        wrote on 16 Mar 2013, 04:26 last edited by
        #3

        thanks for your reply ,but now I have another 2 questions both within my code .
        1)if QmessageBox’s parent is None , show() shows QmessageBox only for a second ,then it disappeared immediately .why this issue occurs ?
        2)what does giving the QMessageBox my QMainWindow as a parent count ?

        1 Reply Last reply
        0
        • J Offline
          J Offline
          jazzycamel
          wrote on 24 Mar 2013, 10:28 last edited by
          #4

          I imagine the answer to 1) is that your QMessageBox goes out of context after show() is called as it is not an attribute of your QMainWindow instance, therefore it is drawn and then garbage collected. As to 2): giving a widget a parent in Qt causes it to be owned by that parent which is generally preferable. WRT to QMessageBox, giving it the QMainWindow as a parent associates it with that window and allows for proper behaviour WRT to modality, positioning, window icon etc. Also, in this case, giving the QMessageBox a parent keeps it in context (see answer to 1)).

          For the avoidance of doubt:

          1. All my code samples (C++ or Python) are tested before posting
          2. As of 23/03/20, my Python code is formatted to PEP-8 standards using black from the PSF (https://github.com/psf/black)
          1 Reply Last reply
          0
          • R Offline
            R Offline
            redstoneleo
            wrote on 25 Mar 2013, 00:47 last edited by
            #5

            some other people says that giving the QMessageBox my QMainWindow as a parent creating a non-blocking message box.

            I checked the doc ,only found "The parent argument, if not None, causes self to be owned by Qt instead of PyQt." is this two the same mean ?

            1 Reply Last reply
            0
            • J Offline
              J Offline
              jazzycamel
              wrote on 25 Mar 2013, 09:23 last edited by
              #6

              The 'blocking' nature of a widget has nothing to do with the parent. If your start a dialog with exec() it starts its own event loop, blocking the main event loop created by QApplication(). If a dialog is started with show() it uses the main event loop and the method returns immediately (i.e. it doesn't block). Whether a widget is owned by Qt or the PyQt bindings is, in this case anyway, irrelevant.

              For the avoidance of doubt:

              1. All my code samples (C++ or Python) are tested before posting
              2. As of 23/03/20, my Python code is formatted to PEP-8 standards using black from the PSF (https://github.com/psf/black)
              1 Reply Last reply
              0
              • A Offline
                A Offline
                andre
                wrote on 25 Mar 2013, 10:56 last edited by
                #7

                And then, next to show and exec, there is also open. That one also returns immediately, but will make the dialog modal just like exec does.

                1 Reply Last reply
                0
                • R Offline
                  R Offline
                  redstoneleo
                  wrote on 28 Mar 2013, 03:25 last edited by
                  #8

                  I have another question to ask : . If the active window belongs to some other process,how to make the QMessageBox or QmainWindow in front of any overlapping sibling widgets when timeout

                  I tried raise_() and activateWindow() ,but both don’t work on WinXP

                  1 Reply Last reply
                  0
                  • J Offline
                    J Offline
                    jazzycamel
                    wrote on 28 Mar 2013, 15:14 last edited by
                    #9

                    When you say "other process" do you mean an application other than your own? For example, a web browser window has been opened on top of your application? If this is the case, the simple answer to your question is that AFAIK you can't and if even if you could, you shouldn't as this would be incredibly annoying from a user perspective. What will happen is that the task bar will signal to the user via the task bar (usually by changing the colour of or flashing you apps entry) that something requires attention (your message box in this case).

                    For the avoidance of doubt:

                    1. All my code samples (C++ or Python) are tested before posting
                    2. As of 23/03/20, my Python code is formatted to PEP-8 standards using black from the PSF (https://github.com/psf/black)
                    1 Reply Last reply
                    0
                    • R Offline
                      R Offline
                      redstoneleo
                      wrote on 5 Apr 2013, 05:53 last edited by
                      #10

                      [quote author="jazzycamel" date="1364483680"]When you say "other process" do you mean an application other than your own? For example, a web browser window has been opened on top of your application? If this is the case, the simple answer to your question is that AFAIK you can't and if even if you could, you shouldn't as this would be incredibly annoying from a user perspective. What will happen is that the task bar will signal to the user via the task bar (usually by changing the colour of or flashing you apps entry) that something requires attention (your message box in this case).[/quote]
                      yes,that's what I mean.thanks 4 your tips

                      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