简体   繁体   English

Qt:QWidget带QGraphicsView / QGraphicsScene上的一个子项不起作用

[英]Qt: QWidget with a child on QGraphicsView/QGraphicsScene not working

I'm looking to create a view with a base image in the background and draggable widgets over the top. 我正在寻找一个在背景中具有基础图像并在顶部具有可拖动小部件的视图。

The setup I have is like so: 我的设置如下所示:

MainWindow::MainWindow(QWidget * parent) : QMainWindow(parent) {
    ui->graphicsView->setScene(new QGraphicsScene(nullptr));
    ui->graphicsView->scene()->addWidget(new BackgroundWidget(nullptr));
}

This works fine. 这很好。 So does: 也是如此:

MainWindow::MainWindow(QWidget * parent) : QMainWindow(parent) {
    ui->graphicsView->setScene(new QGraphicsScene(nullptr));
    BackgroundWidget * bgWidget = new BackgroundWidget(nullptr);
    new ForegroundWidget(bgWidget);
    ui->graphicsView->scene()->addWidget(bgWidget);
}

However, I want to add foreground widgets dynamically, and it doesn't work. 但是,我想动态添加前景小部件,但这不起作用。 What I've noticed is that the following doesn't add the foreground widget at all: 我注意到的是,以下代码根本没有添加前景小部件:

MainWindow::MainWindow(QWidget * parent) : QMainWindow(parent) {
    ui->graphicsView->setScene(new QGraphicsScene(nullptr));
    BackgroundWidget * bgWidget = new BackgroundWidget(nullptr);
    ui->graphicsView->scene()->addWidget(bgWidget);
    new ForegroundWidget(bgWidget);
}

And this adds the foreground widget, but doesn't allow for semi-transparency with the background (which is needed for the project): 并且这添加了前景小部件,但不允许与背景进行半透明(这是项目所需的):

MainWindow::MainWindow(QWidget * parent) : QMainWindow(parent) {
    ui->graphicsView->setScene(new QGraphicsScene(nullptr));
    BackgroundWidget * bgWidget = new BackgroundWidget(nullptr);
    ui->graphicsView->scene()->addWidget(bgWidget);
    ui->graphicsView->scene()->addWidget(new ForegroundWidget(nullptr));
}

Any ideas as to why adding a child widget after adding the background widget isn't working? 关于为什么在添加背景小部件之后添加子小部件的任何想法都不起作用? If so, any ideas how to fix it? 如果是这样,有什么想法如何解决?

Thanks. 谢谢。

EDIT: Corrected some silly typing errors in the code blocks. 编辑:更正了代码块中的一些愚蠢的键入错误。

问题的形象

The image is a reduced version of the issue; 该图像是该问题的简化版本; the widgets are both set to be semi-transparency (alpha at 100 out of 255), yet one widget seems to be above the other. 这些小部件都设置为半透明(alpha在255中为100),但是一个小部件似乎位于另一个之上。 I get this if I add the 'foreground' widget in any way. 如果我以任何方式添加“前景”小部件,我都会得到这个。 Below is what I was expecting to get: 以下是我期望得到的:

好的版本

As I say, if I add them before attaching to the scene, it works fine, but I need to be able to add to the scene at run time. 就像我说的那样,如果我在将它们附加到场景之前添加它们,则效果很好,但是我需要能够在运行时添加到场景。

You have several memory leak problems in your code. 您的代码中有几个内存泄漏问题。

Depending on BackgroundWidget and ForegroundWidget implementation, you either want 根据BackgroundWidgetForegroundWidget实现,您要么想要

MainWindow::MainWindow(QWidget * parent) : QMainWindow(parent) {
    ui->graphicsView->setScene(new QGraphicsScene(nullptr));
    BackgroundWidget * bgWidget = new BackgroundWidget(nullptr);
    ForegroundWidget * fgWidget = new ForegroundWidget(bgWidget);
    ui->graphicsView->scene()->addWidget(fgWidget);
}

or 要么

MainWindow::MainWindow(QWidget * parent) : QMainWindow(parent) {
    ui->graphicsView->setScene(new QGraphicsScene(nullptr));
    BackgroundWidget * bgWidget = new BackgroundWidget(nullptr);
    ForegroundWidget * fgWidget = new ForegroundWidget(nullptr);
    ui->graphicsView->scene()->addWidget(bgWidget);
    ui->graphicsView->scene()->addWidget(fgWidget);
}

Complete example: 完整的例子:

#include <QtGui>

class MainWindow : public QMainWindow
{
public:
    MainWindow(QWidget *parent = 0) : QMainWindow(parent) {
        QGraphicsView *graphicsView = new QGraphicsView;
        setCentralWidget(graphicsView);

        QGraphicsScene *scene = new QGraphicsScene;
        graphicsView->setScene(scene);

        QWidget *fgWidget = new QWidget;
        fgWidget->setStyleSheet("background: rgba(255, 0, 0, 100)");
        fgWidget->setMinimumSize(100, 100);
        fgWidget->setMaximumSize(100, 100);

        QWidget *bgWidget = new QWidget;
        bgWidget->setStyleSheet("background: rgba(0, 0, 255, 100);");
        bgWidget->setMinimumSize(100, 100);
        bgWidget->setMaximumSize(100, 100);
        bgWidget->move(50, 50);

        scene->addWidget(fgWidget);
        scene->addWidget(bgWidget);
    }
};

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

    MainWindow mainWindow;
    mainWindow.show();

    return app.exec();
}

在此处输入图片说明

If you want fgWidget to be parent of bgWidget : 如果您希望fgWidget成为bgWidgetbgWidget

    QWidget *bgWidget = new QWidget(fgWidget);
    ....
    scene->addWidget(fgWidget);
    //scene->addWidget(bgWidget); // don't add it to the scene

在此处输入图片说明

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

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