Qt-制作与QGraphicsView重叠的面板

时间:2019-02-21 08:17:26

标签: c++ qt qgraphicsview qlayout

我试图制作一个显示一些数据的面板,当我按下按钮时会添加这些数据。我将通过这些图像进行解释:

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

如果我单击“帮助”,它将在其上方显示一个永远不会失去焦点的窗口enter image description here

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

1 个答案:

答案 0 :(得分: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();