Results 1 to 9 of 9

Thread: font style and size in QString

  1. #1
    Join Date
    Aug 2013
    Posts
    41
    Thanks
    4
    Qt products
    Qt5
    Platforms
    Windows

    Default font style and size in QString

    Hi,

    How can I make the font style bold in the QString? I have written the following lines:
    Qt Code:
    1. QString color;
    2. QString text;
    3. QString fonttemplate0 = QString(tr("<font color='yellow'>Magnitude: %1").arg(0));
    4. d_mrk->setLabel(fonttemplate0.arg(color, text) );//d_mrk is a plot marker
    To copy to clipboard, switch view to plain text mode 

    What line should be added in tr() so as to make the font style bold and also set the font size to 10pt?

  2. #2
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: font style and size in QString

    Hi,

    QString is only for string purposes, so it will not contain information about font size, color, ...

    Take a look at QFont and QWidget::setFont()
    Òscar Llarch i Galán

  3. #3
    Join Date
    Aug 2013
    Posts
    41
    Thanks
    4
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: font style and size in QString

    But in the above lines, font color works. It is set to yellow! It has something to do with HTML scripts i guess.

  4. #4
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: font style and size in QString

    Hi,

    Beacuse QWidget interprets "<font color='yellow' ...>" as CSS.
    Òscar Llarch i Galán

  5. #5
    Join Date
    Aug 2013
    Posts
    41
    Thanks
    4
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: font style and size in QString

    oh ok. Then where can I insert QFont in the above lines of code?

  6. #6
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: font style and size in QString

    Hi,

    Have your readed my first post? Use QFont class and set it to your QLabel.

    You can also use QDesigner to change the Font of a QWidget. Change it, uic the UI file and take a look at generated cpp file. There you could see how QDesigner changes the font of a widget.
    Last edited by ^NyAw^; 26th November 2013 at 16:07.
    Òscar Llarch i Galán

  7. #7
    Join Date
    Aug 2013
    Posts
    41
    Thanks
    4
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: font style and size in QString

    Hi,
    sorry but I could not attach QLabel to the marker d_mkr.
    Could u please show it with small piece of code?

  8. #8
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: font style and size in QString

    Hi,

    I never used it, but having a look at Qwt plot marker docs:

    "QwtPlotMarker::setLabel(const QwtText &)" -> this expects a QwtText, so take a look at QwtText doc.

    QwtText class hav two interesting methods to do what you want:

    Qt Code:
    1. QwtText qText = "My text";
    2. QFont qFont = qText.font(); // get the current font
    3. qFont.setBold(true); //set the current font to bold
    4. qFont.setPointSize(10); //set the font point size to 10
    5. qText.setFont(qFont); //set the new font to the QwtText object
    6. d_mrk->setLabel(qText);
    To copy to clipboard, switch view to plain text mode 

    This is not a working code, have to try it.
    Òscar Llarch i Galán

  9. #9
    Join Date
    Aug 2013
    Posts
    41
    Thanks
    4
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: font style and size in QString

    Hi,
    thanks for your sample code. I have used QwtText before as shown below:
    Qt Code:
    1. QwtText text1("Magnitude: ");
    2. text1.setFont(QFont("Helvetica", 10, QFont::Bold));
    3. text1.setColor(QColor(Qt::yellow));
    4. d_mrk1->setLabel(text1);
    To copy to clipboard, switch view to plain text mode 

    This works perfectly fine. The reason I switched to QString is because I wanted to use arg() so that I can have the string updated with the integer in the arg() as shown in my previous posts.

    Now, I realized I can use QString inside QwtText, therefore I got the following solution now:

    Qt Code:
    1. QwtText text1(QString("Magnitude: %1").arg(0));
    2. text1.setFont(QFont("Helvetica", 10, QFont::Bold));
    3. text1.setColor(QColor(Qt::yellow));
    4. d_mrk1->setLabel(text1);
    To copy to clipboard, switch view to plain text mode 

    Thanks a lot! :-)
    Last edited by uz_qt; 27th November 2013 at 10:19.

Similar Threads

  1. Replies: 2
    Last Post: 25th April 2013, 14:25
  2. Replies: 1
    Last Post: 30th March 2012, 16:46
  3. Replies: 6
    Last Post: 29th December 2011, 18:37
  4. Font Height and width based on font size
    By Ghufran in forum Qt Programming
    Replies: 1
    Last Post: 31st July 2010, 08:02
  5. change font size and button size of QMessageBox
    By nass in forum Qt Programming
    Replies: 6
    Last Post: 13th September 2006, 19:16

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.