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的生存周期太短了。

相关推荐
倔强的石头_10 小时前
《Kingbase护城河》——猎捕慢查询:执行计划的微观解析与索引调优实战
数据库
SelectDB12 小时前
Apache Doris Python UDF:让 SQL 直接调用 Python 生态,支撑 Agent 时代复杂业务逻辑
大数据·数据库·python
Flittly14 小时前
【AgentScope Java新手村系列】(16)从RAG到多路检索
java·spring boot·spring
小兔崽子去哪了14 小时前
Java 生成二维码解决方案
java·后端
人活一口气18 小时前
从JVM调优到MCP协议:Java全栈技术体系深度总结与企业级架构实践
java·spring boot
NE_STOP20 小时前
Vibe Coding -- 完整项目案例实操
java
荣码20 小时前
GraphRAG:普通RAG只能回答"点"的问题,我踩了4个坑才搞懂
java·python
SimonKing20 小时前
Google第三方授权登录
java·后端·程序员
明月光81820 小时前
从一行 @Builder 说起:重新拾起 Java 的 Lombok、注解与 Builder 模式
java
考虑考虑1 天前
Mybatis实现批量插入
java·后端·mybatis