具有透明背景的小部件在具有QGLWidget视口的QGraphicsView上方。

4

我最近尝试将我的QGraphicsView的视口设置为一个QGLWidget,以了解其性能。我发现以前拥有透明背景的小部件(样式化按钮、没有背景的游戏菜单)现在都有黑色背景。是否有一种简单的方法可以保持透明度并仍然使用QGLWidget视口?

#include <QtCore>
#include <QtWidgets>
#include <QGLWidget>

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    QMainWindow mainWindow;

    QGraphicsView *view = new QGraphicsView(&mainWindow);

    view->setViewport(new QGLWidget(QGLFormat(QGL::SampleBuffers)));
    view->setViewportUpdateMode(QGraphicsView::FullViewportUpdate);

    QPushButton *button = new QPushButton(&mainWindow);
    button->setStyleSheet("QPushButton {"
      "border: 2px solid #8f8f91;"
      "border-radius: 6px;"
      "background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,stop: 0 #f6f7fa, stop: 1 #dadbde);"
      "min-width: 80px;"
      "}");

    QWidget *widget = new QWidget(&mainWindow);
    widget->move(0, 100);

    mainWindow.setCentralWidget(view);
    mainWindow.resize(200, 200);
    mainWindow.show();

    return app.exec();
}

QGLWidget black background example


https://bugreports.qt-project.org/browse/QTBUG-36009 - Mitch
你可以在按钮上使用 setAttribute(Qt::WA_NoSystemBackground)。这对我有效。 - user2123079
1个回答

2
根据我评论中的错误报告分辨率:
这是QGLWidget工作方式不可避免的后果。
其他地方建议使用QGraphicsProxyWidget来解决问题。
#include <QtCore>
#include <QtWidgets>
#include <QGLWidget>

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    QMainWindow mainWindow;

    QGraphicsView *view = new QGraphicsView(&mainWindow);
    QGraphicsScene *scene = new QGraphicsScene;
    view->setScene(scene);

    view->setViewport(new QGLWidget(QGLFormat(QGL::SampleBuffers)));
    view->setViewportUpdateMode(QGraphicsView::FullViewportUpdate);

    QPushButton *button = new QPushButton;
    button->setStyleSheet("QPushButton {"
      "border: 2px solid #8f8f91;"
      "border-radius: 6px;"
      "background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,stop: 0 #f6f7fa, stop: 1 #dadbde);"
      "min-width: 80px;"
      "}");

    scene->addWidget(button);

    mainWindow.setCentralWidget(view);
    mainWindow.resize(200, 200);
    mainWindow.show();

    return app.exec();
}

qgraphicsproxywidget-screenshot


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接