在 QMainWindow 中添加子项

发布于 2025-01-06 03:01:27 字数 312 浏览 0 评论 0原文

如何在 QMainWindow 的相等部分添加两个子 Widget 对象。

MainWindow::MainWindow(QWidget *parent)
     : QMainWindow(parent)

{   TreeArea *ta= new TreeArea(this);
    TreeArea *ta1= new TreeArea(this);
.
.
.
  TreeArea::TreeArea(QWidget *parent) :
 QWidget(parent)
{
.
.
.

How can i add two child Widget objects in equal portion of QMainWindow.

MainWindow::MainWindow(QWidget *parent)
     : QMainWindow(parent)

{   TreeArea *ta= new TreeArea(this);
    TreeArea *ta1= new TreeArea(this);
.
.
.
  TreeArea::TreeArea(QWidget *parent) :
 QWidget(parent)
{
.
.
.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

﹏雨一样淡蓝的深情 2025-01-13 03:01:27

正如 e-zinc 建议的那样,您必须使用布局。假设您想在主窗口中插入两个小部件:

QHBoxLayout *layout = new QHBoxLayout;

QPushButton *button1 = new QPushButton("button1");
QPushButton *button2 = new QPushButton("button2");

layout->addWidget(button1);
layout->addWidget(button2);

setCentralWidget(new QWidget);
centralWidget()->setLayout(layout);

这将水平布局小部件,您将得到以下结果:

QHBoxLayoutExample

如果如果您想使用 QVBoxLayout 垂直布局它们,

我强烈建议您阅读文档。 Qt 中的布局管理

As e-zinc suggested you have to use layouts. Say you want to insert two widgets into the mainwindow:

QHBoxLayout *layout = new QHBoxLayout;

QPushButton *button1 = new QPushButton("button1");
QPushButton *button2 = new QPushButton("button2");

layout->addWidget(button1);
layout->addWidget(button2);

setCentralWidget(new QWidget);
centralWidget()->setLayout(layout);

This will layout widgets horizontally and you will get this result:

QHBoxLayoutExample

And if you want to layout them vertically use QVBoxLayout

I would strongly suggest reading the documentation. Layout Management in Qt

游魂 2025-01-13 03:01:27

使用QMainWindow::setCentralWidget(QWidget *) 添加您自己的控件。

Use QMainWindow::setCentralWidget(QWidget *) to add your own control.

逆夏时光 2025-01-13 03:01:27

////////如果你想从main.cpp创建////////

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

    QMainWindow* MainWindow = new QMainWindow(NULL);
    QWidget*     cwidget    = new QWidget(MainWindow);
    QPushButton* button1    = new QPushButton(cwidget);
    QPushButton* button2    = new QPushButton(cwidget);

    button1->setText("Button1");
    button2->setText("Button2");

    button1->move(10, 100);
    button2->move(10, 200);

    MainWindow->setCentralWidget(cwidget);
    MainWindow->resize(400, 300);
    MainWindow->show();

    return app.exec();
}

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

    QMainWindow* MainWindow = new QMainWindow(NULL);
    QWidget*     cwidget    = new QWidget(MainWindow);
    QHBoxLayout* layout     = new QHBoxLayout; //horizontal layout
    QPushButton* button1    = new QPushButton("button1");
    QPushButton* button2    = new QPushButton("button2");

    layout->addWidget(button1);
    layout->addWidget(button2);

    MainWindow->setCentralWidget(cwidget);
    MainWindow->centralWidget()->setLayout(layout); //centralWidget() is getcentralWidget()
    MainWindow->resize(400, 300);

    MainWindow->show();

    return app.exec();
}
#endif

////////if you want to create from main.cpp////////

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

    QMainWindow* MainWindow = new QMainWindow(NULL);
    QWidget*     cwidget    = new QWidget(MainWindow);
    QPushButton* button1    = new QPushButton(cwidget);
    QPushButton* button2    = new QPushButton(cwidget);

    button1->setText("Button1");
    button2->setText("Button2");

    button1->move(10, 100);
    button2->move(10, 200);

    MainWindow->setCentralWidget(cwidget);
    MainWindow->resize(400, 300);
    MainWindow->show();

    return app.exec();
}

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

    QMainWindow* MainWindow = new QMainWindow(NULL);
    QWidget*     cwidget    = new QWidget(MainWindow);
    QHBoxLayout* layout     = new QHBoxLayout; //horizontal layout
    QPushButton* button1    = new QPushButton("button1");
    QPushButton* button2    = new QPushButton("button2");

    layout->addWidget(button1);
    layout->addWidget(button2);

    MainWindow->setCentralWidget(cwidget);
    MainWindow->centralWidget()->setLayout(layout); //centralWidget() is getcentralWidget()
    MainWindow->resize(400, 300);

    MainWindow->show();

    return app.exec();
}
#endif
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文