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

问题描述 投票:1回答:1

我正在尝试创建一个显示一些数据的面板,当我按下按钮时会添加这些数据。我将通过这些图片解释它:

这将是应用程序的初始状态,一个带有QGraphicsViewThis would be the initial state of the app的窗口

如果我点击“帮助”它应该在它上面显示一个永远不会离开focusenter image description here的窗口

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

c++ qt qgraphicsview qlayout
1个回答
1
投票

您可以在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类在场景中嵌入小部件:

    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();
© www.soinside.com 2019 - 2024. All rights reserved.