Qt栅格布局的示例

cpp 复制代码
    QGridLayout * layout=new QGridLayout;
    for(int i=0;i<10;i++)
    {
        for(int j=0;j<6;j++){
            QLabel *label=new QLabel(this);
            label->setText(QString("%1行%2列").arg(i).arg(j));
            layout->addWidget(label,i,j);
        }
    }
    ui->widget->setLayout(layout);

这样写程序会崩溃的:

cpp 复制代码
    QGridLayout * layout=new QGridLayout;
    for(int i=0;i<10;i++)
    {
        for(int j=0;j<6;j++){
            QLabel label;
            label.setText(QString("%1行%2列").arg(i).arg(j));
            layout->addWidget(&label,i,j);
        }
    }
    ui->widget->setLayout(layout);

因为QLabel的生存周期太短了。

相关推荐
程序猿大帅2 小时前
别再只当调包侠了:用 Spring AI 落地 Function Calling,我被大模型硬生生砸出了三个大坑
java
程序员晓琪3 小时前
约定大于配置:基于 Java 包名自动生成 API 版本路由的最佳实践
java·spring boot·后端
Flittly4 小时前
【AgentScope Java新手村系列】(11)中断与恢复
java·spring boot·spring
众少成多积小致巨4 小时前
JNI (Java Native Interface) 技术手册中文参考指南
android·java·c++
东坡白菜4 小时前
破局全栈:前端开发的Java入门实战记录—JPA(2)
java·后端
SimonKing10 小时前
艹,维护AI写的代码,我心态崩了......
java·后端·程序员
用户2986985301411 小时前
Java Word 文档样式进阶:段落与文本背景色设置完全指南
java·后端
倔强的石头_12 小时前
KingbaseES 新版MySQL 兼容版体验:旧版迁移 + 功能实测
数据库
小bo波1 天前
从"任意文件复制"深挖Java I/O:字符流与字节流的本质抉择
java·nio·io流·后端开发·文件复制
xcyxiner1 天前
DicomViewer (dcmtk读取dcm文件)5
qt