Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Setting font to QLabel failled
Forum Updated to NodeBB v4.3 + New Features

Setting font to QLabel failled

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 5 Posters 13.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • VRoninV VRonin

    You overcomplicated the issue a lot. just call lineedit->setStyleSheet("font: bold italic large "Times New Roman");

    J.HilkJ Offline
    J.HilkJ Offline
    J.Hilk
    Moderators
    wrote on last edited by
    #3

    @VRonin said in Setting font to QLabel failled:

    You overcomplicated the issue a lot. just call lineedit->setStyleSheet("font: bold italic large "Times New Roman");

    I personally am not a fan of setting fonts via stylesheet.

    For example, what if you want an option to change the font size in your app via a QSpinbox, so the elderly can read your texts too :-)

    You have to create a StyleSheet as a QString, insert the new fontsize into it, and apply it to all effected Objects/Widgets.

    Or am I wrong here?


    Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


    Q: What's that?
    A: It's blue light.
    Q: What does it do?
    A: It turns blue.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #4

      Hi and welcome to devnet,

      Just a side note, the setFont method is not virtual so your code is hiding the base class implementation thus you won't have the results you expect.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      H 1 Reply Last reply
      3
      • H H_V_

        Hi,
        I have a widget with a QLabel and a QLineEdit and I want to be able to modify the font of the QLineEdit without affecting the QLabel font. So I reimplemented the setFont of the widget. I've first try to only apply the new font to the line edit, but if I don't call the base class implementation (QWidget::setFont()) it bugs and nothing is displayed (neither QLabel, neither QLineEdit).
        So I've change to call the base class implementation in my implementation, which makes both QLineEdit and QLabel font change.
        So I saved the QLabel original font before and reapply it after... It does'nt work neither. But if I specify a static font it works.
        Code exemple:

        class MyWidget: public QWidget
        {
          virtual void setFont(const QFont& font);
        private:
          QLineEdit* lineedit;
          QLabel* label;
        }
        

        first case:

        void MyWidget::setFont(const QFont& newFont)
        {
           lineedit->setFont(newFont);
        }//nothing is displayed
        

        second case:

        void MyWidget::setFont(const QFont& newFont)
        {
           QFont f = label.font();                //storing label font
           QWidget::setFont(font);           //applying new font to the whole widget
           label->setFont(f);                      //applying back its original font to label
        }//label font is set to newFont and not to f
        
        

        third case:

        void MyWidget::setFont(const QFont& newFont)
        {
           QFont f("Times", 8);                   //creating a local variable font
           QWidget::setFont(font);           //applying new font to the whole widget
           label->setFont(f);                      //applying the local font to label
        }//label font is set to  f
        

        this is working in the third case, but I would like to keep the original font of label and not code it in a static way in case the general style of the application change.
        Any idea?

        joeQJ Offline
        joeQJ Offline
        joeQ
        wrote on last edited by
        #5

        @H_V_ Just In case, my customize function name first character is capitalized. like Class::SetFont(...). qt inner function Class::setFont(...);

        Just do it!

        1 Reply Last reply
        0
        • VRoninV VRonin

          You overcomplicated the issue a lot. just call lineedit->setStyleSheet("font: bold italic large "Times New Roman");

          H Offline
          H Offline
          H_V_
          wrote on last edited by
          #6

          @VRonin How can i set a stylesheet that depend on a font that is choose by the user? Because your solution seems a bit static to me...

          VRoninV 1 Reply Last reply
          0
          • SGaistS SGaist

            Hi and welcome to devnet,

            Just a side note, the setFont method is not virtual so your code is hiding the base class implementation thus you won't have the results you expect.

            H Offline
            H Offline
            H_V_
            wrote on last edited by
            #7

            @SGaist I agree, that's why I call the QWidget::setFont

            1 Reply Last reply
            0
            • H H_V_

              @VRonin How can i set a stylesheet that depend on a font that is choose by the user? Because your solution seems a bit static to me...

              VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by VRonin
              #8

              How can i set a stylesheet that depend on a font that is choose by the user?

              #include <QStringBuilder>
                  QString fontStyleString(const QFont& fnt){
                      const QString baseStr= QStringLiteral("font-family: \"") % fnt.family()
                              % QStringLiteral("\";font-weight: ") % (fnt.bold() ? QStringLiteral("bold") : QStringLiteral("normal"))
                               % QStringLiteral(";font-size: ") % QString::number(fnt.pointSizeF(),'f') % "pt"
                              % QStringLiteral(";font-style: ");
                      switch (fnt.style()){
                      case QFont::StyleItalic:
                          return baseStr % QStringLiteral("italic");
                      case QFont::StyleOblique:
                          return baseStr % QStringLiteral("oblique");
                      default:
                          return baseStr % QStringLiteral("normal");
                      }
                  }
              

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              H 1 Reply Last reply
              3
              • VRoninV VRonin

                How can i set a stylesheet that depend on a font that is choose by the user?

                #include <QStringBuilder>
                    QString fontStyleString(const QFont& fnt){
                        const QString baseStr= QStringLiteral("font-family: \"") % fnt.family()
                                % QStringLiteral("\";font-weight: ") % (fnt.bold() ? QStringLiteral("bold") : QStringLiteral("normal"))
                                 % QStringLiteral(";font-size: ") % QString::number(fnt.pointSizeF(),'f') % "pt"
                                % QStringLiteral(";font-style: ");
                        switch (fnt.style()){
                        case QFont::StyleItalic:
                            return baseStr % QStringLiteral("italic");
                        case QFont::StyleOblique:
                            return baseStr % QStringLiteral("oblique");
                        default:
                            return baseStr % QStringLiteral("normal");
                        }
                    }
                
                H Offline
                H Offline
                H_V_
                wrote on last edited by
                #9

                @VRonin Thanks, it works perfectly.
                But I'm a bit curious on why my method works with the local variable and not with the stored value. If someone has an explaination I would be grateful...

                VRoninV 1 Reply Last reply
                1
                • H H_V_

                  @VRonin Thanks, it works perfectly.
                  But I'm a bit curious on why my method works with the local variable and not with the stored value. If someone has an explaination I would be grateful...

                  VRoninV Offline
                  VRoninV Offline
                  VRonin
                  wrote on last edited by VRonin
                  #10

                  @H_V_ said in Setting font to QLabel failled:

                  But I'm a bit curious on why my method works with the local variable and not with the stored value. If someone has an explaination I would be grateful...

                  strange indeed. funnily enough this works:

                  const QString f = label->font().toString();                //storing label font
                     QWidget::setFont(font);           //applying new font to the whole widget
                  QFont tmpFont;
                  tmpFont.fromString(f);
                     label->setFont(tmpFont);  
                  

                  "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                  ~Napoleon Bonaparte

                  On a crusade to banish setIndexWidget() from the holy land of Qt

                  H 1 Reply Last reply
                  1
                  • H H_V_

                    Hi,
                    I have a widget with a QLabel and a QLineEdit and I want to be able to modify the font of the QLineEdit without affecting the QLabel font. So I reimplemented the setFont of the widget. I've first try to only apply the new font to the line edit, but if I don't call the base class implementation (QWidget::setFont()) it bugs and nothing is displayed (neither QLabel, neither QLineEdit).
                    So I've change to call the base class implementation in my implementation, which makes both QLineEdit and QLabel font change.
                    So I saved the QLabel original font before and reapply it after... It does'nt work neither. But if I specify a static font it works.
                    Code exemple:

                    class MyWidget: public QWidget
                    {
                      virtual void setFont(const QFont& font);
                    private:
                      QLineEdit* lineedit;
                      QLabel* label;
                    }
                    

                    first case:

                    void MyWidget::setFont(const QFont& newFont)
                    {
                       lineedit->setFont(newFont);
                    }//nothing is displayed
                    

                    second case:

                    void MyWidget::setFont(const QFont& newFont)
                    {
                       QFont f = label.font();                //storing label font
                       QWidget::setFont(font);           //applying new font to the whole widget
                       label->setFont(f);                      //applying back its original font to label
                    }//label font is set to newFont and not to f
                    
                    

                    third case:

                    void MyWidget::setFont(const QFont& newFont)
                    {
                       QFont f("Times", 8);                   //creating a local variable font
                       QWidget::setFont(font);           //applying new font to the whole widget
                       label->setFont(f);                      //applying the local font to label
                    }//label font is set to  f
                    

                    this is working in the third case, but I would like to keep the original font of label and not code it in a static way in case the general style of the application change.
                    Any idea?

                    H Offline
                    H Offline
                    H_V_
                    wrote on last edited by
                    #11

                    Ok, I'm so sorry guys, I think I was clearly not awake enough.
                    After more investigation, it appears that the first described case works perfectly when you don't forget to clean your project :-s :

                    void MyWidget::setFont(const QFont& newFont)
                    {
                    lineedit->setFont(newFont);
                    }

                    So sorry for wasting your time and thanks a lot for all your replies

                    1 Reply Last reply
                    0
                    • VRoninV VRonin

                      @H_V_ said in Setting font to QLabel failled:

                      But I'm a bit curious on why my method works with the local variable and not with the stored value. If someone has an explaination I would be grateful...

                      strange indeed. funnily enough this works:

                      const QString f = label->font().toString();                //storing label font
                         QWidget::setFont(font);           //applying new font to the whole widget
                      QFont tmpFont;
                      tmpFont.fromString(f);
                         label->setFont(tmpFont);  
                      
                      H Offline
                      H Offline
                      H_V_
                      wrote on last edited by
                      #12

                      @VRonin said in Setting font to QLabel failled:

                      @H_V_ said in Setting font to QLabel failled:

                      But I'm a bit curious on why my method works with the local variable and not with the stored value. If someone has an explaination I would be grateful...

                      strange indeed. funnily enough this works:

                      const QString f = label->font().toString();                //storing label font
                         QWidget::setFont(font);           //applying new font to the whole widget
                      QFont tmpFont;
                      tmpFont.fromString(f);
                         label->setFont(tmpFont);  
                      

                      Despite I found the solution, I'm still curious about that strange behavior (that appears even stranger after your post)... Have you found any explaination?

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved