3

当我使用 QGridLayout 显示我的小部件时,只显示了小部件,而没有显示透明的图像部分。现在我切换到使用 QGraphicsScene 和 QGraphicsView,现在我的图像在过去是透明的地方都有灰色背景。

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代码,然后是我的main函数。

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

2 回答 2

11

您是否尝试使用样式表来设置背景透明度?

yourWidget->setStyleSheet("background-color: transparent;");
于 2011-09-04T07:40:27.440 回答
0

我确认你的问题。此外,laurents 解决方案无法在 Linux 中运行。但对我来说,下一步行动是:

在通过 addWidget() 方法把你的 QWidget 放到 QGraphicsScene 之前,给他设置下一个标志:

board->setAttribute( Qt::WA_TranslucentBackground );

详细解释在这里(ru):https ://webhamster.ru/mytetrashare/index/mtb0/1625823664d5m6pmpy7s

于 2021-07-09T10:28:52.907 回答