QT之获取布局内容及删除布局

1、删除布局

图1

QLayout *p = ui->verticalLayout_7->itemAt(0)->layout();
while(p->count()){
    QWidget *pWidget = p->itemAt(0)->widget();
    pWidget->setParent(NULL);
    p->removeWidget(pWidget);
    delete pWidget;
}
ui->verticalLayout_7->removeItem(ui->verticalLayout_7->itemAt(0));

图2

    QWidget *p=this->ui->verticalLayout->itemAt(0)->widget();
    p->setParent (NULL);    // 父控件置空
    this->ui->verticalLayout->removeWidget(p);
    delete p; // 清除内存

2、获取内容

图1

    QStringList keyJson, valueJon;
    int a = ui->verticalLayout_7->count();  // 获取该布局下的布局数
    int i = 0;
    while(a)
    {
        QLayoutItem *p=this->ui->verticalLayout_7->itemAt(i);
        int count = p->layout()->count();  // 获取该布局下的布局类的控件数
        int j = 0;
        while(count){
            if(j == 1){
                QLayoutItem *pw = p->layout()->itemAt(j);
                QLineEdit *le = qobject_cast<QLineEdit *>(pw->widget());
                keyJson.append(le->text());
            }
            else if(j == 3){
                QLayoutItem *pw = p->layout()->itemAt(j);
                QLineEdit *le = qobject_cast<QLineEdit *>(pw->widget());
                valueJson.append(le->text());
            }
            j++;
            count--;
        }
        i++;
        a--;
    }

图2

    QStringList strList;
    int a = ui->verticalLayout->count();
    qDebug() << a;
    int i = 0;
    QStringList strList;
    while(a)
    {
        QLayoutItem *p=this->ui->verticalLayout->itemAt(i);
        QTextEdit *le = qobject_cast<QTextEdit *>(p->widget());
        strList.append(le->toPlainText());
        i++;
        a--;
    }

3、增加布局

图1

    QLabel *lKey = new QLabel("KEY:", this);
    QLineEdit *leKey = new QLineEdit(this);
    QLabel *lValue = new QLabel("VALUE:", this);
    QLineEdit *leValue = new QLineEdit(this);
    QHBoxLayout *hbox = new QHBoxLayout;
    hbox->addWidget(lKey);
    hbox->addWidget(leKey);
    hbox->addWidget(lValue);
    hbox->addWidget(leValue);
    ui->verticalLayout_7->addLayout(hbox);

图2

    QTextEdit *leNew = new QTextEdit(this);
    ui->verticalLayout->addWidget(leNew);

原文链接

QT之获取布局内容及删除布局-QT开发中文网QT之获取布局内容及删除布局https://qt.0voice.com/?id=1005

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值