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



  • 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.....



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



  • 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



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


  • Moderators

    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.



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


  • Moderators

    @
    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>");
    @



  • 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..


  • Moderators

    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));
    @



  • Thanks a lot Chris, both work perfect.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.