上图(1)是根据网络天气API获取天气情况叠加videowidget上,并显示相应的天气图标和视频
创建一个widget工程新建一个类继承QLabel
//label.h
#ifndef LABEL_H
#define LABEL_H
#include<QPaintEvent>
#include<QImage>
#include "labsignal.h"
#include <QWidget>
#include<QLabel>
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<QBitmap>
#include<QPainter>
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 <QObject>
#include<QTimer>
//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<QFileInfo>
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<QCoreApplication>
#include<QDir>
#include<QCloseEvent>
#include<QLabel>
#include<label.h>
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<QMessageBox>
#include<QFile>
#include<QPixmap>
#include<QDesktopWidget>
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->raise(Width,Height);
this->setWindowTitle("Player");
this->setWindowIcon(QIcon(":/titl.ico"));//标题LOGO
ui->setupUi(this);
play = new QMediaPlayer(this);//新建播放器
Playlist = new QMediaPlaylist(this);//新建播放列表
play->setPlaylist(Playlist);//play播放列表
videowidget = new QVideoWidget(this);//新建播放窗口
play->setVideoOutput(videowidget); connect(play,QOverload<QMediaPlayer::Error>::of(&QMediaPlayer::error),this,&Widget::error);//play 错误捕捉
this->Playlist->addMedia(QMediaContent(QUrl::fromLocalFile("视频路径")));//添加文件到播放列表
play->play();
void Widget::resizeEvent(QResizeEvent *event){ //窗口拉伸绘制
videowidget->raise(event->size().width(),event->size().height());
pnglabel->raise(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 <QApplication>
#include <QGuiApplication>
#include<widget.h>
#include<windows.h>
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();
}
QT5.15png叠加到videowidget-C++文档类资源-CSDN下载