I'm trying to create a custom widget (itself containing some child widgets) in Qt Creator with Qt Designer.
In the designer I set the styleSheet property for the derived object ControlBar
to the following values:
QWidget{
font-family: "Segoe UI";
font-size: 9;
}
QWidget#ControlBar{
background-color: #3a3a3a;
border-width: 5px;
border-radius: 4px;
border-style: solid;
border-color: #ffffff;
}
Everything looks fine now in designer as well as in the preview mode (Shift+Alt+R).
My intention now is to create an instance of ControlBar
at runtime and assign it to the main vertical layout of a MainWindow
instance:
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ControlBar *controlBar = new ControlBar;
ui->verticalLayout->insertWidget(0, controlBar);
}
Albeit the styling works as expected for every single sub-widget of ControlBar, the given background color (the background-color property of ControlBar
's styleSheet in particular) is not applied to the control bar, as well as all the other background-related properties. Instead, the background color and styling of MainWindow
is used, whereas all the sub-widget-related styling is working as expected.
How can I get rid of this behaviour and make ControlBar
have it's intended background color?
Styles sheets are textual specifications that can be set on the whole application using QApplication::setStyleSheet() or on a specific widget (and its children) using QWidget::setStyleSheet(). If several style sheets are set at different levels, Qt derives the effective style sheet from all of those that are set.
ui->graphicsView->setBackground( Qt::GlobalColor::transparent );
ui file is used to create a ui_calculatorform. h file that can be used by any file listed in the SOURCES declaration. Note: You can use Qt Creator to create the Calculator Form project. It automatically generates the main.
QWidget#ControlBar
refers to any widget with the object name "ControlBar".
Hence it it works in the designer and preview, but not as a child of something else.
To solve this, use
ControlBar{
background-color: #3a3a3a;
}
instead and make sure to inherit ControlBar
from QFrame
and not form QWidget
. If for some reason you cannot, you have to override the paintEvent
. See this answer for the exact code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With