问题
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. 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