栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

QT5.15.2 绘制PNG 叠加videowidget

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

QT5.15.2 绘制PNG 叠加videowidget

上图(1)是根据网络天气API获取天气情况叠加videowidget上,并显示相应的天气图标和视频

创建一个widget工程新建一个类继承QLabel

//label.h

#ifndef LABEL_H
#define LABEL_H
#include
#include
#include "labsignal.h"
#include 
#include
class label : public QLabel
{    
Q_OBJECT
public:    
explicit label(QWidget *parent = nullptr);   
private:   
 QImage mBkg1;    
void paintEvent(QPaintEvent *e);   
 Labsignal Lab;
private slots:    
void get_value(QString);
};
#endif // LABEL_H
//label.cpp
#include "label.h"
#include
#include
label::label(QWidget *parent)    
: QLabel{parent}
{    
this->setAttribute(Qt::WA_TranslucentBackground);//背景透明    
connect(&Lab,SIGNAL(sig_value(QString)),this,SLOT(get_value(QString)));//接收lab signal CPP 信号 emit “signal”
}
void label::get_value(QString value){    
if (value == "signal" ){        
mBkg1.load(":/qidong.png");   }   
 this->update();
}
void label::paintEvent(QPaintEvent *e)//绘制png
{   
 mBkg1 = mBkg1.scaled(size(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);    QPainter p(this);    
p.setRenderHint(QPainter::Antialiasing,true);//抗锯齿    
p.drawImage(rect(), mBkg1);    
QBitmap bmp = QBitmap::fromImage(mBkg1.createAlphaMask());    
this->setMask(bmp);
}

//在新建一个信号类

//labsignal.h

#ifndef LABSIGNAL_H
#define LABSIGNAL_H
#include
#include
//extern QString weathervalue;//全局变量
class Labsignal : public QObject
{
Q_OBJECT
public:
explicit Labsignal(QObject *parent = nullptr);
~Labsignal();
QTimer *timer;
private slots:
void getsignal();
signals:
void sig_value(QString);
};
#endif // LABSIGNAL_H

//labsignal.cpp用到QTmer, 方法很笨,可以用

#include "labsignal.h"
#include

Labsignal::Labsignal(QObject *parent)
    : QObject{parent}
{
    timer = new QTimer(this);
    connect(timer,SIGNAL(timeout()),this,SLOT(getsignal()));//Qtimer连接get
    timer->setTimerType(Qt::PreciseTimer);//精准度
    timer->start(50);
    emit sig_value("signal");
}
void Labsignal::getsignal() //Qtimer槽
{
    emit sig_value("signal");
    timer->stop();
}


Labsignal::~Labsignal()
{
    delete timer;
}

//widget.h

#include

#include

#include

#include

#include

QT_BEGIN_NAMESPACE

namespace Ui { class Widget; }

QT_END_NAMESPACE

class Widget : public QWidget
{   
 Q_OBJECT
public:   
 Widget(QWidget *parent = nullptr);   
 ~Widget();    
   QMediaPlayer*play;   
 QMediaPlaylist*Playlist;    
QVideoWidget *videowidget;        
label *pnglabel;
private slots:    
void error(QMediaPlayer::Error  Error );//paly错误捕捉   
void resizeEvent(QResizeEvent *event);
private:   
 Ui::Widget *ui;
};
//widget.cpp
#include "widget.h"
#include "ui_widget.h"


#include

#include
#include

#include
Widget::Widget(QWidget *parent)    
: QWidget(parent)    
, ui(new Ui::Widget){    
this->setWindowFlags(Qt::FramelessWindowHint);   
QDesktopWidget *desktop = QApplication::desktop();//桌面分辨率    
int Width = desktop->width();    
int Height = desktop->height();    
pnglabel = new label(this);    
pnglabel->setFixedSize(Width,Height);    
pnglabel->raise();//png label蒙版置顶    
this->setFixedSize(Width,Height);    
this->setWindowTitle("Player");    
this->setWindowIcon(QIcon(":/titl.ico"));//标题LOGO    
ui->setupUi(this);  
this->setFixedSize(Width,Height); 
play = new QMediaPlayer(this);//新建播放器    
Playlist  = new QMediaPlaylist(this);//新建播放列表   
 play->setPlaylist(Playlist);//play播放列表    
videowidget = new QVideoWidget(this);//新建播放窗口    
videowidget->setFixedSize(Width,Height);   
 play->setVideoOutput(videowidget);    connect(play,QOverload::of(&QMediaPlayer::error),this,&Widget::error);//play 错误捕捉             
this->Playlist->addMedia(QMediaContent(QUrl::fromLocalFile("视频路径")));//添加文件到播放列表        
play->play();    


void Widget::resizeEvent(QResizeEvent *event){ //窗口拉伸绘制   
videowidget->setFixedSize(event->size().width(),event->size().height());
this->videowidget-> setCursor(Qt::BlankCursor);    
this->videowidget->setAspectRatioMode(Qt::IgnoreAspectRatio);    
this->videowidget->lower();//视频输出窗口放底层 
}


void Widget::error(QMediaPlayer::Error  Error )//paly错误捕捉{  

 if (Error == QMediaPlayer::ResourceError){       
 QMessageBox::information(NULL, NULL, "无法解析媒体资源");}    
if (Error == QMediaPlayer::FormatError){       
 QMessageBox::information(NULL, NULL, "不(完全)支持媒体资源的格式。仍然可以播放,但没有音频或视频组件。");}    
if (Error == QMediaPlayer::NetworkError){       
 QMessageBox::information(NULL, NULL, "网络错误");}    
if (Error == QMediaPlayer::AccessDeniedError){        
QMessageBox::information(NULL, NULL, "没有播放媒体资源的适当权限");}    
if (Error == QMediaPlayer::ServiceMissingError){       
 QMessageBox::information(NULL, NULL, "未找到有效的播放服务,播放无法继续");}
}

 
//main.cpp
#include 
#include 
#include
#include
int main(int argc, char *argv[])
{    
QApplication a(argc, argv);    
Widget w;    
w.show();    
   
::SetWindowPos(HWND(w.winId()), HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW);// window API 置顶    
return a.exec();
}
 

 

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/928874.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号