Unsolved Subclass of QWidget not rendering stylesheet
-
Currently, I am unable to style a subclass of a QWidget using a .qss file where I load before my QMainWindow is created in main(). The QWidget subclass is called "Tab".
I am using a namespace called "TabInterface". Container is a subclass of a QListWidget that contains a multiple "Tab" instances.
namespace TabInterface { ... /** * @class Tab * Individual tab widget class */ class Tab : public QWidget { Q_OBJECT public: Tab(const TabContents &contents, QWidget *parent = 0) : _contents(contents), _layout(new QHBoxLayout(this)), _text(new QLabel(this)), _icon(new QLabel(this)), _indicator(new QLabel(this)), QWidget(parent) { buildLayout(); updateInterface(); } virtual ~Tab() { } void setId(int id) { _id = id; } inline int getId() const { return _id; } void setUserInfo(const QByteArray &userInfo) { _userInfo = userInfo; } inline const QByteArray &getUserInfo() const { return _userInfo; } void setContents(const TabContents &contents) { _contents = contents; updateInterface(); } inline const TabContents &getContents() const { return _contents; } protected: /** * Override paint event to allow stylesheet drawing */ void paintEvent(QPaintEvent *) { QStyleOption option; option.init(this); QPainter painter(this); style()->drawPrimitive(QStyle::PE_Widget, &option, &painter, this); } private: void buildLayout() { _icon->setFixedSize(35, 35); _text->setAlignment(Qt::AlignLeft | Qt::AlignVCenter); _text->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed); _indicator->setFixedSize(25, 25); _layout->setMargin(0); _layout->setSpacing(0); _layout->addSpacerItem(new QSpacerItem(10, 0, QSizePolicy::Fixed, QSizePolicy::Expanding)); _layout->addWidget(_icon); _layout->addSpacerItem(new QSpacerItem(10, 0, QSizePolicy::Fixed, QSizePolicy::Expanding)); _layout->addWidget(_text); _layout->addSpacerItem(new QSpacerItem(10, 0, QSizePolicy::Fixed, QSizePolicy::Expanding)); _layout->addWidget(_indicator); _layout->addSpacerItem(new QSpacerItem(20, 0, QSizePolicy::Fixed, QSizePolicy::Expanding)); setLayout(_layout); } void updateInterface() { _icon->setPixmap(QPixmap(Configuration::getIconImage(_contents.getIcon()))); _text->setText(_contents.getName()); _indicator->setPixmap(QPixmap(Configuration::getIndicatorImage(_contents.getIndicator()))); } int _id; QByteArray _userInfo; TabContents _contents; QHBoxLayout *_layout; QLabel *_text; QLabel *_icon; QLabel *_indicator; }; /** * @class Container * Tab interface wrapper for selecting items */ class Container : public QListWidget { Q_OBJECT public: Container(QWidget *parent = 0) : QListWidget(parent) { } virtual ~Container() { } /** * Add tab to the tab bar with the provided tab information * Throws std::runtime_error on error */ void addTab(int id, const TabContents &contents) throw(std::runtime_error) { for (int i = 0; i < count(); ++i) { QListWidgetItem *row = item(i); Tab *tab = static_cast<Tab *>(itemWidget(row)); if (tab->getId() == id) { throw std::runtime_error("Tab already exists"); } } // Create a new tab with the contents createTabItem(contents); } /** * Updates a tab on the tab bar with the provided tab information * Throws std::runtime_error on error */ void updateTab(int id, const TabContents &contents) throw(std::runtime_error) { for (int i = 0; i < count(); ++i) { QListWidgetItem *row = item(i); Tab *tab = static_cast<Tab *>(itemWidget(row)); if (tab->getId() == id) { tab->setContents(contents); return; } } throw std::runtime_error("Tab does not exist"); } /** * Removes a tab from the tab bar with the given tab information (by name) * Throws std::runtime_error on error */ void removeTab(int id) throw(std::runtime_error) { for (int i = 0; i < count(); ++i) { QListWidgetItem *row = item(i); Tab *tab = static_cast<Tab *>(itemWidget(row)); if (tab->getId() == id) { row = takeItem(i); delete row; return; } } throw std::runtime_error("Tab does not exist"); } private: void createTabItem(const TabContents &contents) { QListWidgetItem *item = new QListWidgetItem(this); item->setSizeHint(QSize(0, 50)); Tab *tab = new Tab(contents); tab->show(); addItem(item); setItemWidget(item, tab); } }; }
I have a couple QLabels in the custom "Tab" widget where I am trying to get the text color to be white with a specific font. My stylesheet is:
QListWidget { border: none; } TabInterface--Container { background: transparent; } TabInterface--Tab > QLabel, TabInterface--Tab { color: #FFFFFF; }
The Container stylesheet seems to work correctly where the background is transparent so the widgets are visible. Unfortunately, the tabs are not getting the stylesheet (text is not white in the Labels).
None of these options seem to work. The only thing I can think of is that its possibly the way I am creating the Tabs in the ListWidget. Is there something I am missing?
-
This post is deleted! -
Hi,
Where and how are you loading your stylesheet ? And what OS are you running ?