PDA

View Full Version : Best place for setting stylesheet



mentalmushroom
28th April 2015, 07:28
Hello. I am currently styling my application and there are things I would like to clear up. The following sample shows the window with the toolbar on the top. The toolbar contains a widget which is used in order to set custom buttons layout. The issue I noticed is the blue background of the toolbar still visible around the widget although I set margin and padding to zero. Here is the code:



#include <QtGui>

class MainWindow: public QMainWindow
{
public:
MainWindow();
};

MainWindow::MainWindow()
{
QToolBar *toolbar = new QToolBar;
toolbar->setObjectName("mainToolbar");
toolbar->setMovable(false);
toolbar->setFloatable(false);
toolbar->setContentsMargins(0, 0, 0, 0);

QWidget *widg = new QWidget;
widg->setObjectName("toolbarWidget");
widg->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

QHBoxLayout *hlay = new QHBoxLayout;

QPushButton *btn1 = new QPushButton("Rosencrantz");
QPushButton *btn2 = new QPushButton("Guildenstern");

hlay->addWidget(btn1);
hlay->addWidget(btn2);

widg->setLayout(hlay);

toolbar->addWidget(widg);
addToolBar(toolbar);
}

int main(int argc, char *argv[])
{
QApplication app(argc, argv);

// Moving this line to the place after MainWindow creation fixes unwanted padding,
// but may produce really weird results on Mac OS X when styling combobox drop-down area.
app.setStyleSheet("QToolBar#mainToolbar { background: blue; border: 1px solid yellow; margin: 0px; padding 0px; } "
" QWidget#toolbarWidget { background: green; border: 1px solid red; margin: 0px; padding: 0px; } ");


MainWindow mainWindow;
mainWindow.show();

return app.exec();
}



11141

Is there something wrong in the code or the stylesheet?

I've noticed that if I apply the stylesheet when GUI is already created, the issue disappears.

11142

From the other side, applying stylesheet when GUI is created may produce weird results on Mac OS X, e.g. when styling combobox drop-down area. So I would like to know what is the recommended place for setting stylesheet - before or after creating GUI?