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 to add a new line at a particular position in QLabel
QtWS25 Last Chance

How to add a new line at a particular position in QLabel

Scheduled Pinned Locked Moved General and Desktop
10 Posts 4 Posters 38.8k 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.
  • D Offline
    D Offline
    Dcqt
    wrote on 14 Apr 2014, 19:49 last edited by
    #1

    Hi,

    I am using single QLabel to represent two different informations.
    to generate some space between two infos i am adding some space , but it does not work in all scenarios.

    Can i make QLabel to store the second info in a new line with some sufficient gap between first and second infomations?

    @
    QString ename = (const char *)eventData.event_list[row].event_name;
    QString edesc = (const char *)eventData.event_list[row].short_description;
    ename.append(" ");
    ename.append(edesc);
    evnt_name_label->setText( ename );
    @

    please reply.....

    1 Reply Last reply
    0
    • C Offline
      C Offline
      ChrisW67
      wrote on 14 Apr 2014, 22:45 last edited by
      #2

      QLabel will wrap text on new line characters if wordWrap is on.
      @
      label->setText("Line 1\nLine 2\n\nLine 4");
      @

      1 Reply Last reply
      0
      • D Offline
        D Offline
        Dcqt
        wrote on 15 Apr 2014, 08:19 last edited by
        #3

        Thanks, i tried the same with append function , it does not work , but if i if use setText function its wokring..

        then , one more i would like to know.

        i want to set the different font sizes for different texts, where the text will be decided run time.

        how to do it

        1 Reply Last reply
        0
        • M Offline
          M Offline
          musimbate
          wrote on 15 Apr 2014, 08:28 last edited by
          #4

          Hi,
          Have you tried using <br/> tags inside your strings?

          Why join the navy if you can be a pirate?-Steve Jobs

          1 Reply Last reply
          0
          • C Offline
            C Offline
            Chris Kawa
            Lifetime Qt Champion
            wrote on 15 Apr 2014, 09:03 last edited by
            #5

            QLabel accepts a "limited subset":https://qt-project.org/doc/qt-5/richtext-html-subset.html of html formatting. You can wrap the parts you need in <font> tags.

            1 Reply Last reply
            0
            • D Offline
              D Offline
              Dcqt
              wrote on 15 Apr 2014, 11:38 last edited by
              #6

              please give me some examples , i am not a frequent user of Qt, and i dont understand these syntaxes properly..

              1 Reply Last reply
              0
              • C Offline
                C Offline
                Chris Kawa
                Lifetime Qt Champion
                wrote on 15 Apr 2014, 11:47 last edited by
                #7

                @
                label->setText("<small>I'm small</small><br>"
                "I'm average :(<br>"
                "<big>I'm big!</big><br>"
                "<font size=15>I'm 15!</font>");
                @

                1 Reply Last reply
                0
                • D Offline
                  D Offline
                  Dcqt
                  wrote on 15 Apr 2014, 14:58 last edited by
                  #8

                  I m sorry to ask you again.. but i want to have these setting on text that is decided during runtime.
                  i tried

                  @
                  QString big("I'm Big");
                  QString small("I'm small");

                  label->setText("<font size=15>big</font><br>"
                  "<font size=10>small</font>");
                  @

                  i am getting the big and small as out put instead of I'm Big and
                  I'm small.

                  how to do it..

                  1 Reply Last reply
                  0
                  • C Offline
                    C Offline
                    Chris Kawa
                    Lifetime Qt Champion
                    wrote on 15 Apr 2014, 15:16 last edited by
                    #9

                    That's not a Qt thing, that's c++ thing. What you did was like
                    @int i = 42;
                    std::string("foo i bar"); //this won't be "foo 42 bar"@

                    Just concatenate strings
                    @
                    QString big("I'm Big");
                    QString small("I'm small");
                    label->setText(QString("<font size=15>") + big +
                    "</font><br><font size=10>" + small + "</font>");
                    @
                    or more readable with arg() method of QString:
                    @
                    QString big("I'm Big");
                    QString small("I'm small");
                    QString text("<font size=15>%1</font><br><font size=10>%2</font>");
                    label->setText(text.arg(big, small));
                    @

                    1 Reply Last reply
                    0
                    • D Offline
                      D Offline
                      Dcqt
                      wrote on 15 Apr 2014, 19:48 last edited by
                      #10

                      Thanks a lot Chris, both work perfect.

                      1 Reply Last reply
                      0

                      9/10

                      15 Apr 2014, 15:16

                      • Login

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