QT5 custom Widgth cannot be displayed
-
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:

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:

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()); } -
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:

so,What's the difference between a class that inherits from QWidget and QWidget
@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); } -
Hi and welcome to devnet,
What exactly is not working ?
-
Your Alpha values are 1, which AFAIK, means almost fully transparent.
-
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:

so,What's the difference between a class that inherits from QWidget and QWidget
-
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:

so,What's the difference between a class that inherits from QWidget and QWidget
@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); } -
@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); }