2016-06-13 32 views
0

我在Dialog上配置了一个文件传输管理器,需要动态生成几个UI元素。这里是产生它的功能:在QT中删除一个dinamically生成的用户界面?

qint32 TransferManager::addTransfer(TransferData td){ 

    // Getting the ID for this tranfer 
    qint32 transferID = transfers.size(); 

    td.createGraphicalUI(this,transferID); 

    // Adding it to the UI 
    ui->globalTMLayout->addWidget(td.getFrame()); 

    connect(td.getAcceptButton(),SIGNAL(wasClicked(qint32)),this,SLOT(onTransferAccepted(qint32))); 
    connect(td.getRejectButton(),SIGNAL(wasClicked(qint32)),this,SLOT(onTransferRejected(qint32))); 
    connect(td.getCancelButton(),SIGNAL(wasClicked(qint32)),this,SLOT(onTransferCanceled(qint32))); 

    // Adding the TD 
    transfers << td; 

    // If a transfer is added this needs to be shown 
    this->show(); 

    return transferID; 

} 

一旦传输完成我需要删除的所有元素:

void TransferData::createGraphicalUI(QDialog *parent, qint32 td_id){ 
    status = new QLabel(parent); 
    filename = new QLabel(parent); 
    contact_label = new QLabel(parent); 
    accept = new IDPushButton(parent,td_id); 
    reject = new IDPushButton(parent,td_id); 
    cancel = new IDPushButton(parent,td_id); 
    progress = new QProgressBar(parent); 
    statlayout = new QHBoxLayout(); 
    frameLayout = new QVBoxLayout(); 
    frame = new QFrame(parent); 

    // Stylying the frame 
    frame->setStyleSheet("background-color: rgb(255, 255, 255);"); 

    // Setting the messages. 
    QString htmlHeader = "<html><head/><body><p><span style='font-weight:792; color:#0000ff;'>"; 
    QString htmlEnder = "</span></p></body></html>"; 
    QString contactMsg = "Transfer "; 
    QString filenameMsg = "File name: </span><span>" + getFileToBeSent(); 
    QString statusMsg = "Status: </span><span>"; 

    cancel->setText("Cancel"); 
    cancel->setIcon(QIcon(":/Icons/icons/cancel.png")); 
    cancel->setVisible(false); 


    if (getIsATransfer()){ 
     // This is a transfer TO, the file will be uploaded 
     contactMsg = contactMsg + "to: </span><span> " + getConctacName(); 
     statusMsg = statusMsg + "Waiting for file to be accepted."; 
     statlayout->addWidget(status); 
     statlayout->addWidget(cancel); 
     accept->setVisible(false); 
     reject->setVisible(false); 
    } 
    else{ 
     // This is a transfer FROM, the file will be downlaoded 
     contactMsg = contactMsg + "from: </span><span> " + getConctacName(); 
     statusMsg = statusMsg + "Transfer must be accepted before it begins."; 
     accept->setText("Accept"); 
     accept->setIcon(QIcon(":/Icons/icons/ok.png")); 
     reject->setText("Reject"); 
     reject->setIcon(QIcon(":/Icons/icons/cancel.png")); 
     statlayout->addWidget(status); 
     statlayout->addWidget(accept); 
     statlayout->addWidget(reject); 
     statlayout->addWidget(cancel); 

    } 

    status->setText(htmlHeader + statusMsg + htmlEnder); 
    filename->setText(htmlHeader + filenameMsg + htmlEnder); 
    contact_label->setText(htmlHeader + contactMsg + htmlEnder); 

    // Resettign the progress bar 
    progress->setValue(0); 

    // Putting it all together. 
    frameLayout->addWidget(contact_label); 
    frameLayout->addWidget(filename); 
    frameLayout->addLayout(statlayout); 
    frameLayout->addWidget(progress); 
    frame->setLayout(frameLayout); 

} 

这是从具有TransferData对象列表的函数调用创建了UI。我不喜欢这样写道:

void TransferManager::removeTransferData(qint32 which){ 
    if (which < transfers.size()){ 

     // Deleting the UI 
     transfers[which].removeGraphicalUI(); 

     // Removing the frame 
     QFrame *frame = transfers.at(which).getFrame(); 
     ui->globalTMLayout->removeWidget(frame); 

     // Removing the data itself 
     transfers.removeAt(which); 
    } 
} 

哪里removeGraphicalUI是这样的功能:

void TransferData::removeGraphicalUI(){ 

    frameLayout->removeWidget(progress); 
    frameLayout->removeWidget(filename); 
    frameLayout->removeWidget(contact_label); 
    statlayout->removeWidget(cancel); 
    statlayout->removeWidget(status); 
    if (!getIsATransfer()){ 
     statlayout->removeWidget(accept); 
     statlayout->removeWidget(reject); 
    } 
} 

什么情况是,框架被删除,但everythign这是在框架内仍然存在。我用打印的消息检查了代码是否能够实现removeUI功能。

那么,为什么这不起作用,什么是删除dinamically生成的用户界面的正确方法?

谢谢!

回答

0

好了,我还没有找到答案,我的问题,但我发现这样做的方式:

基本的文件说,当一个QWidget后裔对象被删除,所有的孩子的被删除。

所以我所做的是基本上使用QFrame作为父母的一切,但布局。

然后,当我想删除所述框架我会简单地调用:

frame->~QFrame() 

在removeGraphicalUI()函数,仅此而已。我也评论过这个:

void TransferManager::removeTransferData(qint32 which){ 
    if (which < transfers.size()){ 

     // Deleting the UI 
     transfers[which].removeGraphicalUI(); 

     // Removing the frame 
     // QFrame *frame = transfers.at(which).getFrame(); 
     // ui->globalTMLayout->removeWidget(frame); 

     // Removing the data itself 
     transfers.removeAt(which); 
    } 
} 

由于不再需要从GUI中删除帧。我希望这可以帮助别人。

+2

实际上删除帧你需要'删除帧;' – nib

+0

这是不是frame->〜QFrame()或者除此之外? – aarelovich

+0

@aarelovich'delete'负责破坏对象。 C++不是Borland Pascal :) –