qtmodern
qtmodern copied to clipboard
How could apply to QDialog and QMessageBox
Hello: I found the QDialog and QMessageBox could not apply the dark theme in their qindow frame and border. How could i apply the dark theme in QDialog and QMessageBox and keep it after close and reopen? Thank you
Hi
I think that QMessageBox
and QDialog
should inherit the style automatically. Like what happens in the example, is this not the case for you? Could you share a project where this happens?
I am not sure what you want to achieve with "keep it after close and reopen". Are you saying that your screens closes immediately when open? The it isn't related to qtmodern, but if that is the case you might need to save a reference to the child so that it doesn't get garbage collected. If you instead are saying that you want it to behave differently when you click on the QMessageBox
buttons then you need to either create your own MessageBox or subclass the existing one.
Hello: Here is simple example. Python 3.7.5 PyQt5==5.13.2 qtmodern==0.1.4 Windows 7 SP1 x64
import qtmodern.styles
import qtmodern.windows
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
import sys
class MainWindow(QMainWindow):
def __init__(self, *args, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
self.setWindowTitle("My Awesome App")
layout = QVBoxLayout()
self.msg = QMessageBox(self)
self.btn_m = QPushButton("Show Message Box")
self.btn_m.clicked.connect(self.msg.show)
self.dig = QDialog(self)
self.btn_d = QPushButton("Show Dialog")
self.btn_d.clicked.connect(self.dig.show)
layout.addWidget(self.btn_m)
layout.addWidget(self.btn_d)
widget = QWidget()
widget.setLayout(layout)
self.setCentralWidget(widget)
app = QApplication(sys.argv)
window = MainWindow()
qtmodern.styles.dark(app)
window = qtmodern.windows.ModernWindow(window)
window.show()
app.exec_()
And this i run the example
I see what you mean now. Yeah dialogs will still have the window frame, only workaround I can think about it to create it from scratch.
I added this in https://github.com/gmarull/qtmodern/pull/44
You can do something like
d = windows.ModernDialog(parent=self, hide_window_buttons=True)
to create a QDialog with no window buttons.
it could then look something like this:
for QMessageBox you could so something like this:
d = windows.ModernDialog(parent=self, hide_window_buttons=True)
d.setWindowTitle('Update Available')
box = QMessageBox(d)
box.setIcon(QMessageBox.Question)
box.setText('There is a new version available.\nWould you like to update now?')
box.setStandardButtons(QMessageBox.Yes | QMessageBox.No)
box.setEscapeButton(QMessageBox.No)
box.setAttribute(Qt.WA_TranslucentBackground)
box.setWindowFlags(Qt.Window | Qt.FramelessWindowHint | Qt.WindowSystemMenuHint)
layout = QHBoxLayout()
layout.addWidget(box, alignment=Qt.Alignment(0))
d.windowContent.setLayout(layout)
d.show()
which would create something like this:
I added this in #44
You can do something like
d = windows.ModernDialog(parent=self, hide_window_buttons=True)
to create a QDialog with no window buttons.
it could then look something like this:
for QMessageBox you could so something like this:
d = windows.ModernDialog(parent=self, hide_window_buttons=True) d.setWindowTitle('Update Available') box = QMessageBox(d) box.setIcon(QMessageBox.Question) box.setText('There is a new version available.\nWould you like to update now?') box.setStandardButtons(QMessageBox.Yes | QMessageBox.No) box.setEscapeButton(QMessageBox.No) box.setAttribute(Qt.WA_TranslucentBackground) box.setWindowFlags(Qt.Window | Qt.FramelessWindowHint | Qt.WindowSystemMenuHint) layout = QHBoxLayout() layout.addWidget(box, alignment=Qt.Alignment(0)) d.windowContent.setLayout(layout) d.show()
which would create something like this:
It do not work when closed.
from PyQt5.QtWidgets import QWidget, QMessageBox, QHBoxLayout, QMainWindow, QPushButton, QApplication
from PyQt5.QtCore import Qt
from qtmodern.windows import ModernDialog
import sys
import qtmodern.styles
class RazaqqModernMessageBox(QWidget):
def __init__(self, parent):
QWidget.__init__(self, parent)
self.init_UI()
def init_UI(self):
self.d = ModernDialog(parent=self, hide_window_buttons=True)
self.d.setWindowTitle("Update Available")
self.box = QMessageBox(self.d)
self.box.setIcon(QMessageBox.Question)
self.box.setStandardButtons(QMessageBox.Yes | QMessageBox.No)
self.box.setEscapeButton(QMessageBox.No)
self.box.setAttribute(Qt.WA_TranslucentBackground)
self.box.setWindowFlags(Qt.Window | Qt.FramelessWindowHint | Qt.WindowSystemMenuHint)
layout = QHBoxLayout()
layout.addWidget(self.box, alignment=Qt.Alignment(0))
self.d.windowContent.setLayout(layout)
if __name__ == "__main__":
app = QApplication(sys.argv)
window = QMainWindow()
layout = window.layout()
m = RazaqqModernMessageBox(window)
m.box.setText("There is a new version available.\nWould you like to update now?")
button = QPushButton(window, clicked=m.d.show)
# m = QMessageBox(window)
# m.setText("There is a new version available.\nWould you like to update now?")
# button = QPushButton(window, clicked=m.show)
layout.addWidget(button)
qtmodern.styles.dark(app)
window.show()
app.exec_()
What you mean with "It do not work when closed."?
If you mean that it does not close when you click yes or no, then that is normal, as you would have to manually add that function.
Do something like this:
from PyQt5.QtWidgets import QWidget, QMessageBox, QHBoxLayout, QMainWindow, QPushButton, QApplication
from PyQt5.QtCore import Qt
from qtmodern.windows import ModernDialog
import sys
from qtmodern import styles
class RazaqqModernMessageBox(QWidget):
def __init__(self, parent):
QWidget.__init__(self, parent)
def create_dialog(self):
d = ModernDialog(parent=self, hide_window_buttons=True)
d.setWindowTitle("Update Available")
box = QMessageBox(d)
box.setIcon(QMessageBox.Question)
box.setStandardButtons(QMessageBox.Yes | QMessageBox.No)
box.setEscapeButton(QMessageBox.No)
box.setAttribute(Qt.WA_TranslucentBackground)
box.setWindowFlags(Qt.Window | Qt.FramelessWindowHint | Qt.WindowSystemMenuHint)
box.setText("There is a new version available.\nWould you like to update now?")
layout = QHBoxLayout()
layout.addWidget(box, alignment=Qt.Alignment(0))
d.windowContent.setLayout(layout)
d.show()
if box.exec_() == QMessageBox.Yes:
d.hide() # maybe return true?
else:
d.hide() # maybe return false?
if __name__ == "__main__":
app = QApplication(sys.argv)
main_window = QMainWindow()
m = RazaqqModernMessageBox(main_window)
layout = QHBoxLayout()
button = QPushButton(main_window, clicked=m.create_dialog, text='Click me!')
layout.addWidget(button)
main_window.setCentralWidget(QWidget())
main_window.centralWidget().setLayout(layout)
styles.dark(app)
main_window.show()
app.exec_()
Hello, When I try to use this solution, ImportError occurred, it says "cannot import name 'ModernDialog' from 'qtmodern.windows'". Can you help on this error? Thanks.
ModernDialog
I got an error AttributeError: module 'qtmodern.windows' has no attribute 'ModernDialog'
. It seems I'm looking forward to a new release.