Style for custom QWidget
-
Sorry this took so long, way too much soccer going on this weekend :)
I am using Qt Creator 3.1.2 (opensource)
Based on Qt 5.3.1 (GCC 4.6.1, 64 bit)In the example if I switch the 2 commented out lines to be QGroupBox the stylesheet works perfectly. If I use the QWidget then it doesn't work at all.
@
// This is ToneWidget.h
class ToneWidget : public QWidget
//class ToneWidget : public QGroupBox
{
Q_OBJECT
public:
ToneWidget(QWidget* parent = 0);
};
@
@
// This is ToneWidget.cpp
#include "ToneWidget.h"ToneWidget::ToneWidget(QWidget* parent) : QWidget(parent)
//ToneWidget::ToneWidget(QWidget* parent) : QGroupBox(parent)
{
setFixedWidth(100);
setObjectName("bar");
setStyleSheet("QWidget#bar {border: 2px solid red; border-radius: 2px}");
}
@
@
//This is mainwindow.cpp
#include "mainwindow.h"
#include "ToneWidget.h"MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
ToneWidget *tone = new ToneWidget();
setCentralWidget(tone);
}MainWindow::~MainWindow()
{}
@ -
Just to be sure, with only this code it doesn't work ? What are your other style sheets ?
-
With this code it doesn't work for QWidget and it does for QGroupBox. The other stylesheets I use that work are for QGroupBox. For QGroupBox I use the border section and the QGroupBox::title selector.
-
This code is working fine here. Can you post all your style sheets ?
-
I have changed everything to use QGroupBox temporarily and it seems to work. I wonder if it is a problem with my environment. I am using ubunto linux as a host. It supprised me that that simple of an application caused the issue to show.
-
Does it also happen if you set this style sheet on a default project ? Just create a new one using a QDialog as base class and set the style sheet on it
-
I made the dialog example and I think I found the following:
If I use a plain QWidget the stylesheets work fine.
If I use a custom widget that inherits from QWidget the style sheets do not work.
If I use a custom widget that inherits from QGroupBox it does work. Just the same as my other example.
@
Dialog::Dialog(QWidget *parent)
: QDialog(parent) {
QVBoxLayout *mainLayout = new QVBoxLayout;
setLayout(mainLayout);
//QWidget *widget = new QWidget(this);
//QGroupBox *widget = new QGroupBox(this);
ToneWidget *widget = new ToneWidget(this);
widget->setFixedWidth(100);
widget->setFixedHeight(100);
//widget->setObjectName("Foo");
//widget->setStyleSheet("QWidget#Foo {border: 2px solid red; border-radius: 2px}");
//widget->setStyleSheet("QGroupBox#Foo {border: 2px solid red; border-radius: 2px}");
// Try to set it externally
//widget->setStyleSheet("ToneWidget#Foo {border: 2px solid red; border-radius: 2px}");
mainLayout->addWidget(widget);
}
@@
class ToneWidget : public QWidget
//class ToneWidget : public QGroupBox
{
Q_OBJECT
public:
ToneWidget(QWidget* parent = 0);
};
@@
ToneWidget::ToneWidget(QWidget* parent) : QWidget(parent)
//ToneWidget::ToneWidget(QWidget* parent) : QGroupBox(parent)
{
setFixedWidth(100);
setFixedHeight(100);setObjectName("bar"); setStyleSheet("QWidget#bar {border: 2px solid red; border-radius: 2px}"); //setStyleSheet("QGroupBox#bar {border: 2px solid red; border-radius: 2px}"); //setStyleSheet("ToneWidget#bar {border: 2px solid red; border-radius: 2px}");
}
@ -
Which OS are you running on ? I can't make it fail here
-
I am using ubuntu the latest 64 bit version. 14.3 or 14.4 I will check again when I get home.
-
I will take it to work tomorrow and try it on a windows machine. I will be depressed if it works on windows and not on linux.
-
"From the Qt 5 documentation"http://qt-project.org/doc/qt-5/stylesheet-reference.html
QWidget Supports only the background, background-clip and background-origin properties.
I should have read this a long time ago. Sorry to waste your time.
-
Which one were you trying to use ?
-
setStyleSheet("QWidget {border: 2px solid red; border-radius: 2px}");
-
This one should be working
-
I just did the same experiment on a Windows machine with Qt Creator 3.1.2
Based on Qt 5.3.1 (MSVC 2010, 32 bit) with the same device. If I take any QWidget and apply the above stylesheet and it doesn't work. If I change the QWidget to a QGroupBox with the appropriate change in the stylesheet the border does show.I have just changed all of the QWidgets that I need a border around to QGroupBox for now. Should I mark this thread closed?