Qt widget播放器中的子widget添加水印

在一个widget上分为上下两块不同widget

Qt widget播放器中的子widget添加水印_第1张图片

 上面为控制部分,下面显示显示部分,显示部分需要添加水印,背景为图片加黑色

水印使用事件过滤器

.h

protected:
    bool eventFilter(QObject *watched, QEvent *event);
private:
    void PaintOnWidget(QWidget *w);
    GetWaterMarkParameter();

    QFont FontWaterMark;		//文字字体
	QString WaterMarkContent;	//水印文字
	int Rectangle_x;			//水印的实际坐标x
	int Rectangle_y;			//水印的实际坐标y	
	int _Rotate;				//水印旋转角度
	int Offset_x;				//两个水印的实际偏移x
	int Offset_y;				//两个水印的实际偏移y
.cpp
//构造函数中
    ui.setupUi(this);
	ui.widget_Video->installEventFilter(this);

void VideoWidget::GetWaterMarkParameter()
{
	//百分比比例坐标
	Rectangle_x = (VideoWatermarkSet::Rectangle_X*this->width()) / 100;
	Rectangle_y = (VideoWatermarkSet::Rectangle_Y*this->height()) / 100;
	//百分比比例偏移量
	Offset_x = (VideoWatermarkSet::Offset_X*this->width()) / 100;
	Offset_y = (VideoWatermarkSet::Offset_Y*this->height()) / 100;
	//水印旋转量
	_Rotate = VideoWatermarkSet::_Rotate;

	WaterMarkContent = VideoWatermarkSet::WaterMarkContent;

	//水印字体,大小
	FontWaterMark.setFamily("宋体");
	FontWaterMark.setPixelSize(20);
}

//事件过滤器筛选条件
bool VideoWidget::eventFilter(QObject *watched, QEvent *event)
{
	if (watched == ui.widget_Video)
	{
		if (event->type() == QEvent::Paint)
		{
			PaintOnWidget(ui.widget_Video);
			return true;
		}
	}
	return QWidget::eventFilter(watched,event);
}

//画图
void VideoWidget::PaintOnWidget(QWidget *w)
{
	GetWaterMarkParameter();

	QPainter painter(w);

	painter.setFont(FontWaterMark);
	painter.setPen(Qt::white);

	painter.translate(Rectangle_x, Rectangle_y);	//位置
	painter.rotate(_Rotate);						//旋转
	painter.drawText(0, 0, WaterMarkContent);



	//水印2
	QPainter painter2(w);

	painter2.setFont(FontWaterMark);
	painter2.setPen(Qt::white);

	painter2.translate(Rectangle_x + Offset_x, Rectangle_y + Offset_y);
	painter2.rotate(_Rotate);
	painter2.drawText(0, 0, WaterMarkContent);
}

默认图片效果使用qss

    background-color:black;
	background-image:url(:/Dialogs/Images/Dialogs/background.png);
	background-position:center;
	background-repeat:no-repeat;

 

你可能感兴趣的:(Qt心得)