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. setVisible() doesn't occur immediately
Qt 6.11 is out! See what's new in the release blog

setVisible() doesn't occur immediately

Scheduled Pinned Locked Moved Solved Qt for Python
2 Posts 2 Posters 951 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.
  • E Offline
    E Offline
    efremdan1
    wrote on last edited by efremdan1
    #1

    I have a dialog box which processes a lengthy calculation when the user clicks Ok. I've placed a hidden label above the Ok button to inform the user that they need to wait, and I'd like to make that label visible when the user clicks Ok. However, setVisible() doesn't seem to take effect until after the lengthy calculation (immediately before the dialog box closes). Any idea why this is or how to fix it? Minimal working example below.

    import sys
    import time
    from PySide2.QtWidgets import *
    
    class CustomDialog(QDialog):
        def __init__(self, parent=None):
            super().__init__(parent=parent)
    
            self.layout = QVBoxLayout()
            self.message = QLabel("Perform lengthy calculation.")
            self.layout.addWidget(self.message)
    
            self.warning = QLabel("Please wait.")
            self.layout.addWidget(self.warning)
            self.warning.setVisible(False)
    
            QBtn = QDialogButtonBox.Ok | QDialogButtonBox.Cancel
            self.buttonBox = QDialogButtonBox(QBtn)
            self.buttonBox.accepted.connect(self.accept) # Comment out this line to see that the warning label does appear, but not until after the lengthy calculation
            self.buttonBox.rejected.connect(self.reject)
    
            self.layout.addWidget(self.buttonBox)
            self.setLayout(self.layout)
    
            self.buttonBox.button(QDialogButtonBox.Ok).clicked.connect(self.onButtonBoxOkClicked)
    
        def onButtonBoxOkClicked(self):
            self.warning.setVisible(True) # We set the warning label to visible before the lengthy calculation
            time.sleep(5)
    
    
    class MainWindow(QMainWindow):
        def __init__(self):
            super().__init__()
            button = QPushButton("Press me for the dialog.")
            button.clicked.connect(self.button_clicked)
            self.setCentralWidget(button)
    
        def button_clicked(self, s):
            dlg = CustomDialog()
            dlg.exec_()
    
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    app.exec_()
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      setVisible will schedule an update to happen when the event loop can do it. Even loop that you block with your lengthy calculation. You might want to consider using QtConcurrent::run to avoid blocking your GUI.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      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