我最近尝试将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();
}
最佳答案 根据我评论中的错误报告的解决方案:
this is an unavoidable consequence of how QGLWidget works
在其他地方建议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();
}