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);

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM