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. QDialog in QMainwinndow and make the background dark
Forum Update on Monday, May 27th 2025

QDialog in QMainwinndow and make the background dark

Scheduled Pinned Locked Moved Unsolved Qt for Python
pythonqmainwindowqdialogchildwidget
6 Posts 2 Posters 1.9k 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.
  • EmrecpE Offline
    EmrecpE Offline
    Emrecp
    wrote on last edited by Emrecp
    #1

    Hello,
    I have design:
    QDialog.png
    I want to create a custom dialog class that takes the central widget as a parameter. And it will show itself and it will make the background dark. Same time, of course not clickable out of the custom dialog.
    My Code:

    # -*- coding: utf-8 -*-
    import sys, os, time
    from PySide6 import QtCore, QtWidgets, QtGui
    from PySide6.QtWidgets import *
    from PySide6.QtCore import *
    from PySide6.QtGui import *
    
    class EDialogData(object):
        AnimationTime = 500  # ms
        FontSize, FontSpacing = 16, 0
        Color = {
            "CORNER": QColor(230, 230, 233),  # QColor(239, 239, 239),
            "BASE_BACKGROUND": QColor(247, 247, 250),
            "BASE_FOREGROUND": QColor(255, 125, 51),
    
            "BASE_DISABLED_BACKGROUND": QColor(188, 188, 188),
    
            "BASE_ACTIVE_BACKGROUND": QColor(255, 125, 51),
            "BASE_ACTIVE_FOREGROUND": QColor(Qt.white)
        }
        Size = QSize(640,480)
        Radius = 14
    
    
    
    class EDialog(QDialog):
        DialogData = EDialogData()
    
        def __init__(self, DialogData=EDialogData(), CENTER_WIDGET=None, parent=None):
            super(EDialog, self).__init__(None)
            self.DialogData, self.CENTER_WIDGET, self.parent = DialogData, CENTER_WIDGET, parent
            self.setWindowFlags(Qt.FramelessWindowHint) # for no control boxes
            self.setAttribute(Qt.WA_TranslucentBackground) # for radius
    
            self.MyStyleSheet = f"""QDialog{{
                                               background-color:{EDialogData.Color['BASE_BACKGROUND'].name()};
                                               border:1px solid {EDialogData.Color['CORNER'].name()}; 
                                               border-radius: {EDialogData.Radius}px;
                                           }}
                                """
            self.setParent(self.parent)
            self.setStyleSheet(self.MyStyleSheet)
            self.resize(self.DialogData.Size)
    
            self.move(int((self.parent.width()- self.width()) /2),
                      int((self.parent.height()- self.height()) /2))
            self.setWindowFlags(Qt.WindowStaysOnTopHint)
            self.show()
    
    if True:
        if __name__ == "__main__":
            app = QApplication(sys.argv)
            wind = QMainWindow()
            wind.setStyleSheet("QMainWindow{background-color:rgb(0,0,255);}")  # 247,247,250)}")
            wind.setWindowTitle("EDialog")
            wind.resize(1000, 650)
            centerwid_mainwindow = QWidget()
            centerlay_mainwindow = QVBoxLayout(centerwid_mainwindow);centerlay_mainwindow.setAlignment(Qt.AlignCenter)
    
            Data = EDialogData()
    
            widDialog = QWidget(); # CENTER_WIDGET
            layDialog = QVBoxLayout(widDialog)
            layDialog.addWidget(QPushButton("dialog button 1"))
            layDialog.addWidget(QPushButton("dialog button 2"))
    
            e = EDialog(Data, widDialog,parent=wind)
    
            table = QTableWidget()
            table.insertColumn(0); table.insertColumn(1);table.insertColumn(2);table.insertRow(0);table.insertRow(1);table.insertRow(2)
    
            centerlay_mainwindow.addWidget(table);centerlay_mainwindow.addWidget(QPushButton("button"));centerlay_mainwindow.addWidget(QTextEdit("text"))
    
    
            wind.setCentralWidget(centerwid_mainwindow)
            wind.show()
            sys.exit(app.exec())
    

    Thanks!

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      If starting with a default dialog, pass it a parent will make it modal to the parent, and thus forbid interaction with said parent. Depending on the platform, the parent will either look disabled and/or dimmed a bit.

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

      EmrecpE 1 Reply Last reply
      1
      • SGaistS SGaist

        Hi,

        If starting with a default dialog, pass it a parent will make it modal to the parent, and thus forbid interaction with said parent. Depending on the platform, the parent will either look disabled and/or dimmed a bit.

        EmrecpE Offline
        EmrecpE Offline
        Emrecp
        wrote on last edited by
        #3

        @SGaist Hello thanks for reply, Hi, thanks for reply. I hadn't thought of using exec() function. But if parent widget has a layout, my dialog is not showing. Can you share demo? I would be very grateful, Thanks.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Something like:

          class MyWidget(QWidget):
              def __init__(self, parent=None) -> None:
                  super().__init__(parent)
                  
                  button = QPushButton(self.tr("Click me"))
                  layout = QVBoxLayout(self)
                  layout.addWidget(button)
                  button.clicked.connect(self.showDialog)
          
              def showDialog(self) -> None:
                  dialog = QDialog(self)
                  dialog.exec()
          
          if __name__ == "__name__":
              app = QApplication(sys.argv)
              widget = MyWidget()
              widget.show()
              sys.exit(app.exec())
          

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

          EmrecpE 1 Reply Last reply
          0
          • SGaistS SGaist

            Something like:

            class MyWidget(QWidget):
                def __init__(self, parent=None) -> None:
                    super().__init__(parent)
                    
                    button = QPushButton(self.tr("Click me"))
                    layout = QVBoxLayout(self)
                    layout.addWidget(button)
                    button.clicked.connect(self.showDialog)
            
                def showDialog(self) -> None:
                    dialog = QDialog(self)
                    dialog.exec()
            
            if __name__ == "__name__":
                app = QApplication(sys.argv)
                widget = MyWidget()
                widget.show()
                sys.exit(app.exec())
            
            EmrecpE Offline
            EmrecpE Offline
            Emrecp
            wrote on last edited by
            #5

            @SGaist Thanks again, I did this too but I need if mainwindow resizing/moving, this child dialog should too resize/moving, I can do this with hooking resizeEvent or moveEvent but I want it automatically doing because of mainwindow layout. This child widget should be in mainwindow layout for auto resize/moving. Thanks again.

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              So you want a modal dialog that is in fact loaded within a widget inside your QMainWindow class ?

              You can manually set the modality of your dialog with setModal.

              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