2016-06-13 5 views
0

私はダイアログのファイル転送マネージャーに、いくつかのUI要素を動的に生成する必要があります。ここではそれを生成する関数は次のとおりです。QTでダイナミックに生成されたUIを削除しますか?

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オブジェクトのリストを持っている関数から呼び出されます。転送は、私はすべての要素を削除する必要が完了すると

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; 

} 

作成されたUI。 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); 
    } 
} 

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生成されたUIを削除する適切な方法は何ですか?

ありがとうございます!

答えて

0

[OK]を、ので、私は私の質問への答えを見つけることができますが、私はこれを行う方法を見つけました:

基本的にドキュメントはQWidgetの子孫オブジェクトが削除されたときに、すべての彼らのチャイルズが削除されていることを述べています。

だから、私がやったことは、基本的にすべてのレイアウトのQFrameを親として使用したことです。

その後、私は削除したいときには、フレームを言った私は単純に呼び出します:removeGraphicalUI()関数と、よりNOTHINGで

frame->~QFrame() 

を。また、私はこれをコメントしました:

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

フレームを削除するには 'delete frame; 'が必要です – nib

+0

これはframe->〜QFrame()の代わりに、またはこれ以外にもありますか? – aarelovich

+0

@arelovich 'delete'はオブジェクトの破壊を行います。 C++はBorland Pascalではありません:) –

関連する問題