在實現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.