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. How can i adjust text of a QWidget just as QMessageBox adjust text automatically ?
Forum Updated to NodeBB v4.3 + New Features

How can i adjust text of a QWidget just as QMessageBox adjust text automatically ?

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 3 Posters 5.0k Views 2 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.
  • AhtiA Offline
    AhtiA Offline
    Ahti
    wrote on last edited by Ahti
    #1

    Can i ? if yes how ?

    this is what i do :

    object->setStylesheet("font:16pt Arial;");
    

    that makes the font size fixed/static but i want to make it dynamic ? i mean the text should fit itself in a widget on its own just as text in QMessageBox is adjusted on its own when the display resolution is changed.

    what is a signature ?? Lol

    K 1 Reply Last reply
    0
    • AhtiA Ahti

      Can i ? if yes how ?

      this is what i do :

      object->setStylesheet("font:16pt Arial;");
      

      that makes the font size fixed/static but i want to make it dynamic ? i mean the text should fit itself in a widget on its own just as text in QMessageBox is adjusted on its own when the display resolution is changed.

      K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      @Ahti

      You can set the font for a widget also through setFont

      If you specify a negative value of point size in the constructor you have default value for the system. Maybe this is of help for you.

      Vote the answer(s) that helped you to solve your issue(s)

      AhtiA 1 Reply Last reply
      3
      • K koahnig

        @Ahti

        You can set the font for a widget also through setFont

        If you specify a negative value of point size in the constructor you have default value for the system. Maybe this is of help for you.

        AhtiA Offline
        AhtiA Offline
        Ahti
        wrote on last edited by
        #3

        @koahnig How is setFont method different than stylesheet ? and what do you mean by "negative value of point size in the constructor" where is this point size ?

        what is a signature ?? Lol

        K 1 Reply Last reply
        0
        • AhtiA Ahti

          @koahnig How is setFont method different than stylesheet ? and what do you mean by "negative value of point size in the constructor" where is this point size ?

          K Offline
          K Offline
          koahnig
          wrote on last edited by
          #4

          @Ahti

          Just follow the links (under "setFont" and "the contructor" in my previous post) to the parts of the documentation.

          Sorry, I do not know the actual difference, but I would go for the font routines rather setting a style, but that is probably more a matter of taste. However, with setFont you can use also some defaults which you might to set as in your style.

          Vote the answer(s) that helped you to solve your issue(s)

          1 Reply Last reply
          0
          • mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Hi
            Do you mean to adjust the width of the widget ?
            Like for text
            "aaa"
            it will be small
            but for
            "aaaaaaaaaaaaaaaaaaaaaaaaa"
            it would be much wider?

            AhtiA 1 Reply Last reply
            3
            • mrjjM mrjj

              Hi
              Do you mean to adjust the width of the widget ?
              Like for text
              "aaa"
              it will be small
              but for
              "aaaaaaaaaaaaaaaaaaaaaaaaa"
              it would be much wider?

              AhtiA Offline
              AhtiA Offline
              Ahti
              wrote on last edited by
              #6

              @mrjj I just want the text to fit in the QWidget so that i will get rid of text getting out of alignment in a widget problem. This happens when i run my app on different resolutions like 1440X900, 1920X1080, ect.

              what is a signature ?? Lol

              mrjjM 1 Reply Last reply
              0
              • AhtiA Ahti

                @mrjj I just want the text to fit in the QWidget so that i will get rid of text getting out of alignment in a widget problem. This happens when i run my app on different resolutions like 1440X900, 1920X1080, ect.

                mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @Ahti
                Well how are you drawing the text ?
                If you dont word wrap, then text will be cut when less screen space is available.

                Do you u mean that text should shrink to fit inside the available widget rect?

                AhtiA 1 Reply Last reply
                2
                • mrjjM mrjj

                  @Ahti
                  Well how are you drawing the text ?
                  If you dont word wrap, then text will be cut when less screen space is available.

                  Do you u mean that text should shrink to fit inside the available widget rect?

                  AhtiA Offline
                  AhtiA Offline
                  Ahti
                  wrote on last edited by Ahti
                  #8

                  @mrjj yeah that is what i want although with word warp it appears on the next line that i don't want to happen i just want it appear in a single line and at the same time it should shrink to fit inside the available widget rect ??

                  this is how i draw text :

                  QLabel* EyeCare::constructLabel(QWidget *parent,QLabel *labelName,QString style,
                                                       int x,int y,int w,int h,QString labelText){
                  
                      labelName = new QLabel(parent) ;
                      labelName->setStyleSheet( style );
                      labelName->setGeometry(x,y,w,h);
                      labelName->setText(labelText);
                      labelName->setAlignment(Qt::AlignCenter);
                      return labelName ;
                  }
                  ...
                  notificationHeader = constructLabel(homeBackground,notificationHeader,
                                                           "color: #c8c8c8;",170,135,300,50,"Welcome To EyeCare") ;     
                       notificationHeader->setAlignment(Qt::AlignCenter);
                       homeLayout->addWidget(notificationHeader,3,0);
                  
                       notificationArea = constructLabel(homeBackground,notificationArea,"color: #c8c8c8;",
                                                         0,188,640,45,"Protect your precious eyes as you compute.") ;
                  ...
                  

                  what is a signature ?? Lol

                  1 Reply Last reply
                  0
                  • mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by mrjj
                    #9

                    Hi
                    you can use

                    QFontMetrics fm(font());
                    and fm.width(text()) to check size of text
                    if too wide, then you must reduce the font.setPointSize and see again.
                    Repeat until it fits.

                    https://stackoverflow.com/questions/40861305/dynamically-change-font-size-of-qlabel-to-fit-available-space

                    1 Reply Last reply
                    2

                    • Login

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