Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-47488

setStyleSheet on MainWindow break gradients in ToolButtons

    XMLWordPrintable

Details

    • Bug
    • Resolution: Unresolved
    • P2: Important
    • None
    • 5.5.0
    • Widgets: Style Sheets
    • None
    • Win8.1

    Description

      If you have set an non empty styleSheet (just a comment as stylesheet is enough) on a MainWindow, gradients in menu of a ToolButton are not repainted after collapsing and reopen again (see screencasts)

      Minimal example

      #include <QApplication>
      #include <QMainWindow>
      #include <QToolButton>
      #include <QWidgetAction>
      
      class GradientWidget : public QWidget
      {
      public:
      	GradientWidget(QWidget* parent = nullptr) : QWidget(parent), m_gradient(0, 0, 1, 0)
      	{
      		setAutoFillBackground(true);
      
      		m_gradient.setCoordinateMode(QGradient::StretchToDeviceMode);
      		m_gradient.setColorAt(0.0, Qt::black);
      		m_gradient.setColorAt(1.0, Qt::white);
      		applyGradient();
      	}
      
      	void mousePressEvent(QMouseEvent *e) override
      	{
      		m_gradient.setColorAt(0.5, Qt::red);
      		applyGradient();
      	}
      
      	void applyGradient()
      	{
      		auto p = palette();
      		p.setBrush(backgroundRole(), m_gradient);
      		setPalette(p);
      	}
      
      	//reapplying gradient on showEvent fix it
      	//void showEvent(QShowEvent *e) override
      	//{
      	//	applyGradient();
      	//}
      
      protected:
      	QLinearGradient m_gradient;
      };
      
      int main(int argc, char * argv[])
      {
      	QApplication qapp(argc, argv);
      	QMainWindow mainWindow;
      
      	auto toolButton = new QToolButton();
      	toolButton->setText("Test");
      
      	QWidgetAction *widgetAction = new QWidgetAction(&mainWindow);
      	auto w = new GradientWidget();
      	w->setFixedSize(50, 20);
      
      	widgetAction->setDefaultWidget(w);
      	toolButton->addAction(widgetAction);
      
      	mainWindow.setCentralWidget(toolButton);
      
      	mainWindow.setStyleSheet("/* */"); //comment out this line helps too
      
      	mainWindow.show();
      
      	qapp.exec();
      }
      

      Attachments

        1. expected.gif
          77 kB
          Heiko Thiel
        2. is_state.gif
          20 kB
          Heiko Thiel
        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            Unassigned Unassigned
            EliteScience Heiko Thiel
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:

              Gerrit Reviews

                There are no open Gerrit changes