<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[QT5 custom Widgth cannot be displayed]]></title><description><![CDATA[<p dir="auto">I added four play Windows using QWidgth, which worked fine with the QWidgth class, but failed to display when replaced with custom widgth,</p>
<p dir="auto">The desired effect:</p>
<p dir="auto"><img src="https://ddgobkiprc33d.cloudfront.net/fe68eae1-2140-42a0-a87c-bd2f9637747d.png" alt="c3b0d0d4-1b45-47c2-8d77-6f3a0562fcad-image.png" class=" img-fluid img-markdown" /></p>
<p dir="auto">code:</p>
<pre><code>            m_layout-&gt;setSpacing(0);
            m_layout-&gt;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-&gt;addWidget(w1,0,0,1,1);
            m_layout-&gt;addWidget(w2,0,1,1,1);
            m_layout-&gt;addWidget(w3,1,0,1,1);
            m_layout-&gt;addWidget(w4,1,1,1,1);
            video_window-&gt;setLayout(m_layout);
</code></pre>
<p dir="auto">But it doesn't show when I replace widgth with my custom widgth:<br />
<img src="https://ddgobkiprc33d.cloudfront.net/91483c38-cb05-4c76-95bf-6cff49243954.png" alt="398a0bb8-98f2-43e3-8d18-236f81a0d646-image.png" class=" img-fluid img-markdown" /></p>
<p dir="auto">custom widgth:</p>
<pre><code>Video_play::Video_play(QWidget *parent) : QWidget(parent)
{

    this-&gt;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-&gt;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-&gt;cursor();
    QMenu *menu=new QMenu(this);

    menu-&gt;addAction(Act_preview);
    menu-&gt;addAction(Act_stop);
    menu-&gt;addAction(Act_cut1);
    menu-&gt;addAction(Act_cut4);
    menu-&gt;addAction(Act_cut8);
    menu-&gt;addAction(Act_cut16);
    menu-&gt;exec(cur.pos());
}
</code></pre>
]]></description><link>https://forum.qt.io/topic/118105/qt5-custom-widgth-cannot-be-displayed</link><generator>RSS for Node</generator><lastBuildDate>Fri, 06 Nov 2026 08:46:51 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/118105.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 17 Aug 2020 03:25:28 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to QT5 custom Widgth cannot be displayed on Wed, 19 Aug 2020 05:49:02 GMT]]></title><description><![CDATA[<p dir="auto">@zj-liu said in <a href="/post/613063">QT5 custom Widgth cannot be displayed</a>:</p>
<blockquote>
<p dir="auto">What's the difference between a class that inherits from QWidget and QWidget</p>
</blockquote>
<p dir="auto">In terms of using stylesheet, you need to have a paintEvent for the  custom widget<br />
that respect the stylesheet.<br />
This allows a custom QWidget to use stylesheet.</p>
<pre><code> void CustomWidget::paintEvent(QPaintEvent *)
 {
     QStyleOption opt;
     opt.init(this);
     QPainter p(this);
     style()-&gt;drawPrimitive(QStyle::PE_Widget, &amp;opt, &amp;p, this);
 }
</code></pre>
]]></description><link>https://forum.qt.io/post/613072</link><guid isPermaLink="true">https://forum.qt.io/post/613072</guid><dc:creator><![CDATA[mrjj]]></dc:creator><pubDate>Wed, 19 Aug 2020 05:49:02 GMT</pubDate></item><item><title><![CDATA[Reply to QT5 custom Widgth cannot be displayed on Wed, 19 Aug 2020 06:56:27 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/mrjj">@<bdi>mrjj</bdi></a><br />
Thanks，After I added paintEvent, I successfully solved the problem</p>
]]></description><link>https://forum.qt.io/post/613077</link><guid isPermaLink="true">https://forum.qt.io/post/613077</guid><dc:creator><![CDATA[zj.liu]]></dc:creator><pubDate>Wed, 19 Aug 2020 06:56:27 GMT</pubDate></item><item><title><![CDATA[Reply to QT5 custom Widgth cannot be displayed on Wed, 19 Aug 2020 05:49:02 GMT]]></title><description><![CDATA[<p dir="auto">@zj-liu said in <a href="/post/613063">QT5 custom Widgth cannot be displayed</a>:</p>
<blockquote>
<p dir="auto">What's the difference between a class that inherits from QWidget and QWidget</p>
</blockquote>
<p dir="auto">In terms of using stylesheet, you need to have a paintEvent for the  custom widget<br />
that respect the stylesheet.<br />
This allows a custom QWidget to use stylesheet.</p>
<pre><code> void CustomWidget::paintEvent(QPaintEvent *)
 {
     QStyleOption opt;
     opt.init(this);
     QPainter p(this);
     style()-&gt;drawPrimitive(QStyle::PE_Widget, &amp;opt, &amp;p, this);
 }
