LimeReport Forum

General Category | Основное => Discussion | Обсуждение => Topic started by: ann on May 16, 2018, 09:41:38 AM

Title: Set bold
Post by: ann on May 16, 2018, 09:41:38 AM
Hi all. I am trying to set the font to bold through text item editor. Below is the sample code to set the text color

if($D{tablewidget.intimestatus}==2 && getVariable("colourFlag") == 0){
THIS.fontColor=QColor('green');
}

So is there any code to set the text to bold?. Thanks in advance.
Title: Re: Set bold
Post by: Subst on May 16, 2018, 11:12:28 AM
Hi!

for example

$S{
 var font=QFont("Arial",12,false,75); // Arial, 12 points - size, not italic, bold

 if (1)
  THIS.font=font;
"klop";
 }
Title: Re: Set bold
Post by: ann on May 16, 2018, 12:09:46 PM
Hey Subst, you save my day  :) the solution work. Thank you very much.
Title: Re: Set bold
Post by: Arin Alex on May 16, 2018, 12:11:32 PM
Hi!
If you want to modify existing font, you can write font helper then register it in script engine and use it in script.
For example:
Code: [Select]
class ScriptFontHelper: public QObject{
    Q_OBJECT
public:
    ScriptFontHelper(QObject* parent = 0):QObject(parent){}
    Q_INVOKABLE QFont setBold(QFont font, bool bold){ font.setBold(bold); return font;}
    Q_INVOKABLE QFont setItalic(QFont font, bool italic){ font.setItalic(italic); return font;}
};

.....
ScriptFontHelper* fontHelper = new ScriptFontHelper(this);
QScriptValue jsFontHelper = report->scriptManager()->scriptEngine()->newQObject(fontHelper);
report->scriptManager()->scriptEngine()->globalObject().setProperty("FontHelper", jsFontHelper);
in script
Code: [Select]
$S{THIS.font = FontHelper.setBold(THIS.font,true);""}
Test
Title: Re: Set bold
Post by: Arin Alex on May 16, 2018, 12:16:48 PM
Subst Why 75 instead true ?  :o
Title: Re: Set bold
Post by: ann on May 16, 2018, 01:05:51 PM
Hi Arin Alex, thanks for the suggestion. Does the script perform better? Or else I will stick to current code because script seems like complex to me    :P

Title: Re: Set bold
Post by: Arin Alex on May 16, 2018, 01:21:19 PM
My solution is more universal :)
If you write THIS.font=QFont("Arial",12,false,true); it will always Arial,12 independently from TextItem font settings.
But you can use both methods as you wish :)
Title: Re: Set bold
Post by: Subst on May 16, 2018, 05:13:48 PM
To Alex


Constant Value Description
QFont::Thin 0 0
QFont::ExtraLight 12 12
QFont::Light 25 25
QFont::Normal 50 50
QFont::Medium 57 57
QFont::DemiBold 63 63
QFont::Bold 75 75
QFont::ExtraBold 81 81
QFont::Black 87 87

And in script work fine QFont(const QString &family, int pointSize = -1, bool italic = false, int weight = -1) instead QFont(const QString &family, int pointSize = -1, int weight = -1, bool italic = false). I don't know why. Empirical way
Title: Re: Set bold
Post by: ann on May 21, 2018, 04:36:57 AM
My solution is more universal :)
If you write THIS.font=QFont("Arial",12,false,true); it will always Arial,12 independently from TextItem font settings.
But you can use both methods as you wish :)

I see. Currently I do not need a versatile build yet. But still thank you for the suggestion, might use it next time.