PDA

View Full Version : font style and size in QString



uz_qt
26th November 2013, 14:24
Hi,

How can I make the font style bold in the QString? I have written the following lines:

QString color;
QString text;
QString fonttemplate0 = QString(tr("<font color='yellow'>Magnitude: %1").arg(0));
d_mrk->setLabel(fonttemplate0.arg(color, text) );//d_mrk is a plot marker

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

^NyAw^
26th November 2013, 14:41
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()

uz_qt
26th November 2013, 14:57
But in the above lines, font color works. It is set to yellow! It has something to do with HTML scripts i guess.

^NyAw^
26th November 2013, 15:13
Hi,

Beacuse QWidget interprets "<font color='yellow' ...>" as CSS.

uz_qt
26th November 2013, 15:21
oh ok. Then where can I insert QFont in the above lines of code?

^NyAw^
26th November 2013, 15:45
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.

uz_qt
26th November 2013, 19:51
Hi,
sorry but I could not attach QLabel to the marker d_mkr.
Could u please show it with small piece of code?

^NyAw^
27th November 2013, 09:34
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:



QwtText qText = "My text";
QFont qFont = qText.font(); // get the current font
qFont.setBold(true); //set the current font to bold
qFont.setPointSize(10); //set the font point size to 10
qText.setFont(qFont); //set the new font to the QwtText object
d_mrk->setLabel(qText);


This is not a working code, have to try it.

uz_qt
27th November 2013, 10:08
Hi,
thanks for your sample code. I have used QwtText before as shown below:

QwtText text1("Magnitude: ");
text1.setFont(QFont("Helvetica", 10, QFont::Bold));
text1.setColor(QColor(Qt::yellow));
d_mrk1->setLabel(text1);

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:


QwtText text1(QString("Magnitude: %1").arg(0));
text1.setFont(QFont("Helvetica", 10, QFont::Bold));
text1.setColor(QColor(Qt::yellow));
d_mrk1->setLabel(text1);

Thanks a lot! :-)