Qt - 制作一个与 QGraphicsView 重叠的面板

Qt - Making a panel that overlaps a QGraphicsView

我正在尝试制作一个显示一些数据的面板,当我按下一个按钮时,这些数据就会被添加。我将通过这些图像来解释它:

这将是应用程序的初始状态,一个带有 QGraphicsView

的 window

如果我单击 "Help",它应该会在其上方显示一个 window,它永远不会失焦

我研究过使用 QDockWidget,但这只是在它旁边创建了一个面板,这不是我想要的。如果有人知道如何做到这一点,我将不胜感激,谢谢。

您可以在 QGraphicsView 中设置子控件并将其视为常规 QWidget:

    QApplication app(argc, argv);
    QGraphicsScene* scene = new QGraphicsScene(0, 0, 1000, 1000);
    QGraphicsView* view = new QGraphicsView(scene);
    view->show();

    QPushButton* button = new QPushButton("Show label");
    QLabel* label = new QLabel("Foobar");
    QVBoxLayout* layout = new QVBoxLayout(view);
    layout->setAlignment(Qt::AlignRight | Qt::AlignTop);
    layout->addWidget(button);
    layout->addWidget(label);
    label->hide();
    QObject::connect(button, &QPushButton::clicked, label, &QLabel::show);
    return app.exec();

当您单击按钮时,标签将在 QGraphicsView 中可见。

您还可以使用 QGraphicsProxyWidget class:

在您的场景中嵌入小部件
    QApplication app(argc, argv);
    QGraphicsScene* scene = new QGraphicsScene(0, 0, 1000, 1000);
    scene->addItem(new QGraphicsRectItem(500, 500, 50, 50));
    QGraphicsView* view = new QGraphicsView(scene);
    view->show();

    QWidget* w = new QWidget();
    QGraphicsProxyWidget* proxy = new QGraphicsProxyWidget();


    QPushButton* button = new QPushButton("Show label");
    QLabel* label = new QLabel("Foobar");
    QVBoxLayout* layout = new QVBoxLayout(w);
    layout->addWidget(button);
    layout->addWidget(label);
    layout->setAlignment(Qt::AlignRight | Qt::AlignTop);
    label->hide();
    QObject::connect(button, &QPushButton::clicked, label, &QLabel::show);

    proxy->setWidget(w);
    scene->addItem(proxy);
    return app.exec();