在实现qt播放时,调用的mplayer,由于采用的是自定义绘图,用的是setAttribute(Qt::WA_TranslucentBackground);结果不能正常在上面显示播放画面,在默认皮肤下是没有问题的,决定用九宫格图片拉伸方式(效果如图)
附件图片:
文件:
tst2.rar
大小:
10KB
下载:
typedef struct RECTANGLE
{
int left;
int top;
int right;
int bottom;
RECTANGLE()
{
left = 0;
top = 0;
right = 0;
bottom = 0;
}
RECTANGLE(int _left, int _top, int _right, int _bottom)
{
left = _left;
top = _top;
right = _right;
bottom = _bottom;
}
} RectangleEx;
Form::Form()
{
RectangleEx margin(40, 30, 20, 20);
resize(350, 500);
QPalette p = palette();
QPixmap img("back.png");
QBitmap mask("backMask.png");
QPixmap imgAA = scaleEx(img, margin);;
QPixmap maskAA = scaleEx(mask, margin);;
QRect r = imgAA.rect();
QRect r2 = maskAA.rect();
p.setBrush(QPalette::Window, QBrush(imgAA));
setPalette(p);
setMask(maskAA);
setWindowFlags(Qt::FramelessWindowHint);
}
QPixmap Form::scaleEx(QPixmap &map,RectangleEx rect)
{
QPixmap rMap;
QRect fRect = frameGeometry();
QRect pRect = map.rect();
// for four corner rects of src
// 1. topLeft rect
QRect srcRect1 = QRect(0, 0, rect.left, rect.top);
// 2. topRight rect
QRect srcRect2 = QRect(pRect.width()-rect.right, 0, rect.right, rect.top);
// 3. bottomLeft rect
QRect srcRect3 = QRect(0, pRect.height()-rect.bottom, rect.left, rect.bottom);
// 4. bottomRight rect
QRect srcRect4 = QRect(pRect.width()-rect.right, pRect.height()-rect.bottom,
rect.right, rect.bottom);
QRect pSrcRectMid = QRect(rect.left, rect.top,
pRect.width()-(rect.left+rect.right), pRect.height()-(rect.top+rect.bottom));
QRect pSrcRectMidTop = QRect(rect.left, 0, pRect.width()-(rect.left+rect.right), rect.top);
QRect pSrcRectMidBottom = QRect(rect.left,pRect.height()-rect.bottom,
pRect.width()-(rect.left+rect.right), rect.bottom);
QRect pSrcRectMidLeft = QRect(0, rect.top, rect.left,
pRect.height()-(rect.bottom+rect.top));
QRect pSrcRectMidRight = QRect(pRect.width()-rect.right,rect.top,
rect.right, pRect.height()-(rect.top+rect.bottom));
// for four corner rects of desc
// 1. topLeft rect
QRect descRect1 = QRect(0, 0, rect.left, rect.top);
// 2. topRight rect
QRect descRect2 = QRect(fRect.width()-rect.right, 0, rect.right, rect.top);
// 3. bottomLeft rect
QRect descRect3 = QRect(0, fRect.height()-rect.bottom, rect.left, rect.bottom);
// 4. bottomRight rect
QRect descRect4 = QRect(fRect.width()-rect.right, fRect.height()-rect.bottom,
rect.right, rect.bottom);
QRect pDescRectMid = QRect(rect.left, rect.top,
fRect.width()-(rect.left+rect.right), fRect.height()-(rect.top+rect.bottom));
QRect fDescRectMidTop = QRect(rect.left, 0,
fRect.width()-(rect.left+rect.right), rect.top);
QRect fDescRectMidLeft = QRect(0, rect.top, rect.left,
fRect.height()-(rect.top + rect.bottom));
QRect pDescRectMidRight = QRect(fRect.width()-rect.right, rect.top,
rect.right, fRect.height()-(rect.top + rect.bottom));
QRect fDescRectMidBottom = QRect(rect.left, fRect.height()-rect.bottom,
fRect.width()-(rect.left+rect.right), rect.bottom);
QPixmap map1 = map.copy(srcRect1);
QPixmap map2 = map.copy(srcRect2);
QPixmap map3 = map.copy(srcRect3);
QPixmap map4 = map.copy(srcRect4);
QPixmap mapMid = map.copy(pSrcRectMid);
QPixmap mapMidLeft = map.copy(pSrcRectMidLeft);
QPixmap mapMidRight = map.copy(pSrcRectMidRight);
QPixmap mapMidTop = map.copy(pSrcRectMidTop);
QPixmap mapMidBottom = map.copy(pSrcRectMidBottom);
// new pixmap size
QPixmap mapMidNew(fRect.width(), fRect.height());
QPainter painter(&mapMidNew);
// for four corners
painter.drawPixmap(descRect1, map1, map1.rect());
painter.drawPixmap(descRect2, map2, map2.rect());
painter.drawPixmap(descRect3, map3, map3.rect());
painter.drawPixmap(descRect4, map4, map4.rect());
// mid and for four borders
painter.drawPixmap(pDescRectMid, mapMid, mapMid.rect());
painter.drawPixmap(fDescRectMidLeft, mapMidLeft, mapMidLeft.rect());
painter.drawPixmap(pDescRectMidRight, mapMidRight, mapMidRight.rect());
painter.drawPixmap(fDescRectMidTop, mapMidTop, mapMidTop.rect());
painter.drawPixmap(fDescRectMidBottom, mapMidBottom, mapMidBottom.rect());
//mapMidNew.save("kk02.png");
return mapMidNew;
}
Form::~Form()
{
}
补允一下,这是一个测试,若稍改进一点,应该有个类似结构:
typedef struct PIXMAPINFO
{
QRect rectTopLeft;
QRect rectTopRight;
QRect rectBottomLeft;
QRect rectBottomRight;
QRect rectMiddle;
QRect rectMidLeft;
QRect rectMidTop;
QRect rectMidRight;
QRect rectMidBottom;
QPixmap pixTopLeft;
QPixmap pixTopRight;
QPixmap pixBottomLeft;
QPixmap pixBottomRight;
QPixmap pixMid;
QPixmap pixMidLeft;
QPixmap pixMidTop;
QPixmap pixMidRight;
QPixmap pixMidBottom;
} PixmapInfo;
这样理解比较清晰一点
PixmapInfo src;
PixmapInfo desc;
问题:
1. 当切换不同皮肤时,应该把src信息与desc信息分开,否则在resizeEvent中调用会造成拉伸窗口(改变大小)时显得不流畅
2. 当图拉大时,会出现占用内存增大
img;
img.load("test.jpg");
for (int i = 0; i < img.height(); i++)
{
for (int j = 0; j < img.width(); j++)
{
if(img.pixel(j, i) == qRgba(255,255,255,255))
img.setPixel(j, i, Qt::transparent);//Or you can use qRgba(0,0,0,0) instead for trans
}
}
img.save("changed.png");//I dont remember that png can alpha channel or not.