Design custom QT Widget on top of QT Window

好久不见. 提交于 2021-02-08 10:22:47

问题


I have a QT main window and on top of this I want to add a widget ( containing buttons), as similar to image below. If I add a dock widget , it is added in separate row, but not added as overlay on existing main window. enter image description here Any inputs ?


回答1:


The easiest is to set your overlay widget's parent to be the main window. But because it will not be in any layout you have to take care of its geometry yourself. In case you want to have multiple overlays, the last added will be the top most.

#include <QApplication>
#include <QtGui>

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

    QMainWindow *window = new QMainWindow();
    QWidget *centralWiddget = new QWidget();
    window->setCentralWidget(centralWiddget);

    QVBoxLayout *layout = new QVBoxLayout(centralWiddget);

    QPushButton *button = new QPushButton("Button in a layout");
    layout->addWidget(button);

    QPushButton *overlayButton = new QPushButton("Button overlay");
    overlayButton->setParent(window);
    overlayButton->setGeometry(40, 40, 120, 30)

    window->show();

    return app.exec();
}



回答2:


You should look into using QStackedLayout to so this.



来源:https://stackoverflow.com/questions/19438770/design-custom-qt-widget-on-top-of-qt-window

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!