How do I get a QMenu to react to text size changes on High DPI diplays



  • The text in menubars and menues automatically change size when a window is moved from a 96 dpi screen to 192 dpi screen. I naively thought Qt would automatically resize menues when the menu text size changed but I'm obviously wrong here.

    1. How can I get QMenuBar and QMenu to change size when the text changes size? (Specifically when the window is dragged to the 192 dpi screen)

    Text size only change when I move the window to a 192 dpi screen. If the window is initially shown on the 192 screen it will draw the smaller 96 dpi text.

    1. How do I ensure that the text is the correct size when the mainwindow opens on a 192 dpi screen?

    I've tested playing around with the QT_DEVICE_PIXEL_RATIO env variable but this doesn't solve anything. I can force the menues to become larger by setting the value to 2, but I need them to change size depending on the screen in use. And the application must be Per-Monitor DPI Aware on Windows so leaving it to the window manager auto scale is not an option.

    I've also tested this with the Fusion style to rule out it being related to the native Windows style.
    Menues
    (Just noticed the 192 dpi image is actually at 144 dpi, the 192 dpi version looks worse..)

    A trivial test case:

    #include <QMainWindow>
    #include <QMenuBar>
    #include <QStyle>
    //#include <QStyleFactory>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
    
        //a.setStyle((QStyleFactory::create(("Fusion"))));
    
        QMainWindow w;
    
        QMenuBar *menuBar = w.menuBar();
    
        QMenu *menuFile = menuBar->addMenu("File");
        QMenu *menuEdit = menuBar->addMenu("Edit");
        QMenu *menuCompany = menuBar->addMenu("&Company");
        QMenu *menuArrange = menuBar->addMenu("Arrange");
    
        // file menu
        menuFile->addAction(a.style()->standardIcon(QStyle::SP_DirOpenIcon), "Open", nullptr, nullptr, QKeySequence::Open);
        menuFile->addAction(a.style()->standardIcon(QStyle::SP_DriveFDIcon), "Save", nullptr, nullptr, QKeySequence::Save);
        QAction* actionQuit = menuFile->addAction("Quit");
        QObject::connect(actionQuit, &QAction::triggered, &w, &QMainWindow::close);
        // edit menu
        menuEdit->addAction(a.style()->standardIcon(QStyle::SP_ArrowLeft), "Undo", nullptr, nullptr, QKeySequence::Undo);
        menuEdit->addAction(a.style()->standardIcon(QStyle::SP_ArrowRight), "Redo", nullptr, nullptr, QKeySequence::Redo);
        // company menu
        menuCompany->addAction(a.style()->standardIcon(QStyle::SP_DriveNetIcon), "DB Connect", nullptr, nullptr, QKeySequence(Qt::SHIFT + Qt::Key_Insert));
    
        w.setCentralWidget(new QWidget);
    
        w.show();
    
        return a.exec();
    }
    

    Tested on Windows 8.1 with Qt 5.4 & 5.5. All dpi values are what Windows reports, not the actual physical screen dpi.
    I'm hoping to find a non native solution to this but I don't really know if this is an issue on any other platform then Windows.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.