</code></pre>
]]></description><link>https://forum.qt.io/post/613072</link><guid isPermaLink="true">https://forum.qt.io/post/613072</guid><dc:creator><![CDATA[mrjj]]></dc:creator><pubDate>Wed, 19 Aug 2020 05:49:02 GMT</pubDate></item><item><title><![CDATA[Reply to QT5 custom Widgth cannot be displayed on Wed, 19 Aug 2020 01:32:00 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/sgaist">@<bdi>SGaist</bdi></a></p>
<p dir="auto">I removing the subclass's style sheet,but hasn't changed,</p>
<p dir="auto">In addition, the parent class has a set style sheet:</p>
<pre><code>video_window-&gt;setStyleSheet("background:transparent;border:2px solid red;");
</code></pre>
<p dir="auto">then,I replaced one of the custom QWidgth with QWidgth:</p>
<pre><code>            m_layout-&gt;setSpacing(0);
            m_layout-&gt;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-&gt;addWidget(w1,0,0,1,1);
            m_layout-&gt;addWidget(w2,0,1,1,1);
            m_layout-&gt;addWidget(w3,1,0,1,1);
            m_layout-&gt;addWidget(w4,1,1,1,1);
            video_window-&gt;setLayout(m_layout);
</code></pre>
<p dir="auto">run:<br />
<img src="https://ddgobkiprc33d.cloudfront.net/e90c91fc-803c-42b6-a4a9-bc82a4e59693.png" alt="d39e6989-f92d-4ade-86f2-8403bad94317-image.png" class=" img-fluid img-markdown" /></p>
<p dir="auto">so,What's the difference between a class that inherits from QWidget and QWidget</p>
]]></description><link>https://forum.qt.io/post/613063</link><guid isPermaLink="true">https://forum.qt.io/post/613063</guid><dc:creator><![CDATA[zj.liu]]></dc:creator><pubDate>Wed, 19 Aug 2020 01:32:00 GMT</pubDate></item><item><title><![CDATA[Reply to QT5 custom Widgth cannot be displayed on Tue, 18 Aug 2020 19:29:38 GMT]]></title><description><![CDATA[<p dir="auto">Your Alpha values are 1, which AFAIK, means almost fully transparent.</p>
]]></description><link>https://forum.qt.io/post/613021</link><guid isPermaLink="true">https://forum.qt.io/post/613021</guid><dc:creator><![CDATA[SGaist]]></dc:creator><pubDate>Tue, 18 Aug 2020 19:29:38 GMT</pubDate></item><item><title><![CDATA[Reply to QT5 custom Widgth cannot be displayed on Tue, 18 Aug 2020 00:36:03 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/sgaist">@<bdi>SGaist</bdi></a> I want four Windows like those in Figure 1 to appear, but they don't</p>
]]></description><link>https://forum.qt.io/post/612834</link><guid isPermaLink="true">https://forum.qt.io/post/612834</guid><dc:creator><![CDATA[zj.liu]]></dc:creator><pubDate>Tue, 18 Aug 2020 00:36:03 GMT</pubDate></item><item><title><![CDATA[Reply to QT5 custom Widgth cannot be displayed on Mon, 17 Aug 2020 18:59:00 GMT]]></title><description><![CDATA[<p dir="auto">Hi and welcome to devnet,</p>
<p dir="auto">What exactly is not working ?</p>
]]></description><link>https://forum.qt.io/post/612788</link><guid isPermaLink="true">https://forum.qt.io/post/612788</guid><dc:creator><![CDATA[SGaist]]></dc:creator><pubDate>Mon, 17 Aug 2020 18:59:00 GMT</pubDate></item></channel></rss>