简体   繁体   English

使用QGraphicsView时窗口小部件背景不透明但使用QGridLayout时透明

[英]Widget background not transparent when using QGraphicsView but transparent when using QGridLayout

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. 当我使用QGridLayout显示我的小部件时,只显示了小部件,并且未显示透明的图像部分。 Now I switched to using QGraphicsScene and QGraphicsView, and now my images have a gray background wherever they used to be transparent. 现在我切换到使用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);
} 

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 如有必要,可以在这里找到完整的代码(可能没有必要): https//github.com/gsingh93/Chess

EDIT: I can't figure this problem out at all... I tried using setAutoFillBackground(false); 编辑:我根本无法解决这个问题...我尝试使用setAutoFillBackground(false); but that didn't work. 但那没用。 So my last hope was converting my whole class from a QWidget to a QGrahhicsItem. 所以我最后的希望是将我的全班从QWidget转换为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? 如果您无法弄清楚此代码有什么问题,有人可以发布或链接我的示例如何使用QGraphicsScene显示具有透明背景的图像? Here is the original code, followed by the QGraphicsItem code, followed by my main function. 这是原始代码,后跟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();
}

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM