qt 子窗体透明 不受父窗体背景影响 播放视频

使用qt版本 qt5.11.0 windows10 平台

设置子窗体透明的方式很多。但是很多方式在有些情况下效果却很糟。

如果父窗体上播放视频操作,或其他有修改窗体swichbuffer的行为时,透明效果需要做点小操作。

首先子窗体要保证时时刻刻在父窗体上显示,需要继承自父窗体,再先raise()。而如果希望子窗体不受父窗体影响,需要修改窗体flag值。Qt窗体类型有三种,一种是Qt::widget,一种是Qt::dialog,一种是Qt::window ,widget 会强制使用父窗体背景,所以会选择Dilog或window flag。WindowstaysOnTopHint会让窗体一直保持在最上面。如果不继承父窗体, 则会一直在最上面。并且需要设置SubWindow flag,才能保证在任务栏上该程序只显示一个图标。

m_topInformationBoard = new TopInformationBoard(this);
m_topInformationBoard->setGeometry(QRect(0,0,geometry().size().width(),50));
m_topInformationBoard->setWindowFlags(Qt::FramelessWindowHint|Qt::Dialog);//|Qt::WindowStaysOnTopHint//|Qt::Tool|Qt::Popup
m_topInformationBoard->setAttribute(Qt::WA_TranslucentBackground, true);
//QPalette pal;​
//pal.setColor(QPalette::Background,QColor(90,90,90,90));
//m_topInformationBoard->setAutoFillBackground(true);
//m_topInformationBoard->setPalette(pal);​
m_topInformationBoard->raise();​
m_topInformationBoard->show();

再重载widget的paintevent(Qpaintevent *e)

void RightSideSetting::paintEvent(QPaintEvent *e)​
{
    QPainter painter(this);​
    painter.fillRect(this->rect(), QColor(90,90,90,90));​
}
 

然后 上面setAttribute WA_TranslucentBackground 为true,表示子窗口透明时不会考虑父窗口,否则透明时会将父窗口原始的。framebuffer中的像素作为底图显示。再重载子窗体paintevent,在paintevent中绘制透明颜色。

这样子窗体就能透明了。

猜你喜欢

转载自blog.csdn.net/qiushangren/article/details/81035544
今日推荐