PDA

View Full Version : problem with Font changing



phillip_Qt
21st April 2008, 10:46
Hi All.
In my application i'm changing the colour of window by applying .qss.
but while applying default.qss UI's font is remaining un affected . but while applying Pagefold.qss font is getting changed from ariel-12 to default or some thing.
i tried to solve like following
QFont font("font: Arial", 12, QFont::System);
qApp->setFont(font);
bhut its changing whole application font.
I need to change only QLabel font to arial-12.
Can any body help me. :(

sonuani
22nd April 2008, 05:36
Try like QFont font("font: Arial", 12, QFont:referDefault);
qApp->setFont(font);"

phillip_Qt
22nd April 2008, 05:57
Try like QFont font("font: Arial", 12, QFont:referDefault);
qApp->setFont(font);"

I tried like this. but its changing size of all the ui linked to main ui. But i wanna to change the letter font size of only one. and ui name is Base. How to do it?:(

mchara
22nd April 2008, 07:26
Hi, why not using just

Try like QFont font("font: Arial", 12, QFont:referDefault);
ui.someLabel->setFont(font);

or something like this to change font only for a widgets you need?


qApp()->setFont will always change font for entire application and if i understood correctly, you need to change font only for several widgets, so you should use setFont method of those widgets.

If you intend to change font in whole ui (not concrete widgets) u may use styleSheet.
The simplest way to use a styleSheet is to open ui in qtdesigner, and paste something like

QLabel{font: bold italic "Times New Roman"; font-size:16}

in styleSheet Property of main ui's widget.

phillip_Qt
22nd April 2008, 08:17
Hi, why not using just

Try like QFont font("font: Arial", 12, QFont:referDefault);
ui.someLabel->setFont(font);

or something like this to change font only for a widgets you need?


qApp()->setFont will always change font for entire application and if i understood correctly, you need to change font only for several widgets, so you should use setFont method of those widgets.

If you intend to change font in whole ui (not concrete widgets) u may use styleSheet.
The simplest way to use a styleSheet is to open ui in qtdesigner, and paste something like

QLabel{font: bold italic "Times New Roman"; font-size:16}

in styleSheet Property of main ui's widget.


Thanx. I tried like this. but the programm is getting terminated abnormaly.:(

mchara
22nd April 2008, 11:12
Which way did you tried?

if u are setting widgets font with
Try like QFont font("font: Arial", 12, QFont:referDefault);
ui.someLabel->setFont(font);

make sure it is done after setupUi() (in constructor) because setupUi() creates widgets in your ui - calling anything like ui.widget->method() before widget is initialized will cause crash.

if this isn't a reason try give a bit more detailed description of crash you have.

would be easier to find a solution if u would write anything about the problem ;)
so which method did u tried, when application crashes, where(maybe small part of code?), what debugger says etc.

phillip_Qt
22nd April 2008, 12:09
Which way did you tried?

if u are setting widgets font with
Try like QFont font("font: Arial", 12, QFont:referDefault);
ui.someLabel->setFont(font);

make sure it is done after setupUi() (in constructor) because setupUi() creates widgets in your ui - calling anything like ui.widget->method() before widget is initialized will cause crash.

if this isn't a reason try give a bit more detailed description of crash you have.

would be easier to find a solution if u would write anything about the problem ;)
so which method did u tried, when application crashes, where(maybe small part of code?), what debugger says etc.

I tried like following
Ui::uiclassname ui; in .h
in constructor
setUp(this);
and inside function
i did like m_ui.centralwidget->setFont(font);

and its terminating abruptly.:(

mchara
22nd April 2008, 14:10
Let's be more precise, does your constuctor looks like this?

// SomeClass constructor.
SomeClass::SomeClass(QWidget *aParent)
:QWidget(aParent)
{
ui.setupUi(this);
QFont font("Arial", 16);
ui.someWidget->setFont(font);
ui.someWidget2->setFont(font);
}


because termination on ui.someWidget->setFont(font); call it's surely because ui.someWidget is not initialized correctly.

and did you tried other way with styleSheet?

phillip_Qt
23rd April 2008, 10:52
Let's be more precise, does your constuctor looks like this?

// SomeClass constructor.
SomeClass::SomeClass(QWidget *aParent)
:QWidget(aParent)
{
ui.setupUi(this);
QFont font("Arial", 16);
ui.someWidget->setFont(font);
ui.someWidget2->setFont(font);
}


because termination on ui.someWidget->setFont(font); call it's surely because ui.someWidget is not initialized correctly.

and did you tried other way with styleSheet?

I did like ui.setupUi(this);;
QFont font("font: Arial", 12, QFont::PreferDefault);
ui.centralwidget->setFont(font);

phillip_Qt
23rd April 2008, 10:53
Let's be more precise, does your constuctor looks like this?

// SomeClass constructor.
SomeClass::SomeClass(QWidget *aParent)
:QWidget(aParent)
{
ui.setupUi(this);
QFont font("Arial", 16);
ui.someWidget->setFont(font);
ui.someWidget2->setFont(font);
}


because termination on ui.someWidget->setFont(font); call it's surely because ui.someWidget is not initialized correctly.

and did you tried other way with styleSheet?

I did like ui.setupUi(this);;
QFont font("font: Arial", 12, QFont::PreferDefault);
ui.centralwidget->setFont(font);
still same problem. font is not getting changed.

mchara
23rd April 2008, 11:13
and what is centralWidget?

momesana
23rd April 2008, 15:41
If you intend to change font in whole ui (not concrete widgets) u may use styleSheet.
The simplest way to use a styleSheet is to open ui in qtdesigner, and paste something like


It is possible to apply a Stylesheet to a single widget, or to a class of widgets, or to widgets with some certain properties:



// applies to the QLabel object with the objectName "certain-label"
QLabel#certain-label {
color: blue;
font-weight: bold;
}
He could also call setStyleSheet on the widget to force a certain stylesheet for the widget after applying the other stylesheet on the application.

Stylesheets can much more than this ...

vycke
23rd April 2008, 18:36
I did like ui.setupUi(this);
QFont font("font: Arial", 12, QFont::PreferDefault);
ui.centralwidget->setFont(font);
still same problem. font is not getting changed.

Perhaps the problem is the font name here.... "font: Arial" is not "Arial"

Vycke

phillip_Qt
24th April 2008, 06:18
Perhaps the problem is the font name here.... "font: Arial" is not "Arial"

Vycke

Sorry. i didnt understrand it. U mean instead of "font: Arial" i should write "Arial" only?:confused:

mchara
24th April 2008, 06:35
It is possible to apply a Stylesheet to a single widget, or to a class of widgets, or to widgets with some certain properties:



// applies to the QLabel object with the objectName "certain-label"
QLabel#certain-label {
color: blue;
font-weight: bold;
}
He could also call setStyleSheet on the widget to force a certain stylesheet for the widget after applying the other stylesheet on the application.

