简体   繁体   中英

How to modify the font in a layout in the About section in PyQt5?

I don't manage to find a way to modify the font in the About section in PyQt5:

在此处输入图像描述

I would like Version 0.0 not being in bold.

Here is the code I used:

about_box = QMessageBox()
about_box.about(self.parent(), "About", "Appli\nVersion 0.0")

Apparently, it's just possible to enter a mere string in the About.

Someone knows how to pull this off?

The about function is a static method: it is a "helper", which automatically constructs a message box, runs its exec() and returns its result. This means that there's no way to access the message box instance and, therefore, its font.

Note that, since it's a static method, there's no use in creating a new instance of QMessageBox, as you could call about on the class alone:

QMessageBox.about(self.parent(), "About", "Appli\nVersion 0.0")

According to the sources , on MacOS Qt automatically uses a bold font for the label of all message boxes.

The solution is to avoid the static method, and create a new instance of QMessageBox. Since the label widget is private, the only way to access it is through findChild() , which on PyQt allows us to use both a class and an object name; luckily, Qt sets an object name for the label ( qt_msgbox_label ), so we can access it and set the font accordingly:

    def showAbout(self):
        msgBox = QMessageBox(QMessageBox.NoIcon, 'About', 'Appli\nVersion 0.0', 
            buttons=QMessageBox.Ok, parent=self.parent())
        # find the QLabel
        label = msgBox.findChild(QLabel, 'qt_msgbox_label')
        # get its font and "unbold" it
        font = label.font()
        font.setBold(False)
        # set the font back to the label
        label.setFont(font)
        msgBox.exec_()

Try it:

from PyQt5 import QtCore, QtGui, QtWidgets


class MainWindow(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.setWindowIcon(QtGui.QIcon("icono.png"))                  
        menu = self.menuBar().addMenu("Menu")
        self.actionAbout = menu.addAction("About")
        self.actionAbout.triggered.connect(self.openAbout)

    @QtCore.pyqtSlot()
    def openAbout(self):
        messagebox = QtWidgets.QMessageBox(
            QtWidgets.QMessageBox.NoIcon,
            "About",
            """
              <p style='color: white; text-align: center;'> Appli<br>  
                <b style='color: yellow; font: italic bold 16px;'>Version 0.0</b> 
              </p>
            """,
            parent=self,
        )

        messagebox.setIconPixmap(QtGui.QPixmap("Qt.png").scaled(100, 100, QtCore.Qt.KeepAspectRatio))
        messagebox.setAttribute(QtCore.Qt.WA_DeleteOnClose)
        messagebox.setStyleSheet("""
            QMessageBox {
                border: 5px solid blue;           
                border-radius: 5px;
                background-color: rgb(100, 1, 1);
            }         
        """)

        messagebox.exec_()


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    w = MainWindow()
    w.show()
    sys.exit(app.exec_())

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM