You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Qt程序运行正常但弹出窗口无任何内容显示该如何解决?

问题原因

QMainWindow本身有固定的布局结构,必须通过setCentralWidget()指定中心承载部件,不能直接给QMainWindow实例设置布局。你当前代码直接将布局的父对象设为MainWindow,所有控件没有挂载到正确的渲染容器上,因此无法显示。

修复方案

修改MainWindow构造函数逻辑,先创建中心部件,再将布局绑定到中心部件上即可,修改后代码如下:

#include "mainwindow.h"
#include <QVBoxLayout>
#include <QGridLayout>

MainWindow::MainWindow()
{
    spinbox = new QSpinBox;
    slider1 = new QSlider(Qt::Horizontal);
    dial = new QDial;
    progressbar = new QProgressBar;

    // 新增:创建QMainWindow的中心承载部件
    QWidget *centralWidget = new QWidget(this);
    setCentralWidget(centralWidget);

    // 布局绑定到中心部件,而非MainWindow本身
    QVBoxLayout *lay1 = new QVBoxLayout(centralWidget);

    lay1 -> addWidget(spinbox);
    lay1 -> addWidget(slider1);
    lay1 -> addWidget(dial);
    lay1 -> addWidget(progressbar);
}

修改后重新编译运行,所有控件即可正常显示。

内容的提问来源于stack exchange,提问作者user13011796

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.28 19:24:03