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
QtWS25 Last Chance

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.
  • E Offline
    E Offline
    Emrecp
    wrote on 2 Apr 2022, 06:58 last edited by Emrecp 4 Feb 2022, 06:58
    #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
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 2 Apr 2022, 19:36 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

      E 1 Reply Last reply 3 Apr 2022, 17:07
      1
      • S SGaist
        2 Apr 2022, 19:36

        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.

        E Offline
        E Offline
        Emrecp
        wrote on 3 Apr 2022, 17:07 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
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 3 Apr 2022, 19:15 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

          E 1 Reply Last reply 4 Apr 2022, 07:15
          0
          • S SGaist
            3 Apr 2022, 19:15

            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())
            
            E Offline
            E Offline
            Emrecp
            wrote on 4 Apr 2022, 07:15 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
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 6 Apr 2022, 18:21 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

              3/6

              3 Apr 2022, 17:07

              • Login

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