Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to Set Font to all Application



  • Hi,
    in my application i have to allow user to choose Font

    in mainwindows i call fontdialog()

    now how i can set selected font to all form (and all contents of all form)?

    i've tried with:

    QFont V ("Verdana", 14);
    QApplication::setFont(V);
    

    but don't works (menubar was not changed correctly for example)

    i've also tried with:

     ui->menuBar->setStyleSheet("QMenuBar { font-family:Verdana; font-size:12px;}" "QMenuBar::item { font-family:Verdana; font-size:12px;}" "QMenuBar::item:selected { font-family:Verdana; font-size:12px;}");
    
    

    but don't works too (don't works for Action)

    this solution works but i don't want to change font one by one (moreover don't works for Action):

    bool ok;
     QFont Carattere = QFontDialog::getFont(&ok, this);
     if (ok) {
         V = Carattere;
         ui->menuBar->setFont(V);
         ui->menuFile->setFont(V);
         ui->menuOrdini->setFont(V);
         ui->menuModifica->setFont(V);
         ui->menuRegistra->setFont(V);
         ui->menuFornitore->setFont(V);
         ui->menuInserisci->setFont(V);
         ui->menuMagazzino->setFont(V);
         ui->menuAnagrafica->setFont(V);
         ui->menuProduzione->setFont(V);
         ui->menuda_Clienti->setFont(V);
         ui->menuCambia->setFont(V);
    }
    else {
        return;
    }
    

    any ideas?


  • Qt Champions 2017

    Did you try with QApplication setFont method ?



  • @dheerendra Yes, it's the first code i've posted up


  • Moderators

    @TheCipo76
    generally fonts propagate down to descendant child widgets (see this)

    Are you setting fonts via stylesheet in your application?


  • Banned

    This post is deleted!


  • @raven-worx No i don't have used stylesheet

    i need to allow font selection because my application works both on os x than windows..

    i have already read this article today ;)



  • with a fontdialog i can set the QApplication::font

    at the moment i have to set the QApplication::font to every widget one by one eg:

    ui->label->setFont(QApplication::Font());
    

    and when i open a dialog i have to do the same operation to all form widget..

    i'm looking to something faster / easier


  • Moderators

    @TheCipo76
    since you read the article, you are aware that setting the font explicitly (manually or via stylesheet) prevents font propagation (Qt::WA_SetFont attribute)?!



  • @raven-worx yes.. i'm fix it with this:

    void MainWindow::updateAllWidgets()
    {
        foreach (QWidget *widget, QApplication::allWidgets()) {
            widget->setFont(QApplication::font());
            widget->update();
        }
    }
    

    But don't works on Menu Action


Log in to reply