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

相关推荐
nanxun88631 分钟前
记一次诡异的 Docker 容器"串包"故障排查
java
xcyxiner1 小时前
DicomViewer (后台线程处理文件)4
qt
用户1563068103513 小时前
Day01 | Java 基础(Java SE)
java
行者全栈架构师5 小时前
Maven dependency:tree 的 8 个高级用法
java·后端
xcyxiner9 小时前
DicomViewer (添加模型类)3
qt
行者全栈架构师9 小时前
IDEA 中 Maven 项目的 15 个红色报错快速解决方法
java·后端
令人头秃的代码0_09 小时前
mac(m5)平台编译openjdk
java
xcyxiner1 天前
DicomViewer (目录调整) 2
qt
xcyxiner1 天前
dcmtk vtk vtk-dicom(gdcm) 编译(debug) v2
qt
倔强的石头_1 天前
《Kingbase护城河》——数据库存储空间全景探测与精细化瘦身实战
数据库