Stylesheets can much more than this ...

Sure, but if i understood philip, he has several ui's in main window and want to change font only on several ui's so the best approach with stylesheets would be applying it on central widget of ui and let qt to propagate stylesheet to children.

I tried to suggest such solution to philip, but he didn't checked it i think...
So Philip, once again:
try with

ui.setupUi(this);
QString styleSheet = "QLabel{font: bold italic "Arial"; font-size:16}";
ui.centralwidget->setStyleSheet(styleSheet);

phillip_Qt
24th April 2008, 06:44
It is possible to apply a Stylesheet to a single widget, or to a class of widgets, or to widgets with some certain properties:



// applies to the QLabel object with the objectName "certain-label"
QLabel#certain-label {
color: blue;
font-weight: bold;
}
He could also call setStyleSheet on the widget to force a certain stylesheet for the widget after applying the other stylesheet on the application.

Stylesheets can much more than this ...

Hi i tried like
QLabel#m_funktion1
{
font : Arial 12x;
}

But getting compilation error.
and also SetStyleSheet(QLabel#m_funktion1{font: Arial 12x}");
is not working.:(

aamer4yu
24th April 2008, 06:58
Why dont u just set the stylesheet to the label u want ??

my_label->setStyleSheet("font:Arial ; font-size: 12px");

mchara
24th April 2008, 07:03
try with


ui.setupUi(this);
QString styleSheet = "QLabel{font: bold italic "Arial"; font-size:16}";
ui.centralwidget->setStyleSheet(styleSheet);

and you can also paste QLabel{font: bold italic "Arial"; font-size:16} in qt designer (styleSheet property of main widget) - tested it, works.

Try to use styleSheet as is and if it will work adjust it to you need - then you'll be sure that problem is with approach or with stylesheet itself.

phillip_Qt
24th April 2008, 07:06
Sure, but if i understood philip, he has several ui's in main window and want to change font only on several ui's so the best approach with stylesheets would be applying it on central widget of ui and let qt to propagate stylesheet to children.

I tried to suggest such solution to philip, but he didn't checked it i think...
So Philip, once again:
try with

ui.setupUi(this);
QString styleSheet = "QLabel{font: bold italic "Arial"; font-size:16}";
ui.centralwidget->setStyleSheet(styleSheet);

Thanx mchara very much. U are absolutly right. I've so many UIs. But i need to change only ceratin UI's font. Actually i'm chaning the colour by applyng qss file. there i "ve
QLabel
{
color : white;
font : Arial 12x;
}
But this one is not chaning the size.
Inside code as u told i"ve added
QString styleSheet ( "QLabel{font: bold italic; Arial; font-size:16}");
ui.centralwidget->setStyleSheet(styleSheet);

But this also not working. :(

phillip_Qt
24th April 2008, 07:44
Why dont u just set the stylesheet to the label u want ??

my_label->setStyleSheet("font:Arial ; font-size: 12px");
Thanx aamer. I tried like so, still same problem.

mchara
24th April 2008, 07:48
So maybe qss file overrides stylesheets used in code.

try different variations of qss file
i.e:
font : Arial
font-size: 12px

Maybe it's just a matter of invalid
font : Arial 12x;
entry is stylesheet that is applied after other.

mazurekwrc
24th April 2008, 09:13
try it:
in .qss:


QLabel{
font-family: Verdana;
font-size: 16px;
}


and in your code insert this:


your_label->setStyleSheet( "font-family: Arial; font-size : 12px;");

phillip_Qt
25th April 2008, 04:37
Hi All
Now the font is changed. I did like below
ui.m_label->setStyleSheet("QLabel#m_label1 {background-color: white;"
"color : black; border: 2px solid black;"
"font:Arial ; font-size: 20px}");

But the problem is that two images are coming. one in default font and another in Arial font. So the text is not visible properly. What ll i do to solve this?