I would like to have my text in QLabel somewhere between bold and normal style and I believe that setting font-weight should be the answer to my problem.
In Qt documentation, I have found out that there are two options how to change font-weight:
From cpp side via: QFont::setWeight() method which accepts numbers 0-99
http://doc.qt.io/qt-4.8/qfont.html#Weight-enum
From Qss style via: font-weight attribute, which accepts numbers 100,200,...,900
http://doc.qt.io/qt-4.8/stylesheet-reference.html#font-weight
I have tried both methods and nothing seems to work. I always get only normal or the ordinary bold style and nothing in between.
Example:
QLabel* test1 = new QLabel("Font-weight testing");
test1->show();
QLabel* test2 = new QLabel("Font-weight testing");
QFont font = test2->font();
font.setWeight(40);
test2->setFont(font);
test2->show();
QLabel* test3 = new QLabel("Font-weight testing");
test3->setStyleSheet("font-weight: 400");
test3->show();
In the example above, I have created 3 labels. One without any additional setting, one where I have changed font weight via setWeight method, and one where the font-weight should be changed via Qss style. But all three will end up being exactly the same.
I have even tried to make font bigger, enable antialiasing, or use different font but nothing helped.
The QFont::setWeight method expects its input value to be one of the QFont::Weight enum values.
http://doc.qt.io/qt-5/qfont.html#setWeight
The correct version:
QLabel* test2 = new QLabel("Font-weight testing");
QFont font = test2->font();
font.setWeight(QFont::Bold);
test2->setFont(font);
Also you have two errors in the QSS version. First, you didn't specify a selector for your rule. Second, the value of 400 corresponds to 'normal' font.
https://developer.mozilla.org/en-US/docs/Web/CSS/font-weight
The correct version:
QLabel* test3 = new QLabel("Font-weight testing");
test3->setStyleSheet("QLabel { font-weight: bold; }");
Use function setWeight like this: setWeight(QFont::ExtraBold);
QFont font;
font.setWeight(QFont::ExtraBold); // set font weight with enum QFont::Weight
font.setPixelSize(25); // this for setting font size
ui->label->setFont(font);
void QFont::setWeight(int weight):
Sets the weight the font to weight, which should be a value from the QFont::Weight enumeration.
Related
I Want to have a bigger QMessageBox and centered texts in it but when I increase the size of it by stylsheet it'll be like this:
if I could give it Some Padding or margin it would be fixed but I can't.
void MainWindow::showMsg()
{
QMessageBox m_MsgBox;
m_MsgBox.setWindowFlags(Qt::Window | Qt::FramelessWindowHint);
m_MsgBox.setIcon(QMessageBox::Warning);
m_MsgBox.setText("Your Trial is finished! Please purachase a plan.");
m_MsgBox.setStandardButtons(QMessageBox::Ok);
m_MsgBox.setStyleSheet("QLabel{min-width:200 px; font-size: 13px;} QPushButton{ width:25px; font-size: 13px; }");
if(m_MsgBox.exec() == QMessageBox::Ok)
m_MsgBox.close();
}
I want to give different css properties to each QLabel(QMessageBox::Warning & setText) in this QMessageBox.
You can address specific sub-widgets within a widget by their object name:
QLabel#MyLabel { style... }
QWidget* p_widget = new QWidget;
QLabel* p_label1 = new QLabel;
p_label1->setText("I'm not cool.");
QLabel* p_label2 = new QLabel;
p_label2->setObjectName("coolLabel");
p_label2->setText("I'm VERY cool.");
QVBoxLayout* p_layout = new QVBoxLayout;
p_layout->addWidget(p_label1);
p_layout->addWidget(p_label2);
p_widget->setLayout(p_layout);
p_widget->setStyleSheet("QLabel {color: black;} QLabel#coolLabel {color: red;}");
This example should work.
I want to stress though, that setting sizes and layouts through QSS is a bad idea. Get comfortable with QLayouts, they are very powerful and dynamic!
btw: "Your trial has ended. Please purchase a plan."
I got this answer in qt forum:
Only answering to your topic title, if you look into the source code of QMessageBox, every label has a object name, so that should be easy to set different style to them by using ID selector.
text: "qt_msgbox_label"
icon: "qt_msgboxex_icon_label"
informativeText: "qt_msgbox_informativelabel"
Note: These names may change in future versions.
I'm adding actions to a QToolBar only using icons and an empty text, and I want to change each action style when it is triggered (specifically, changing its border color):
toolbar = new QToolBar;
action1 = toolbar->addAction(my_icon1, "");
action2 = toolbar->addAction(my_icon2, "");
QObject::connect(action1, &QAction::triggered, [this]{
// change border color of action1
// unset border color of action2
});
QObject::connect(action2, &QAction::triggered, [this]{
// change border color of action2
// unset border color of action1
});
But since a QIcon is not a widget (not a QAction of course), I don't know where to set the style of a specific action, and QAction::associatedWidget() returns the QToolBar widget and not the associated button that owns the icon.
I'm using only C++ code, without QML or ui files.
Since QAction::parentWidget and QAction::associatedWidgets both contain the QToolBar instead of the actual action widget, I didn't give initial credit to QToolBar::widgetForAction (I thought it would be some kind of convenient function for the QAction:: methods above). But it deserves it, because it returns the actual widget for that action, as the function name says:
toolbar->widgetForAction(action1)->setStyleSheet
("QWidget { border: 1px solid blue; }");
I have to apply drop shadow for multiple QLabel in my Application. I used QGraphicsDropShadowEffect and it is working fine, if i am adding it for one QLabel. I tried applying the same Graphics effect for two QLabels.
QGraphicsDropShadowEffect* effect = new QGraphicsDropShadowEffect();
effect->setColor(Qt::white);
effect->setBlurRadius(0);
effect->setXOffset(1);
effect->setYOffset(0);
QLabel* label = new QLabel();
label->setText("QLabel1");
label->setGraphicsEffect(effect);
QLabel* label2 = new QLabel();
label2->setText("QLabel2");
label2->setGraphicsEffect(effect);
In this case,the shadow effect is applied only to label2.
I tried creating two different QGraphicsDropShadowEffect objects and setting the QLabels with that.
QGraphicsDropShadowEffect* effect = new QGraphicsDropShadowEffect();
effect->setColor(Qt::white);
QLabel* label = new QLabel();
label->setText("QLabel1");
label->setGraphicsEffect(effect);
QGraphicsDropShadowEffect* effect1 = new QGraphicsDropShadowEffect();
effect1->setColor(Qt::white);
QLabel* label2 = new QLabel();
label2->setText("QLabel2");
label2->setGraphicsEffect(effect1);
In this case, Application is crashing in QRasterPaintEngine::transformChanged() call.
Any idea on how to fix this issue?
I am using qt 5.3.
Was just trying to solve this myself. As much as this solution pains me it's the best I came up with quickly. Use QList to help yourself out here:
// List instances containing labels and drop shadows
QList<QLabel*> label_list_;
QList<QGraphicsDropShadowEffect*> shadow_list_;
// Get all UI labels and apply shadows
label_list_ = this->findChildren<QLabel*>();
foreach(QLabel *lbl, label_list_) {
shadow_list_.append(new QGraphicsDropShadowEffect);
shadow_list_.back()->setBlurRadius(10);
shadow_list_.back()->setOffset(3, 3);
lbl->setGraphicsEffect(shadow_list_.back());
}
If don't want all UI labels in your list you can add manually with append
How do I set a drop-shadow effect on a QPushButton text?
I could set shadow on the entire QPushButton using QGraphicsDropShadowEffect, I however, am not able to find a way to directly set the effect of text inside the QButton.
Edit:
I tried the following, not sure if the syntax is correct
in the .qss file:
MyButton::text
{
shadow: palette(dark);
}
I set the button's drop shadow effect by:
QGraphicsDropShadowEffect* effect = new QGraphicsDropShadowEffect( );
effect->setBlurRadius( 5 );
mStartButton->setGraphicsEffect( effect );
Try this:
Set a QLabel iside QPushButton rather than simple text. Then apply shadow effect to the label.
You may need to add extra code for centering the label inside the pushbutton.
mStartButton->setText("");
QLabel *label = new QLabel(mStartButton);
label->setText("<b>Button</b>");
QGraphicsDropShadowEffect* effect = new QGraphicsDropShadowEffect( );
effect->setBlurRadius( 5 );
label ->setGraphicsEffect( effect );
Okay I'm working on a GUI with Qt Designer for c++, and I have a bunch of labels setup to display some content from a file once it is loaded.
On the labels that are used to display loaded content, I currently have them set to "set later", and in the initial GUI configuration, I call ui->label_id.setText(""); on each of the labels before they can be seen. I really just want them to have some text so I can visually see them in the designer, yet not have any when the program is running before content is loaded.
So my question is: Is this the best way to handle this? Or is there a character I can set the text to in designer that won't appear at runtime?
With more and more labels as my gui expands, I don't want to bog down the loading time of the program setting the text to "" for every one, so that's why I'm here.
One possible solution could be to set a color to that labels with a transparent color (0 alpha value).
This can be handled via a style sheet in the widget constructor:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
setStyleSheet("QLabel { color: qrgba(255, 255, 255, 0); }");
}
If you want to selectively apply a stylesheet to a label, you could use all kinds of Qt Style Sheet Selector Types.
You could use the ID Selector for example. If you've set in QtDesigner the objectName attribute of a label to label_1, you hide this label selectively by setting this style sheet:
setStyleSheet("#label_1 { color: qrgba(255, 255, 255, 0); }");
A more advanced solution would be a method that returns the style sheet for the object names:
QString MainWindow::styleSheetForHiddenLabels(const QStringList &labelObjectNames)
{
QString styleSheet;
foreach (QString labelName, labelObjectNames) {
QString style("#");
style.append(labelName);
style.append(" { color: qrgba(255, 255, 255, 0); }");
styleSheet.append(style);
}
return styleSheet;
}
This method could be used this way:
QStringList labelNames;
labelNames << "label1" << "label2" << "label3";
QString stylesheet = styleSheetForHiddenLabels(labelNames);
setStyleSheet(stylesheet);
An alternative to the stylesheet answer.
QList<QLabel*> widgets = this->findChildren<QLabel*>();
for (QLabel *label : widgets) {
label->setText("");
}
place this in your MainWindow class constructor. It will get a list of all QLabels from your window, you can then cycle through them and set the text to "".
You can use Object inspector. In Object inspector you'll se a list of your labels, and when you click on some of your labels you'll see border around your widget. That way you can see your labels without putting any text in it.