Welcome, Guest. Please login or register.
Did you miss your activation email?

Author Topic: Set bold  (Read 4800 times)

ann

  • Newbie
  • *
  • Posts: 16
    • View Profile
Set bold
« 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.

Subst

  • Sr. Member
  • ****
  • Posts: 450
    • View Profile
Re: Set bold
« Reply #1 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";
 }

ann

  • Newbie
  • *
  • Posts: 16
    • View Profile
Re: Set bold
« Reply #2 on: May 16, 2018, 12:09:46 pm »
Hey Subst, you save my day  :) the solution work. Thank you very much.

Arin Alex

  • Developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 1005
    • View Profile
Re: Set bold
« Reply #3 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
« Last Edit: May 16, 2018, 01:07:56 pm by Arin Alex »

Arin Alex

  • Developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 1005
    • View Profile
Re: Set bold
« Reply #4 on: May 16, 2018, 12:16:48 pm »
Subst Why 75 instead true ?  :o

ann

  • Newbie
  • *
  • Posts: 16
    • View Profile
Re: Set bold
« Reply #5 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


Arin Alex

  • Developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 1005
    • View Profile
Re: Set bold
« Reply #6 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 :)

Subst

  • Sr. Member
  • ****
  • Posts: 450
    • View Profile
Re: Set bold
« Reply #7 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
« Last Edit: May 16, 2018, 05:17:08 pm by Subst »

ann

  • Newbie
  • *
  • Posts: 16
    • View Profile
Re: Set bold
« Reply #8 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.