Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. [Solved] QFont doesn't seem to work properly
QtWS25 Last Chance

[Solved] QFont doesn't seem to work properly

Scheduled Pinned Locked Moved QML and Qt Quick
5 Posts 2 Posters 7.4k Views
  • 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.
  • E Offline
    E Offline
    Endless
    wrote on last edited by
    #1

    In the constructor below, I set the default font for all QLabels with the setStyleSheet function. I want the first label, faultLabel, to have a font other than the default, so I set it specifically. When I run the application, though, both labels have the default font set in the stylesheet. What am I doing wrong?
    @
    StatusBar::StatusBar(QWidget *parent) :
    QWidget(parent)
    {
    QFrame *statusFrame = new QFrame(parent);
    statusFrame->resize(1280, 42);
    statusFrame->setStyleSheet("QLabel{ font: 24px "MS Shell Dlg 2", sans-serif; }");

    QLabel *faultLabel = new QLabel(statusFrame);
    faultLabel->setFont(QFont("Arial", 8));
    faultLabel->move(300, 2);
    faultLabel->setText("Fault:");
    
    QLabel *dateLabel = new QLabel(statusFrame);
    dateLabel->move(1080, 2);
    dateLabel->setText("Date field");
    

    }
    @

    1 Reply Last reply
    0
    • G Offline
      G Offline
      giesbert
      wrote on last edited by
      #2

      If you use style sheets, member changes for font and palette are ignored.
      You must the set the font on the specific label also via style sheet.

      Nokia Certified Qt Specialist.
      Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

      1 Reply Last reply
      1
      • E Offline
        E Offline
        Endless
        wrote on last edited by
        #3

        That makes sense, but when I add the following line to my setStyleSheet call, I get the same result:
        @
        "QLabel#faultLabel{ font: 10px "Arial", sans-serif; }");
        @

        1 Reply Last reply
        0
        • G Offline
          G Offline
          giesbert
          wrote on last edited by
          #4

          That is because you have no object of that name, only a local variable :-)

          @
          StatusBar::StatusBar(QWidget *parent) :
          QWidget(parent)
          {
          QFrame *statusFrame = new QFrame(parent);
          statusFrame->resize(1280, 42);
          statusFrame->setStyleSheet("QLabel{ font: 24px "MS Shell Dlg 2", sans-serif; }"
          "QLabel#faultLabel{ font: 10px "Arial", sans-serif; }");

          QLabel *faultLabel = new QLabel(statusFrame);
          faultLabel->setFont(QFont("Arial", 8));
          faultLabel->setObjectName("faultLabel"); // <-- this is the trick
          faultLabel->move(300, 2);
          faultLabel->setText("Fault:");
          
          QLabel *dateLabel = new QLabel(statusFrame);
          dateLabel->move(1080, 2);
          dateLabel->setText("Date field");
          

          }
          @

          Nokia Certified Qt Specialist.
          Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

          1 Reply Last reply
          0
          • E Offline
            E Offline
            Endless
            wrote on last edited by
            #5

            Thanks - that really helped!

            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