QT5自定义QWidget无法显示



我正在使用QT添加四个播放窗口,每个窗口都有一个右键菜单,我想要效果:使用QWidget 的效果

代码:

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);

但当我用自定义小部件替换小部件时,它不会显示:

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

但是:

使用自定义QWidget 的效果

自定义Widget添加了以下方法来解决问题

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

相关内容

  • 没有找到相关文章

最新更新