C++ 如果我没有';我不想使用QMainWindow';什么样的布局?QMainWindow应首选在哪里?

C++ 如果我没有';我不想使用QMainWindow';什么样的布局?QMainWindow应首选在哪里?,c++,qt,qmainwindow,C++,Qt,Qmainwindow,QMainWindow有自己的布局,不能直接设置。 您可能应该在中心小部件上设置布局,或者如果您不想使用QMainWindow的布局/功能,则可能根本不使用QMainWindow 我有自己的一套按钮和其他东西,我想在网格中排列。 我是否应该将我的小部件添加到QMainWindow的默认布局? 如果我不想使用QMainWindow的布局,我应该使用什么来代替QMainWindow?QMainWindow应首选在哪里?来自: QMainWindow有自己的布局,您可以在其中添加QToolBars、Q

QMainWindow有自己的布局,不能直接设置。 您可能应该在中心小部件上设置布局,或者如果您不想使用QMainWindow的布局/功能,则可能根本不使用QMainWindow

我有自己的一套按钮和其他东西,我想在网格中排列。
我是否应该将我的小部件添加到QMainWindow的默认布局?

如果我不想使用QMainWindow的布局,我应该使用什么来代替QMainWindow?QMainWindow应首选在哪里?

来自:

QMainWindow有自己的布局,您可以在其中添加QToolBars、QDockWidgets、QMenuBar和QStatusBar。布局有一个中心区域,可以被任何类型的小部件占据。您可以在下面看到布局的图像

如果我不想使用QMainWindow的布局,我应该使用什么来代替QMainWindow?QMainWindow应首选在哪里? 因此,如果您对
QToolBar
s、
QDockWidget
s、
QMenuBar
s或
QStatusBar
s感兴趣,您应该使用
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(&centralWidget); //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(&centralWidget);
    //main window can have a toolbar too
    QToolBar* myToolBar = mainWindow.addToolBar("myToolBar");
    myToolBar->addAction("myToolBar action");
    //show mainwindow
    mainWindow.show();
    
    return a.exec();
}
#包括
int main(int argc,char*argv[]){
质量保证申请a(argc、argv);
QMainWindow主窗口;
QWidget centralWidget;//这是用于放置按钮的小部件
QGridLayout centralLayout(¢ralWidget);//设置小部件的布局
//添加按钮
对于(int i=0;i,从:

QMainWindow有自己的布局,您可以在其中添加QToolBars、QDockWidgets、QMenuBar和QStatusBar。该布局的中心区域可以被任何类型的小部件占据。您可以在下面看到布局的图像

如果我不想使用QMainWindow的布局,我应该使用什么来代替QMainWindow?QMainWindow应该首选在哪里? 因此,如果您对
QToolBar
s、
QDockWidget
s、
QMenuBar
s或
QStatusBar
s感兴趣,您应该使用
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(&centralWidget); //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(&centralWidget);
    //main window can have a toolbar too
    QToolBar* myToolBar = mainWindow.addToolBar("myToolBar");
    myToolBar->addAction("myToolBar action");
    //show mainwindow
    mainWindow.show();
    
    return a.exec();
}
#包括
int main(int argc,char*argv[]){
质量保证申请a(argc、argv);
QMainWindow主窗口;
QWidget centralWidget;//这是用于放置按钮的小部件
QGridLayout centralLayout(¢ralWidget);//设置小部件的布局
//添加按钮
对于(int i=0;i