2011-08-30 78 views
3

當我使用QGridLayout顯示窗口小部件時,只顯示窗口小部件,並且未顯示圖像的透明部分。現在我切換到使用QGraphicsScene和QGraphicsView,現在我的圖像灰色背景,他們曾經是透明的。使用QGraphicsView時,窗口小部件背景不透明但在使用QGridLayout時透明

void Piece::paintEvent(QPaintEvent *) 
{ 
    string image = ":/images/" + color + piece + ".png"; 
    pixmap.load(image.c_str()); 
    //pixmap.setMask(pixmap.createMaskFromColor(QColor(240, 240, 240))); 

    QPainter paint(this); 
    paint.drawPixmap(0, 0, pixmap); 
} 

這就是圖像如何顯示在我的小部件上。當我使用代碼時,

layout->addWidget(0,0,1,1); 

背景是透明的。但是當我使用時,

scene->addWidget(piece); 

小部件有灰色背景。我怎樣才能讓它透明?完整的代碼可以在這裏找到,如果有必要(可能不需要):https://github.com/gsingh93/Chess

編輯:我無法找出這個問題了...我嘗試使用setAutoFillBackground(false);但那不起作用。所以我最後的希望是把我的整個班級從QWidget轉換爲QGrahhicsItem。這不起作用,圖像的背景仍然是灰色的,而不是透明的。如果你不知道這段代碼有什麼問題,有人可以發帖或鏈接我一個如何使用QGraphicsScene顯示透明背景圖像的例子嗎?這是原始代碼,接着是QGraphicsItem代碼,後面是我的主要功能。

#include "headers/piece.h" 
#include <QPainter> 
#include <QMouseEvent> 
#include <QBitmap> 
#include <QCursor> 
using namespace std; 

Piece::Piece(string color, string piece, QWidget *parent) : 
    QWidget(parent) 
{ 
    this->piece = piece; 
    this->color = color; 
    this->setMaximumHeight(36); 
    this->setMaximumWidth(36); 
    x = 0; 
    y = 0; 
    setMouseTracking(false); 
} 

void Piece::paintEvent(QPaintEvent *) 
{ 
    string image = ":/images/" + color + piece + ".png"; 
    pixmap.load(image.c_str()); 
    //pixmap.setMask(pixmap.createMaskFromColor(QColor(240, 240, 240))); 

    QPainter paint(this); 
    paint.drawPixmap(0, 0, pixmap); 
} 

void Piece::setPosition(int file, int rank) 
{ 
    pixmap.load(":/images/whitepawn.png"); 
    QImage image = pixmap.toImage(); 
    x = (file-1)*50 + 18;// - image.width()/2; 
    y = (rank-1)*50 + 18;// - image.height()/2; 
    move(x, y); 
} 

void Piece::mouseMoveEvent(QMouseEvent *event) 
{ 
    if(event->buttons() == Qt::LeftButton) 
    { 
    x = event->globalX()-18; 
    y = event->globalY()-18; 
    move(x,y); 
    } 
} 

#include "piece2.h" 
#include <QPainter> 
#include <QMouseEvent> 
#include <QBitmap> 
#include <QCursor> 
#include <QGraphicsSceneMouseEvent> 
using namespace std; 

Piece2::Piece2(string color, string piece, QObject *parent) : 
    QGraphicsItem() 
{ 
    this->piece = piece; 
    this->color = color; 
    //this->setMaximumHeight(36); 
    //this->setMaximumWidth(36); 
    x = 0; 
    y = 0; 
    //setMouseTracking(false); 
} 

void Piece2::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) 
{ 
    string image = ":/images/" + color + piece + ".png"; 
    pixmap.load(image.c_str()); 
    //pixmap.setMask(pixmap.createMaskFromColor(QColor(240, 240, 240))); 

    //QPainter paint(this); 
    painter->drawPixmap(0, 0, pixmap); 
} 

void Piece2::setPosition(int file, int rank) 
{ 
    pixmap.load(":/images/whitepawn.png"); 
    QImage image = pixmap.toImage(); 
    x = (file-1)*50 + 18;// - image.width()/2; 
    y = (rank-1)*50 + 18;// - image.height()/2; 
    setPos(x, y); 
} 

void Piece2::mouseMoveEvent(QGraphicsSceneMouseEvent *event) 
{ 
    if(event->buttons() == Qt::LeftButton) 
    { 
// x = event->globalX()-18; 
// y = event->globalY()-18; 
    setPos(x,y); 
    } 
} 

#include <QtGui> 
#include <QGraphicsScene> 
#include <QGraphicsView> 
#include "headers/board.h" 
#include "headers/pawn.h" 
#include "headers/knight.h" 
#include "headers/bishop.h" 
#include "headers/rook.h" 
#include "headers/king.h" 
#include "headers/queen.h" 

int main(int argc, char *argv[]) 
{ 
    QApplication app(argc, argv); 
    QGraphicsScene *scene = new QGraphicsScene(); 
    QGraphicsView *view = new QGraphicsView(); 
    Board board; 

    scene->addWidget(&board); 
    scene->addWidget(board.pawn2); 
    board.pawn2->setPosition(1,1); 

    //view->viewport()->setPalette(QColor(Qt::transparent)); 
    //view->viewport()->setAutoFillBackground(false); 
    view->setScene(scene); 
    //view->setBackgroundRole(QPalette::NoRole); 
    view->show(); 
    return app.exec(); 
} 

回答

7

您是否嘗試過使用樣式表來設置背景透明度?

yourWidget->setStyleSheet("background-color: transparent;"); 
+2

哇......我可以發誓我曾試過這個。我可能弄亂了參數。無論如何,謝謝你,這個問題已經讓我的進度停了一週!我不知道爲什麼這不是默認設置,但它看起來像佈局默認情況下是透明的,這也應該。 – gsingh2011