Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. The stylesheet do not work in custom widget
QtWS25 Last Chance

The stylesheet do not work in custom widget

Scheduled Pinned Locked Moved General and Desktop
14 Posts 3 Posters 20.6k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    Franzk
    wrote on last edited by
    #2

    Your custom widget should be made "style aware":http://doc.qt.nokia.com/latest/style-reference.html. Especially if you draw the widget yourself.

    "Horse sense is the thing a horse has which keeps it from betting on people." -- W.C. Fields

    http://www.catb.org/~esr/faqs/smart-questions.html

    1 Reply Last reply
    0
    • L Offline
      L Offline
      linlin_0
      wrote on last edited by
      #3

      [quote author="Franzk" date="1290066386"]Your custom widget should be made "style aware":http://doc.qt.nokia.com/latest/style-reference.html. Especially if you draw the widget yourself.[/quote]
      ok, thanks.
      but my custom widget just like this:
      //.h
      class mywidget : public QWidget
      {
      Q_OBJECT
      public:
      mywidget(QWidget *parent = 0);

      };

      //.cpp

      mywidget::mywidget(QWidget *parent):QWidget(parent)
      {

      }

      and the initialization is :
      my = new mywidget(this);
      my->setStyleSheet(QString(stylesheet));
      my->setGeometry(QRect(10, 10, 100, 100));

      and the stylesheet is "background-color: rgb(0, 170, 255);".
      the stylesheet works well in QLabel but bad in mywidget.

      and in a simple application mywidget works well with the stylesheet, but bad in my project.

      1 Reply Last reply
      0
      • S Offline
        S Offline
        Sorin
        wrote on last edited by
        #4

        try this:

        @myWidget->setObjectName( "myWidget" );
        myWidget->setStyleSheet( "QWidget#myWidget{ background-color: rgb(0, 170, 255); }" );@

        There are only 10 types of people in the world: Those who understand binary, and those who don't

        1 Reply Last reply
        0
        • L Offline
          L Offline
          linlin_0
          wrote on last edited by
          #5

          [quote author="Sorin" date="1290071156"]try this: @myWidget->setObjectName( "myWidget" ); myWidget->setStyleSheet( "QWidget#myWidget{ background-color: rgb(0, 170, 255); }" );@[/quote]

          it also works bad.

          1 Reply Last reply
          0
          • S Offline
            S Offline
            Sorin
            wrote on last edited by
            #6

            are you sure that your widget is visible?, maybe is overlapped by other widget or his parent is not visible or the parent size is too small. Can you post your code here?

            There are only 10 types of people in the world: Those who understand binary, and those who don't

            1 Reply Last reply
            0
            • L Offline
              L Offline
              linlin_0
              wrote on last edited by
              #7

              @
              //.h
              #ifndef MAINWINDOW_H
              #define MAINWINDOW_H

              #include <QMainWindow>

              namespace Ui {
              class MainWindow;
              }

              class mywidget;

              class MainWindow : public QMainWindow
              {
              Q_OBJECT

              public:
              explicit MainWindow(QWidget *parent = 0);
              ~MainWindow();

              void resizeEvent(QResizeEvent *);
              

              private:
              Ui::MainWindow *ui;

              mywidget* w;
              

              };

              class mywidget : public QWidget
              {
              Q_OBJECT
              public:
              mywidget(QWidget *parent = 0);

              protected:
              void paintEvent(QPaintEvent *);
              };

              #endif // MAINWINDOW_H

              //.cpp

              #include "mainwindow.h"
              #include "ui_mainwindow.h"

              #include <QPainter>

              const char wstyleSheet[] = "QWidget#myWidget{ background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0, stop:0 rgba(200, 200, 200, 255), stop:1 rgba(150,140, 160, 255)); }";

              MainWindow::MainWindow(QWidget *parent) :
              QMainWindow(parent),
              ui(new Ui::MainWindow)
              {
              ui->setupUi(this);

              w = new mywidget(this);
              w->setObjectName("mywidget");
              w->setStyleSheet(QString(wstyleSheet));
              w->setGeometry(rect());
              

              }

              MainWindow::~MainWindow()
              {
              delete ui;
              }

              void MainWindow::resizeEvent(QResizeEvent * e)
              {
              QMainWindow::resizeEvent(e);
              w->setGeometry(rect());
              }

              mywidget::mywidget(QWidget *parent):QWidget(parent)
              {

              }

              void mywidget::paintEvent(QPaintEvent * e)
              {
              QWidget::paintEvent(e);

              //test
              QPainter p(this);
              QRect t(rect());
              t.adjust(50, 50, -50, -50);
              p.fillRect(t, QColor(255,255, 0));
              

              }

              //main.cpp
              #include <QtGui/QApplication>
              #include "mainwindow.h"

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

              // mywidget w;
              // w.setStyleSheet("background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0, stop:0 rgba(200, 200, 200, 255), stop:1 rgba(150,140, 160, 255));");
              MainWindow w;
              #if defined(Q_WS_S60)
              w.showMaximized();
              #else
              w.show();
              #endif

              return a.exec&#40;&#41;;
              

              }
              @

              1 Reply Last reply
              0
              • L Offline
                L Offline
                linlin_0
                wrote on last edited by
                #8

                [quote author="Sorin" date="1290073359"]are you sure that your widget is visible?, maybe is overlapped by other widget or his parent is not visible or the parent size is too small. Can you post your code here?[/quote]

                that is the code.

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  Sorin
                  wrote on last edited by
                  #9

                  replace @w = new mywidget(this);@ with @w = new mywidget; w->show(); @ and you will see the difference, it's not a stylesheet problem

                  There are only 10 types of people in the world: Those who understand binary, and those who don't

                  1 Reply Last reply
                  0
                  • L Offline
                    L Offline
                    linlin_0
                    wrote on last edited by
                    #10

                    [quote author="Sorin" date="1290075672"]replace @w = new mywidget(this);@ with @w = new mywidget; w->show(); @ and you will see the difference, it's not a stylesheet problem [/quote]
                    well.
                    but why does it work bad when mywidget is child of one parent?

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      Sorin
                      wrote on last edited by
                      #11

                      I did some modifications to your code, the basic idea is: take a look at ui_mainwindow.h( I'm guessing that you used MainWindow class generated by QT Creator, like an advice, try to create your own classes from scratch, it's easy to "control" your code this way ), there you will see that your mainWindow has a central widget on it, so your widget should be a child of the central widget, also I added a layout to the centralWidget control, but the strange part that I removed Q_OBJECT macro from your class and then the stylesheet worked, I didn't understand way because I used the same macro when I implemented some custom widget in the past for a qmainwindow, the only difference is that I implemented from scratch a class derived from qmainwindow

                      @//h
                      #ifndef MAINWINDOW_H
                      #define MAINWINDOW_H

                      #include <QMainWindow>
                      #include<QHBoxLayout>

                      namespace Ui {
                      class MainWindow;
                      }

                      class mywidget;

                      class MainWindow : public QMainWindow
                      {
                      Q_OBJECT

                      public:
                      explicit MainWindow(QWidget parent = 0);
                      ~MainWindow();
                      QHBoxLayout
                      ddd;

                      private:
                      Ui::MainWindow ui;
                      mywidget
                      xxx;
                      };

                      class mywidget : public QWidget
                      {

                      public:
                      mywidget(QWidget *parent = 0);

                      };

                      #endif // MAINWINDOW_H

                      //cpp
                      #include "mainwindow.h"
                      #include "ui_mainwindow.h"

                      #include <QPainter>
                      #include<QPushButton>
                      #include<QLabel>

                      const char wstyleSheet[] = "QWidget#myWidget{ background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0, stop:0 rgba(200, 200, 200, 255), stop:1 rgba(150,140, 160, 255)); }";

                      MainWindow::MainWindow(QWidget *parent) :
                      QMainWindow(parent),
                      ui(new Ui::MainWindow)
                      {
                      ui->setupUi(this);

                      ddd = new QHBoxLayout;
                      centralWidget()->setLayout( ddd );
                      
                      QLabel* sss = new QLabel( centralWidget() );
                      sss->setMinimumSize( QSize( 10,50) );
                      sss->setStyleSheet( "QLabel{  background-color : red;  }");
                      xxx = new mywidget( centralWidget() );
                      
                      ddd->addWidget(  xxx );
                      ddd->addWidget( sss );
                      

                      }

                      MainWindow::~MainWindow()
                      {
                      delete ui;
                      }

                      mywidget::mywidget(QWidget *parent):QWidget(parent)
                      {
                      setObjectName("myWidget");
                      setStyleSheet("QWidget#myWidget{ background-color : blue }");
                      setMinimumSize( QSize( 50,100) );
                      setMaximumSize( QSize( 50,100) );
                      setVisible( true );
                      setWindowTitle( " test ");
                      }

                      /*void mywidget::paintEvent(QPaintEvent * e)
                      {
                      QWidget::paintEvent(e);

                      //test
                      QPainter p(this);
                      QRect t(rect());
                      t.adjust(50, 50, -50, -50);
                      p.fillRect(t, QColor(255,255, 0));
                      

                      }*/

                      //main
                      #include <QtGui/QApplication>
                      #include "mainwindow.h"

                      int main(int argc, char *argv[])
                      {
                      /*QApplication a(argc, argv);
                      MainWindow w;
                      w.show();

                      return a.exec&#40;&#41;;*/
                      QApplication a(argc, argv&#41;;
                      
                      //    mywidget w;
                      //    w.setStyleSheet("background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0, stop:0 rgba(200, 200, 200, 255&#41;, stop:1 rgba(150,140, 160, 255));");
                          MainWindow* w = new MainWindow ;
                      
                      
                      
                      
                          w->show();
                      
                          return a.exec(&#41;;
                      

                      }

                      @

                      There are only 10 types of people in the world: Those who understand binary, and those who don't

                      1 Reply Last reply
                      0
                      • L Offline
                        L Offline
                        linlin_0
                        wrote on last edited by
                        #12

                        [quote author="Sorin" date="1290092560"]I did some modifications to your code @[/quote]
                        yes, it works.
                        but the slot and signal are not supported without the "Q_OBJECT" marco。

                        do you have any example that using custom widget with "Q_OBJECT" ?

                        thanks a lot.

                        1 Reply Last reply
                        0
                        • S Offline
                          S Offline
                          Sorin
                          wrote on last edited by
                          #13

                          I took a look at some code that I wrote some weeks ago, I didn't subclass my widget from QWidget, try to subclass your widget for something else relevant to your output( something that inherits QWidget like QToolButton for example ) and then it will work with Q_OBJECT, I'm going to throw a question regarding this problem on the forum, for sure we forgot to call or override a QWidget method

                          There are only 10 types of people in the world: Those who understand binary, and those who don't

                          1 Reply Last reply
                          0
                          • S Offline
                            S Offline
                            Sorin
                            wrote on last edited by
                            #14

                            Pfffff what a mistake we did :))))))) , QWidget supports only the background and background-origin attributes for the stylesheet, replace "backgroud-color" with "background" this is the correct answer.......

                            There are only 10 types of people in the world: Those who understand binary, and those who don't

                            1 Reply Last reply
                            0

                            • Login

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • Users
                            • Groups
                            • Search
                            • Get Qt Extensions
                            • Unsolved