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:

Qt Code:
  1. #include <QtGui>
  2.  
  3. class MainWindow: public QMainWindow
  4. {
  5. public:
  6. MainWindow();
  7. };
  8.  
  9. MainWindow::MainWindow()
  10. {
  11. QToolBar *toolbar = new QToolBar;
  12. toolbar->setObjectName("mainToolbar");
  13. toolbar->setMovable(false);
  14. toolbar->setFloatable(false);
  15. toolbar->setContentsMargins(0, 0, 0, 0);
  16.  
  17. QWidget *widg = new QWidget;
  18. widg->setObjectName("toolbarWidget");
  19. widg->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
  20.  
  21. QHBoxLayout *hlay = new QHBoxLayout;
  22.  
  23. QPushButton *btn1 = new QPushButton("Rosencrantz");
  24. QPushButton *btn2 = new QPushButton("Guildenstern");
  25.  
  26. hlay->addWidget(btn1);
  27. hlay->addWidget(btn2);
  28.  
  29. widg->setLayout(hlay);
  30.  
  31. toolbar->addWidget(widg);
  32. addToolBar(toolbar);
  33. }
  34.  
  35. int main(int argc, char *argv[])
  36. {
  37. QApplication app(argc, argv);
  38.  
  39. // Moving this line to the place after MainWindow creation fixes unwanted padding,
  40. // but may produce really weird results on Mac OS X when styling combobox drop-down area.
  41. app.setStyleSheet("QToolBar#mainToolbar { background: blue; border: 1px solid yellow; margin: 0px; padding 0px; } "
  42. " QWidget#toolbarWidget { background: green; border: 1px solid red; margin: 0px; padding: 0px; } ");
  43.  
  44.  
  45. MainWindow mainWindow;
  46. mainWindow.show();
  47.  
  48. return app.exec();
  49. }
To copy to clipboard, switch view to plain text mode 

teststylesheet.png

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.

teststylesheet-correct.png

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?