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生成的用戶界面的正確方法?
謝謝!
實際上刪除幀你需要'刪除幀;' – nib
這是不是frame->〜QFrame()或者除此之外? – aarelovich
@aarelovich'delete'負責破壞對象。 C++不是Borland Pascal :) –