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. Custom Widget is not visible in Widget window
Forum Updated to NodeBB v4.3 + New Features

Custom Widget is not visible in Widget window

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 1.0k Views 1 Watching
  • 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.
  • D Offline
    D Offline
    devillIsHeree
    wrote on last edited by
    #1

    I have created my own custom widget class, the code is below:

    class myWidget : public QWidget
    {
        Q_OBJECT
    public:
        explicit myWidget(QWidget*parent=nullptr): QWidget(parent){}
        void enterEvent(QEvent *event) override
        {
            Q_UNUSED(event);
            qDebug()<<"Entered";
        }
    };
    

    In widget.cpp I have created it's instance, like below

    myWidget *w;
    
    Widget::Widget(QWidget *parent)
        : QWidget(parent)
        , ui(new Ui::Widget)
    {
        ui->setupUi(this);
        w=new myWidget(this);
        w->setStyleSheet("background-color: rgb(138, 226, 52);");
        w->setGeometry(10,10,100,100);
        w->show();
    }
    

    As you can see my object is child of the thiswidgetwindow but whenever I run the program it won't be visible to me in the window but when i go to top bottom i can see the console output of mine Entered, So it is present in my Widget window but I am not able to see it even I have set the background color as green, If I just use the Qt GUI to add one Widget into the form and set the background color and run the code then that Widget which I added from the GUI is visible but My Widget which i created by my own custom widget is not visible.

    mrjjM 1 Reply Last reply
    0
    • D devillIsHeree

      I have created my own custom widget class, the code is below:

      class myWidget : public QWidget
      {
          Q_OBJECT
      public:
          explicit myWidget(QWidget*parent=nullptr): QWidget(parent){}
          void enterEvent(QEvent *event) override
          {
              Q_UNUSED(event);
              qDebug()<<"Entered";
          }
      };
      

      In widget.cpp I have created it's instance, like below

      myWidget *w;
      
      Widget::Widget(QWidget *parent)
          : QWidget(parent)
          , ui(new Ui::Widget)
      {
          ui->setupUi(this);
          w=new myWidget(this);
          w->setStyleSheet("background-color: rgb(138, 226, 52);");
          w->setGeometry(10,10,100,100);
          w->show();
      }
      

      As you can see my object is child of the thiswidgetwindow but whenever I run the program it won't be visible to me in the window but when i go to top bottom i can see the console output of mine Entered, So it is present in my Widget window but I am not able to see it even I have set the background color as green, If I just use the Qt GUI to add one Widget into the form and set the background color and run the code then that Widget which I added from the GUI is visible but My Widget which i created by my own custom widget is not visible.

      mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @devillIsHeree
      Hi
      How do your paintEvent look like for myWidget?
      It has to be like

       void CustomWidget::paintEvent(QPaintEvent *)
       {
           QStyleOption opt;
           opt.init(this);
           QPainter p(this);
           style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
       }
      

      to use a stylesheet.

      D 1 Reply Last reply
      1
      • mrjjM mrjj

        @devillIsHeree
        Hi
        How do your paintEvent look like for myWidget?
        It has to be like

         void CustomWidget::paintEvent(QPaintEvent *)
         {
             QStyleOption opt;
             opt.init(this);
             QPainter p(this);
             style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
         }
        

        to use a stylesheet.

        D Offline
        D Offline
        devillIsHeree
        wrote on last edited by
        #3

        @mrjj Thanks it works, Can you please explain the logic behind it? because if we do the same without using my own class it works but when I do the same by inheriting I have to modify the paintEvent as you told.

        mrjjM B 2 Replies Last reply
        0
        • D devillIsHeree

          @mrjj Thanks it works, Can you please explain the logic behind it? because if we do the same without using my own class it works but when I do the same by inheriting I have to modify the paintEvent as you told.

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @devillIsHeree
          Hi
          For some reason, the QWidget::paintEvent is empty so once subclassed
          its not doing anything. I never really found out how come plain QWidget can
          work but I guess that QStyle somehow makes it possible as long its a plain QWidget.
          So when we subclass it, we have to draw using QStyle to make a stylesheet take effect.

          1 Reply Last reply
          0
          • D devillIsHeree

            @mrjj Thanks it works, Can you please explain the logic behind it? because if we do the same without using my own class it works but when I do the same by inheriting I have to modify the paintEvent as you told.

            B Offline
            B Offline
            Bonnie
            wrote on last edited by
            #5

            @devillIsHeree
            I don't actually know the logic, it is just in the documentation

            If you subclass from QWidget, you need to provide a paintEvent for your custom QWidget as below:
            ...

            And if you feel it a little annoying to write a paintEvent, there's a trick that I was told by @JonB :

            setAttribute(Qt::WA_StyledBackground);
            

            add this in your custom widget's constructor.

            1 Reply Last reply
            1

            • Login

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