0

Consider this example, ripped mostly from https://pythonbasics.org/pyqt-qmessagebox/:

import sys
from PyQt5 import QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QMessageBox
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import pyqtSlot

defaultfont = QtGui.QFont('Arial', 8)

def window():
  app = QApplication(sys.argv)
  win = QWidget()
  button1 = QPushButton(win)
  button1.setText("Show dialog!")
  button1.move(50,50)
  button1.clicked.connect(showDialog)
  win.setWindowTitle("Click button")
  win.show()
  sys.exit(app.exec_())

def showDialog():
  msgBox = QMessageBox()
  msgBox.setStyleSheet("QLabel{min-width: 200px;}")
  msgBox.setFont(defaultfont)
  #msgBox.button(QMessageBox.Ok).setFont(defaultfont) # nowork, msgBox.button(QMessageBox.Ok) is None
  #print(msgBox.buttons()) # []
  #print(msgBox.findChildren(QtWidgets.QDialogButtonBox)) # [<PyQt5.QtWidgets.QDialogButtonBox object at 0x0000000005f950d0>]
  #print(msgBox.findChildren(QtWidgets.QDialogButtonBox)[0].buttons()) # []
  #print(msgBox.findChildren(QtWidgets.QDialogButtonBox)[0].standardButtons()) # <PyQt5.QtWidgets.QDialogButtonBox.StandardButtons object at 0x0000000005f60580>
  msgBox.setIcon(QMessageBox.Information)
  msgBox.setText("Message box pop up window")
  msgBox.setWindowTitle("QMessageBox Example")
  msgBox.buttonClicked.connect(msgButtonClick)

  returnValue = msgBox.exec_()
  if returnValue == QMessageBox.Ok:
    print('OK clicked')

def msgButtonClick(i):
  print("Button clicked is:",i.text())

if __name__ == '__main__':
  window()

As the code shows, I tried applying msgBox.setFont(defaultfont) - and indeed, it does change the font of most of the message - but it does not change the font of buttons, if the line msgBox.setStyleSheet("QLabel{min-width: 200px;}") is present; this is how it looks like on Raspberry Pi in that case:

button-font-bad

However, if you comment the line msgBox.setStyleSheet("QLabel{min-width: 200px;}"), then the font is applied also to the button:

button-font-ok

So, how can I both use the setStyleSheet command, and change the font of the message box - for both texts and the button? (I am aware the window title bar font is under the control of the OS, and cannot be changed via pyqt5).

sdbbs
  • 4,270
  • 5
  • 32
  • 87

2 Answers2

3

If you want to change the minimum width of the QLabels then you can use setMinimumWidth():

def showDialog():
    msgBox = QMessageBox()
    msgBox.setFont(defaultfont)
    msgBox.setIcon(QMessageBox.Information)
    msgBox.setText("Message box pop up window")
    msgBox.setWindowTitle("QMessageBox Example")
    msgBox.buttonClicked.connect(msgButtonClick)

    for label in msgBox.findChildren(QtWidgets.QLabel):
        label.setMinimumWidth(200)

    returnValue = msgBox.exec_()
    if returnValue == QMessageBox.Ok:
        print("OK clicked")

Another solution is to access the button and set the font, but this is created after using the show() method:

def showDialog():
    msgBox = QMessageBox()
    msgBox.setFont(defaultfont)
    msgBox.setIcon(QMessageBox.Information)
    msgBox.setText("Message box pop up window")
    msgBox.setWindowTitle("QMessageBox Example")
    msgBox.buttonClicked.connect(msgButtonClick)

    msgBox.setStyleSheet("QLabel{min-width: 200px;}")

    msgBox.show()
    msgBox.findChild(QtWidgets.QPushButton).setFont(defaultfont)

    returnValue = msgBox.exec_()
    if returnValue == QMessageBox.Ok:
        print("OK clicked")
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • 1
    I've noticed this behavior for QDialogButtonBox, and I really don't understand if it's a bug or deliberate: it seems like, when stylesheets are applied, they stop not only palette propagation, but font also. Do you have any insight about this? – musicamante Jul 22 '20 at 15:35
  • Many thanks, the `setMinimumWidth()` worked great for me! Just for the record, I wanted to change the minimum width of the QLabels because I wanted to resize the message box dialog, and I ripped that CSS from https://stackoverflow.com/questions/37668820/how-can-i-resize-qmessagebox - forgot completely I could also try `setMinimumWidth()`! Also great to know buttons are accessible after the show() methods - I was otherwise quite puzzled why I cannot access them. – sdbbs Jul 23 '20 at 06:49
  • Just wanted to also note this: if you have a class-based window (which this question & answer does not have), I've noticed that instantiation like `msgBox = QMessageBox(self)` where `self` is the reference to the window class, also prevents the button from picking up the `setFont` setting, but `msgBox = QMessageBox()` seems to work fine just as in this question/answer. – sdbbs Jul 23 '20 at 07:02
1

Add to your code, this line:

msgBox.setStyleSheet("QPushButton {color:red; font-family: Arial; font-size:8px;}")

The button Ok on msgBox will change to red color, and your font! Tested!

protoproto
  • 2,081
  • 1
  • 13
  • 13