Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QT5 custom Widgth cannot be displayed
Qt 6.12 LTS is out! See what's new in the release blog

QT5 custom Widgth cannot be displayed

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 1.3k Views 2 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.
  • Z Offline
    Z Offline
    zj.liu
    wrote on last edited by
    #1

    I added four play Windows using QWidgth, which worked fine with the QWidgth class, but failed to display when replaced with custom widgth,

    The desired effect:

    c3b0d0d4-1b45-47c2-8d77-6f3a0562fcad-image.png

    code:

                m_layout->setSpacing(0);
                m_layout->setContentsMargins(0,0,0,0);
                QWidget *w1= new QWidget(video_window);
                QWidget *w2= new QWidget(video_window);
                QWidget *w3= new QWidget(video_window);
                QWidget *w4= new QWidget(video_window);
                m_layout->addWidget(w1,0,0,1,1);
                m_layout->addWidget(w2,0,1,1,1);
                m_layout->addWidget(w3,1,0,1,1);
                m_layout->addWidget(w4,1,1,1,1);
                video_window->setLayout(m_layout);
    

    But it doesn't show when I replace widgth with my custom widgth:
    398a0bb8-98f2-43e3-8d18-236f81a0d646-image.png

    custom widgth:

    Video_play::Video_play(QWidget *parent) : QWidget(parent)
    {
    
        this->init();
    }
    
    
    void Video_play::init()
    {
        Act_stop=new QAction(tr("停止预览"),this);
        Act_preview=new QAction(tr("预览"),this);
    
        Act_cut1=new QAction(tr("全屏"),this);
        Act_cut4=new QAction(tr("切换4画面"),this);
        Act_cut8=new QAction(tr("切换8画面"),this);
        Act_cut16=new QAction(tr("切换16画面"),this);
    
        connect(Act_stop, SIGNAL(triggered()), this, SLOT(stop_preview()));
        connect(Act_preview, SIGNAL(triggered()), this, SLOT(action_preview()));
    
        connect(Act_cut1, SIGNAL(triggered()), this, SLOT(cut_1()));
        connect(Act_cut4, SIGNAL(triggered()), this, SLOT(cut_4()));
        connect(Act_cut8, SIGNAL(triggered()), this, SLOT(cut_8()));
        connect(Act_cut16, SIGNAL(triggered()), this, SLOT(cut_16()));
       this->setStyleSheet("QMenu {background-color:rgba(255,255,255,1);border:1px solid rgba(82,130,164,1);}QMenu::item:selected {background:rgba(82,130,164,1);border:1px solid rgba(82,130,164,1);}");
    }
    void Video_play::contextMenuEvent(QContextMenuEvent *event)
    {
    
        QCursor cur=this->cursor();
        QMenu *menu=new QMenu(this);
    
        menu->addAction(Act_preview);
        menu->addAction(Act_stop);
        menu->addAction(Act_cut1);
        menu->addAction(Act_cut4);
        menu->addAction(Act_cut8);
        menu->addAction(Act_cut16);
        menu->exec(cur.pos());
    }
    
    1 Reply Last reply
    -1
    • Z zj.liu

      @SGaist

      I removing the subclass's style sheet,but hasn't changed,

      In addition, the parent class has a set style sheet:

      video_window->setStyleSheet("background:transparent;border:2px solid red;");
      

      then,I replaced one of the custom QWidgth with QWidgth:

                  m_layout->setSpacing(0);
                  m_layout->setContentsMargins(0,0,0,0);
                  Video_play *w1= new Video_play(video_window);
                  QWidget *w2= new QWidget(video_window);
                  Video_play *w3= new Video_play(video_window);
                  Video_play *w4= new Video_play(video_window);
                  m_layout->addWidget(w1,0,0,1,1);
                  m_layout->addWidget(w2,0,1,1,1);
                  m_layout->addWidget(w3,1,0,1,1);
                  m_layout->addWidget(w4,1,1,1,1);
                  video_window->setLayout(m_layout);
      

      run:
      d39e6989-f92d-4ade-86f2-8403bad94317-image.png

      so,What's the difference between a class that inherits from QWidget and QWidget

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

      @zj-liu said in QT5 custom Widgth cannot be displayed:

      What's the difference between a class that inherits from QWidget and QWidget

      In terms of using stylesheet, you need to have a paintEvent for the custom widget
      that respect the stylesheet.
      This allows a custom QWidget to use stylesheet.

       void CustomWidget::paintEvent(QPaintEvent *)
       {
           QStyleOption opt;
           opt.init(this);
           QPainter p(this);
           style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
       }
      
      Z 1 Reply Last reply
      3
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #2

        Hi and welcome to devnet,

        What exactly is not working ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        Z 1 Reply Last reply
        1
        • SGaistS SGaist

          Hi and welcome to devnet,

          What exactly is not working ?

          Z Offline
          Z Offline
          zj.liu
          wrote on last edited by
          #3

          @SGaist I want four Windows like those in Figure 1 to appear, but they don't

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #4

            Your Alpha values are 1, which AFAIK, means almost fully transparent.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            Z 1 Reply Last reply
            1
            • SGaistS SGaist

              Your Alpha values are 1, which AFAIK, means almost fully transparent.

              Z Offline
              Z Offline
              zj.liu
              wrote on last edited by
              #5

              @SGaist

              I removing the subclass's style sheet,but hasn't changed,

              In addition, the parent class has a set style sheet:

              video_window->setStyleSheet("background:transparent;border:2px solid red;");
              

              then,I replaced one of the custom QWidgth with QWidgth:

                          m_layout->setSpacing(0);
                          m_layout->setContentsMargins(0,0,0,0);
                          Video_play *w1= new Video_play(video_window);
                          QWidget *w2= new QWidget(video_window);
                          Video_play *w3= new Video_play(video_window);
                          Video_play *w4= new Video_play(video_window);
                          m_layout->addWidget(w1,0,0,1,1);
                          m_layout->addWidget(w2,0,1,1,1);
                          m_layout->addWidget(w3,1,0,1,1);
                          m_layout->addWidget(w4,1,1,1,1);
                          video_window->setLayout(m_layout);
              

              run:
              d39e6989-f92d-4ade-86f2-8403bad94317-image.png

              so,What's the difference between a class that inherits from QWidget and QWidget

              mrjjM 1 Reply Last reply
              0
              • Z zj.liu

                @SGaist

                I removing the subclass's style sheet,but hasn't changed,

                In addition, the parent class has a set style sheet:

                video_window->setStyleSheet("background:transparent;border:2px solid red;");
                

                then,I replaced one of the custom QWidgth with QWidgth:

                            m_layout->setSpacing(0);
                            m_layout->setContentsMargins(0,0,0,0);
                            Video_play *w1= new Video_play(video_window);
                            QWidget *w2= new QWidget(video_window);
                            Video_play *w3= new Video_play(video_window);
                            Video_play *w4= new Video_play(video_window);
                            m_layout->addWidget(w1,0,0,1,1);
                            m_layout->addWidget(w2,0,1,1,1);
                            m_layout->addWidget(w3,1,0,1,1);
                            m_layout->addWidget(w4,1,1,1,1);
                            video_window->setLayout(m_layout);
                

                run:
                d39e6989-f92d-4ade-86f2-8403bad94317-image.png

                so,What's the difference between a class that inherits from QWidget and QWidget

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

                @zj-liu said in QT5 custom Widgth cannot be displayed:

                What's the difference between a class that inherits from QWidget and QWidget

                In terms of using stylesheet, you need to have a paintEvent for the custom widget
                that respect the stylesheet.
                This allows a custom QWidget to use stylesheet.

                 void CustomWidget::paintEvent(QPaintEvent *)
                 {
                     QStyleOption opt;
                     opt.init(this);
                     QPainter p(this);
                     style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
                 }
                
                Z 1 Reply Last reply
                3
                • mrjjM mrjj

                  @zj-liu said in QT5 custom Widgth cannot be displayed:

                  What's the difference between a class that inherits from QWidget and QWidget

                  In terms of using stylesheet, you need to have a paintEvent for the custom widget
                  that respect the stylesheet.
                  This allows a custom QWidget to use stylesheet.

                   void CustomWidget::paintEvent(QPaintEvent *)
                   {
                       QStyleOption opt;
                       opt.init(this);
                       QPainter p(this);
                       style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
                   }
                  
                  Z Offline
                  Z Offline
                  zj.liu
                  wrote on last edited by
                  #7

                  @mrjj
                  Thanks,After I added paintEvent, I successfully solved the problem

                  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
                  • Unsolved