QDialog in QMainwinndow and make the background dark
-
Hello,
I have design:
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!
-
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.
-
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())
-
@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.