Widget background not transparent when using QGraphicsView but transparent when using QGridLayout

蓝咒 提交于 2021-01-27 12:50:50

问题


When I was using QGridLayout to display my widgets, only the widget was shown and the part of the image that was transparent was not shown. Now I switched to using QGraphicsScene and QGraphicsView, and now my images have a gray background wherever they used to be transparent.

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);
} 

That's how the image is displayed on my widget. When I used the code,

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

the background is transparent. But when I use,

scene->addWidget(piece);

The widget has a gray background. How can I make it transparent? The full code can be found here if necessary (probably won't be necessary): https://github.com/gsingh93/Chess

EDIT: I can't figure this problem out at all... I tried using setAutoFillBackground(false); but that didn't work. So my last hope was converting my whole class from a QWidget to a QGrahhicsItem. That didn't work and the background of the image is still gray instead of transparent. If you can't figure out what's wrong with this code can someone please post or link me to an example of how to display an image with a transparent background using QGraphicsScene? Here is the original code, followed by the QGraphicsItem code, followed by my main function.

#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();
}

回答1:


Did you try using a stylesheet to set the background transparency?

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


来源:https://stackoverflow.com/questions/7238027/widget-background-not-transparent-when-using-qgraphicsview-but-transparent-when

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!