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

change font of stackwidget



  • I have a GUI program debug for raspberry pi with one central widget and I use this code for change the font that add in resources file:

    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
       MainWindow w;
       QFontDatabase::addApplicationFont(":/new/prefix1/Nastaliq.ttf");
       QFont nastaliq("Nastaliq",20,QFont::Normal);
       w.setFont(nastaliq);
       w.show();
       return a.exec();
    }
    

    and it's work

    and I have an other program with stack-widget and use above code it doesn't change font of GUI application why?


  • Lifetime Qt Champion

    Hi,

    Do you mean you set the font on the stack widget ? It's just a container widget forQStackedLayout so it doesn't have a "body" like QMainWindow.



  • @SGaist I use the stack widget and I want to set the font for the content on it like push button, combobox ....
    So why the font doesn't change?



  • @zhmh
    I use QStackedWidget and just set application font and it works everywhere, QStackedWidget is not relevant.



  • @JonB you set font application like my code?
    I use it on test project with QStackWidget and it works but in my project doesn't work ,so what's the problem ?


  • Lifetime Qt Champion

    If you want all your widgets to use the same font, then just set it through QApplication::setFont.



  • @SGaist I use this:

    QFont newFont(":/new/prefix1/Nastaliq.ttf", 8, QFont::Bold, true);
        QApplication::setFont(newFont);
    

    The size is set, but the font does not change


  • Lifetime Qt Champion

    QFont doesn't take a path as parameter but the family name.

    Use QFontDatabase to load your custom font, build the QFont object using the correct family name and then set it on QApplication.



  • @SGaist can you show me with code?
    I used the qfontdatabase at first an doesn’t work!


  • Lifetime Qt Champion

    Then show the current code you use.



  • @zhmh said in change font of stackwidget:

    I used the qfontdatabase at first an doesn’t work!

    Can you check if addApplicationFont function returns -1 ?
    as mentioned here



  • @Ratzz yes I checked it before, this is

    int fnt=QFontDatabase::addApplicationFont(":/new/prefix1/Nastaliq.ttf");
         qDebug()<< fnt;
    

    returns 0 to me!



  • @zhmh said in change font of stackwidget:

    returns 0 to me

    Which means the fond is loaded properly.

    Use http://doc.qt.io/qt-5/qfontdatabase.html#applicationFontFamilies to get the font and set back to application.

    Can you show the code ?



  • @Ratzz

    int fnt=QFontDatabase::addApplicationFont(":/new/prefix1/Nastaliq.ttf");
          qDebug() << fnt << QFontDatabase::applicationFontFamilies(fnt);
    

    returns: 0 ("Nastaliq")

    @Ratzz said in change font of stackwidget:

    Can you show the code ?

    this is my main.cpp:

    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        MainWindow w;
        int fnt=QFontDatabase::addApplicationFont(":/new/prefix1/Nastaliq.ttf");
         qDebug() << fnt << QFontDatabase::applicationFontFamilies(fnt);
        QFont nastaliq("Nastaliq",20,QFont::Normal);
        w.setFont(nastaliq);
        w.show();
    
        return a.exec();
    }
    


  • @zhmh said in change font of stackwidget:

    QFont nastaliq("Nastaliq",20,QFont::Normal);

    You should set the font which you got from the http://doc.qt.io/qt-5/qfontdatabase.html#applicationFontFamilies



  • @Ratzz how ?

    int fnt=QFontDatabase::addApplicationFont(":/new/prefix1/Nastaliq.ttf");
    QString family = QFontDatabase::applicationFontFamilies(fnt).at(0);
     w.setFont(family);
    

    is it true?



  • @zhmh said in change font of stackwidget:

    is it true?

    Yes. Does it work that way?



  • @Ratzz No :(


  • Lifetime Qt Champion

    Where did you get that font from ?



  • @SGaist font work with other program


  • Lifetime Qt Champion

    That's beside the question. If people knows where to download the font, they can test it on their system to find out what's going wrong.





  • @zhmh
    I tried same font an QStackedWidget with QPushButton in it and seems fine for me.

        QStringList list= QFontDatabase::applicationFontFamilies(fnt);
        QFont nastaliq(list.at((0)));
        w.setFont(nastaliq);
    

    alt text



  • @Ratzz I don't know why, but it's not work for me



  • @zhmh

    Can you show me a minimal sample/code with which I can reproduce your problem?


  • Lifetime Qt Champion

    @zhmh
    Hi, so buttons keep their normal font or in what
    way does it not work?



  • @mrjj said in change font of stackwidget:

    buttons keep their normal font

    Hi,buttons keep their normal font



  • @Ratzz
    cpp:

    Healthy::Healthy(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::Healthy)
    {
        ui->setupUi(this);
    
        //--------------------------for changing the page-------------------------------------//
        connect(ui->back2,  &QAbstractButton::clicked,[=](){ui->stackedWidget->setCurrentIndex(0);});
            connect(ui->doctor,  &QAbstractButton::clicked,[=](){ui->stackedWidget->setCurrentIndex(1);});
        connect(ui->back3,  &QAbstractButton::clicked,[=](){ui->stackedWidget->setCurrentIndex(1);});
        connect(ui->next1,  &QAbstractButton::clicked,[=](){ui->stackedWidget->setCurrentIndex(2);});
    
    
    
    
        //---------------------------------close GUI-----------------------------------------//
        connect(ui->exit, SIGNAL(clicked()), this, SLOT(close()));
    
    
    }
    
    Healthy::~Healthy()
    {
        delete ui;
    }
    

    main:

    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        Healthy w;
        int fnt=QFontDatabase::addApplicationFont(":/new/prefix1/Nastaliq.ttf");
        QStringList list= QFontDatabase::applicationFontFamilies(fnt);
         QFont nastaliq(list.at((0)));
         w.setFont(nastaliq);
        w.show();
    
        return a.exec();
    }
    

    ui:

    <?xml version="1.0" encoding="UTF-8"?>
    <ui version="4.0">
     <class>Healthy</class>
     <widget class="QMainWindow" name="Healthy">
      <property name="geometry">
       <rect>
        <x>0</x>
        <y>0</y>
        <width>803</width>
        <height>485</height>
       </rect>
      </property>
      <property name="windowTitle">
       <string>Healthy</string>
      </property>
      <property name="styleSheet">
       <string notr="true">background-color: rgb(161, 194, 231)</string>
      </property>
      <widget class="QWidget" name="centralWidget">
       <widget class="QStackedWidget" name="stackedWidget">
        <property name="geometry">
         <rect>
          <x>20</x>
          <y>20</y>
          <width>761</width>
          <height>421</height>
         </rect>
        </property>
        <property name="font">
         <font>
          <pointsize>15</pointsize>
         </font>
        </property>
        <property name="styleSheet">
         <string notr="true">background-color:rgb(255, 255, 255)</string>
        </property>
        <property name="currentIndex">
         <number>0</number>
        </property>
        <widget class="QWidget" name="page">
         <widget class="QPushButton" name="doctor">
          <property name="geometry">
           <rect>
            <x>190</x>
            <y>180</y>
            <width>221</width>
            <height>91</height>
           </rect>
          </property>
          <property name="styleSheet">
           <string notr="true"/>
          </property>
          <property name="text">
           <string>پزشک</string>
          </property>
         </widget>
         <widget class="QPushButton" name="exit">
          <property name="geometry">
           <rect>
            <x>50</x>
            <y>30</y>
            <width>81</width>
            <height>31</height>
           </rect>
          </property>
          <property name="styleSheet">
           <string notr="true"/>
          </property>
          <property name="text">
           <string>خروج</string>
          </property>
         </widget>
        </widget>
        <widget class="QWidget" name="page_2">
         <widget class="QPushButton" name="back2">
          <property name="geometry">
           <rect>
            <x>70</x>
            <y>349</y>
            <width>101</width>
            <height>41</height>
           </rect>
          </property>
          <property name="styleSheet">
           <string notr="true"/>
          </property>
          <property name="text">
           <string>&lt; قبلی</string>
          </property>
         </widget>
         <widget class="QPushButton" name="next1">
          <property name="geometry">
           <rect>
            <x>600</x>
            <y>349</y>
            <width>101</width>
            <height>41</height>
           </rect>
          </property>
          <property name="styleSheet">
           <string notr="true"/>
          </property>
          <property name="text">
           <string>بعدی &gt;</string>
          </property>
         </widget>
         <widget class="QLineEdit" name="meliCode">
          <property name="geometry">
           <rect>
            <x>315</x>
            <y>130</y>
            <width>201</width>
            <height>41</height>
           </rect>
          </property>
         </widget>
         <widget class="QComboBox" name="gender">
          <property name="geometry">
           <rect>
            <x>64</x>
            <y>120</y>
            <width>121</width>
            <height>61</height>
           </rect>
          </property>
          <property name="styleSheet">
           <string notr="true"/>
          </property>
          <item>
           <property name="text">
            <string>مرد</string>
           </property>
          </item>
          <item>
           <property name="text">
            <string>زن</string>
           </property>
          </item>
          <item>
           <property name="text">
            <string/>
           </property>
          </item>
         </widget>
         <widget class="QLabel" name="label_2">
          <property name="geometry">
           <rect>
            <x>580</x>
            <y>140</y>
            <width>61</width>
            <height>31</height>
           </rect>
          </property>
          <property name="font">
           <font>
            <pointsize>15</pointsize>
           </font>
          </property>
          <property name="text">
           <string>کدملی</string>
          </property>
         </widget>
         <widget class="QLabel" name="label_3">
          <property name="geometry">
           <rect>
            <x>220</x>
            <y>50</y>
            <width>41</width>
            <height>31</height>
           </rect>
          </property>
          <property name="font">
           <font>
            <pointsize>15</pointsize>
           </font>
          </property>
          <property name="text">
           <string>سن</string>
          </property>
         </widget>
         <widget class="QLabel" name="label_4">
          <property name="geometry">
           <rect>
            <x>200</x>
            <y>140</y>
            <width>71</width>
            <height>31</height>
           </rect>
          </property>
          <property name="font">
           <font>
            <pointsize>15</pointsize>
           </font>
          </property>
          <property name="text">
           <string>جنسبت</string>
          </property>
         </widget>
         <widget class="QLabel" name="label_5">
          <property name="geometry">
           <rect>
            <x>560</x>
            <y>220</y>
            <width>81</width>
            <height>31</height>
           </rect>
          </property>
          <property name="font">
           <font>
            <pointsize>15</pointsize>
           </font>
          </property>
          <property name="text">
           <string>تاریخ </string>
          </property>
         </widget>
         <widget class="QLineEdit" name="date">
          <property name="geometry">
           <rect>
            <x>315</x>
            <y>210</y>
            <width>201</width>
            <height>41</height>
           </rect>
          </property>
         </widget>
         <widget class="QLineEdit" name="age">
          <property name="geometry">
           <rect>
            <x>60</x>
            <y>50</y>
            <width>141</width>
            <height>41</height>
           </rect>
          </property>
         </widget>
         <widget class="QLineEdit" name="name_line">
          <property name="geometry">
           <rect>
            <x>316</x>
            <y>41</y>
            <width>201</width>
            <height>41</height>
           </rect>
          </property>
         </widget>
         <widget class="QLabel" name="label">
          <property name="geometry">
           <rect>
            <x>534</x>
            <y>47</y>
            <width>161</width>
            <height>41</height>
           </rect>
          </property>
          <property name="font">
           <font>
            <pointsize>15</pointsize>
           </font>
          </property>
          <property name="text">
           <string>نام و نام خانوادگی</string>
          </property>
         </widget>
        </widget>
        <widget class="QWidget" name="page_3">
         <widget class="QLabel" name="label_6">
          <property name="geometry">
           <rect>
            <x>436</x>
            <y>20</y>
            <width>141</width>
            <height>51</height>
           </rect>
          </property>
          <property name="font">
           <font>
            <pointsize>15</pointsize>
           </font>
          </property>
          <property name="text">
           <string>سوابق بیماری</string>
          </property>
         </widget>
         <widget class="QCheckBox" name="checkBox">
          <property name="geometry">
           <rect>
            <x>260</x>
            <y>80</y>
            <width>201</width>
            <height>71</height>
           </rect>
          </property>
          <property name="font">
           <font>
            <pointsize>20</pointsize>
           </font>
          </property>
          <property name="text">
           <string>قلبی عروقی</string>
          </property>
          <property name="iconSize">
           <size>
            <width>50</width>
            <height>50</height>
           </size>
          </property>
         </widget>
         <widget class="QCheckBox" name="checkBox_2">
          <property name="geometry">
           <rect>
            <x>250</x>
            <y>280</y>
            <width>201</width>
            <height>61</height>
           </rect>
          </property>
          <property name="font">
           <font>
            <pointsize>20</pointsize>
           </font>
          </property>
          <property name="text">
           <string>قندخون و دیابت</string>
          </property>
         </widget>
         <widget class="QCheckBox" name="checkBox_3">
          <property name="geometry">
           <rect>
            <x>290</x>
            <y>170</y>
            <width>171</width>
            <height>81</height>
           </rect>
          </property>
          <property name="font">
           <font>
            <pointsize>20</pointsize>
           </font>
          </property>
          <property name="text">
           <string>فشار خون</string>
          </property>
         </widget>
         <widget class="QPushButton" name="back3">
          <property name="geometry">
           <rect>
            <x>70</x>
            <y>349</y>
            <width>101</width>
            <height>41</height>
           </rect>
          </property>
          <property name="styleSheet">
           <string notr="true"/>
          </property>
          <property name="text">
           <string>&lt;قبلی</string>
          </property>
         </widget>
        </widget>
       </widget>
      </widget>
      <widget class="QMenuBar" name="menuBar">
       <property name="geometry">
        <rect>
         <x>0</x>
         <y>0</y>
         <width>803</width>
         <height>25</height>
        </rect>
       </property>
      </widget>
      <widget class="QToolBar" name="mainToolBar">
       <attribute name="toolBarArea">
        <enum>TopToolBarArea</enum>
       </attribute>
       <attribute name="toolBarBreak">
        <bool>false</bool>
       </attribute>
      </widget>
      <widget class="QStatusBar" name="statusBar"/>
     </widget>
     <layoutdefault spacing="6" margin="11"/>
     <resources/>
     <connections/>
    </ui>
    


  • @zhmh said in change font of stackwidget:

    cpp:

    It works for me in Qt 5.9.3 on Win 10.
    What platform and Qt you use?



  • @Ratzz 1-Qt 5.5.1 on ubuntu 14.04
    ,2-cross compile Qt 5.6 for raspberry Pi
    3-Qt 5.6 on windows 8.1
    I check with on windows and linux buttons and comboBox and ...... keep their normal font
    as I said before in this specific program I have this problem ,I test with other program and It works


Log in to reply