如果我不想使用 QMainWindow 的布局,我应该使用什么来代替 QMainWindow? QMainWindow 应该首选在哪里?
What should I use instead of QMainWindow if I don't want to use QMainWindow's layout? Where should QMainWindow be preferred?
QMainWindow comes with its own layout, you can't set that directly.
You probably should be setting your layout on the central widget, or possibly not using a QMainWindow at all if you don't want its layout/features.
我有自己的一组按钮和其他我想排列在网格中的东西。
我应该将我的小部件添加到 QMainWindow 的默认布局吗?
如果我不想使用 QMainWindow 的布局,我应该使用什么来代替 QMainWindow? QMainWindow 应该首选哪里?
来自 docs:
QMainWindow has its own layout to which you can add QToolBars, QDockWidgets, a QMenuBar, and a QStatusBar. The layout has a center area that can be occupied by any kind of widget. You can see an image of the layout below.
如果我不想使用QMainWindow的布局,我应该使用什么来代替QMainWindow? QMainWindow 应该首选哪里?
因此,如果您有兴趣拥有 QToolBar
、QDockWidget
、QMenuBar
或 QStatusBar
,您应该使用 QMainWindow
.否则,您可以只使用普通的 QWidget
.
我应该将我的小部件添加到 QMainWindow 的默认布局吗?
不,您无权访问 QMainWindow
的布局。您应该有一个 QWidget
包含布局中的所有小部件(例如您的按钮),然后将该小部件用作 QMainWindow
的中央小部件,例如:
#include <QtWidgets>
int main(int argc, char* argv[]){
QApplication a(argc, argv);
QMainWindow mainWindow;
QWidget centralWidget; //this is the widget where you put your buttons
QGridLayout centralLayout(¢ralWidget); //sets layout for the widget
//add buttons
for(int i=0; i<3; i++)
for(int j=0; j<3; j++)
centralLayout
.addWidget(new QPushButton(
QStringLiteral("(%0, %1)")
.arg(i).arg(j)),
i, j);
//use your widget inside the main window
mainWindow.setCentralWidget(¢ralWidget);
//main window can have a toolbar too
QToolBar* myToolBar = mainWindow.addToolBar("myToolBar");
myToolBar->addAction("myToolBar action");
//show mainwindow
mainWindow.show();
return a.exec();
}
QMainWindow comes with its own layout, you can't set that directly. You probably should be setting your layout on the central widget, or possibly not using a QMainWindow at all if you don't want its layout/features.
我有自己的一组按钮和其他我想排列在网格中的东西。
我应该将我的小部件添加到 QMainWindow 的默认布局吗?
如果我不想使用 QMainWindow 的布局,我应该使用什么来代替 QMainWindow? QMainWindow 应该首选哪里?
来自 docs:
QMainWindow has its own layout to which you can add QToolBars, QDockWidgets, a QMenuBar, and a QStatusBar. The layout has a center area that can be occupied by any kind of widget. You can see an image of the layout below.
如果我不想使用QMainWindow的布局,我应该使用什么来代替QMainWindow? QMainWindow 应该首选哪里?
因此,如果您有兴趣拥有 QToolBar
、QDockWidget
、QMenuBar
或 QStatusBar
,您应该使用 QMainWindow
.否则,您可以只使用普通的 QWidget
.
我应该将我的小部件添加到 QMainWindow 的默认布局吗?
不,您无权访问 QMainWindow
的布局。您应该有一个 QWidget
包含布局中的所有小部件(例如您的按钮),然后将该小部件用作 QMainWindow
的中央小部件,例如:
#include <QtWidgets>
int main(int argc, char* argv[]){
QApplication a(argc, argv);
QMainWindow mainWindow;
QWidget centralWidget; //this is the widget where you put your buttons
QGridLayout centralLayout(¢ralWidget); //sets layout for the widget
//add buttons
for(int i=0; i<3; i++)
for(int j=0; j<3; j++)
centralLayout
.addWidget(new QPushButton(
QStringLiteral("(%0, %1)")
.arg(i).arg(j)),
i, j);
//use your widget inside the main window
mainWindow.setCentralWidget(¢ralWidget);
//main window can have a toolbar too
QToolBar* myToolBar = mainWindow.addToolBar("myToolBar");
myToolBar->addAction("myToolBar action");
//show mainwindow
mainWindow.show();
return a.exec();
}