Qt 透明窗体

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/wqs880527/article/details/54316228
#include "qtpainter.h"


QtPainter::QtPainter(QWidget *parent)
: QWidget(parent)
{
ui.setupUi(this);




}


void QtPainter::drawTextOnWin(QPainter *painter)
{
painter->setFont(QFont(font().family(),15));
painter->drawText( (width()-300)/2,0,300,50,Qt::AlignHCenter,"Now you can see me!");
}
void QtPainter::drawImageOnWin(QPainter *painter)
{
QPixmap imageTest("E:\\pic\\right.jpg");
painter->drawPixmap( (width()-imageTest.width())/2, (height()-imageTest.height())/2,imageTest.width(), imageTest.height(), imageTest );
}




QtPainter::~QtPainter()
{


}
void QtPainter::resizeEvent(QResizeEvent *)
{
// Use a bitmap as a mask. A bitmap only has two kind of colors: white(value is 0)
// or black(other values). When we use it to set mask, we can see the window at the position
// where the color value is 0, and other place will be transparent.
QBitmap bitMap(width(),height()); // A bit map has the same size with current widget
QPainter painter(&bitMap);
painter.setPen(QColor(255,255,0)); // Any color that is not QRgb(0,0,0) is right
painter.drawRect(0,0,width(),height());


// Now begin to draw the place where we want to show it
painter.setPen(QColor(0,0,0));
drawTextOnWin(&painter);
drawImageOnWin(&painter);


painter.drawPixmap(0, 0, bitMap);


setMask(bitMap);
}
void QtPainter::paintEvent(QPaintEvent *)
{

QPainter painter(this);
painter.setPen(QColor(Qt::red));
// As the place where we want to draw has been set opaque in the resizeEvent, so what we draw here
// will be shown
drawTextOnWin(&painter);
drawImageOnWin(&painter);



}
展开阅读全文

没有更多推荐了,返回首页