Search code examples
pythonpyqtpyqtgraphqtstylesheets

How to change PyQtGraph context menu font size?


I'm trying to change the font size of the menu whenever you right click on the plot in PyQtGraph. When I change the font size of the entire application using setStyleSheet, it also changes the font size of the menu.

Before

enter image description here

After

enter image description here

I don't want to individually change the font-size of the button because I have many other widgets in the GUI so I changed the app font-size. But it also changes the font-size of the plot menu. How can I make the font-size of the menu smaller? Either changing the font-size smaller or somehow making the menu larger so the words don't cut off would work.

from pyqtgraph.Qt import QtGui, QtCore
import numpy as np
import pyqtgraph as pg
import sys

if __name__ == '__main__':

    app = QtGui.QApplication([])

    main_window = QtGui.QMainWindow()

    widget = QtGui.QWidget()
    main_layout = QtGui.QVBoxLayout()
    widget.setLayout(main_layout)
    main_window.setCentralWidget(widget)

    button = QtGui.QPushButton('hello')

    plot_widget = pg.PlotWidget()
    plot = plot_widget.plot()

    layout = QtGui.QHBoxLayout()
    layout.addWidget(button)
    layout.addWidget(plot_widget)

    main_layout.addLayout(layout)
    main_window.show()

    app.setStyleSheet('QWidget {font-size: 30px}')

    if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
        QtGui.QApplication.instance().exec_()

Solution

  • Considering the MWE that you have provided (1) the solution is to create another rule in QSS to set the font of QMenu and its children widgets:

    app.setStyleSheet("""
        QWidget {font-size: 30px}
        QMenu {font-size: 15px}
        QMenu QWidget {font-size: 15px}
    """)
    

    (1) For a more complex widget my solution could modify other parts, so there is no general solution but it depends on the widget itself.