删除动态创建的QHBoxLayout和QCheckBox

问题描述

我想知道在我创建的包含复选框的表中删除一组指针向量的正确方法

enter image description here

在定义类中,我将变量声明如下:

   class tableFramer : public QWidget
   {
    Q_OBJECT
    public : 
       explicit tableFramer(QWidget *parent = nullptr);
       ~tableFramer();

   private: 

    QVector<QHBoxLayout*> hbLayoutPtrVector;
    QVector<QWidget*> wdgPtrVector;
    QVector<QCheckBox*> cbAcceptPtrVector;
    QVector<QCheckBox*> cbRemovePtrVector;
}

在实现中,我按如下方式创建它们:

void tableFramer::makeForm()
{
:

    ui->tableWidget->clear();

    int j  = 0;
    ui->tableWidget->setRowCount(this->rows);
    ui->tableWidget->setColumnCount(this->cols);
    ui->tableWidget->verticalHeader()->setVisible(false);
    ui->tableWidget->horizontalHeader()->setVisible(false);


    for(int i = 0; i < this->rows; i++)
    {
        QHBoxLayout* hbLayout(new QHBoxLayout());
        QWidget* wdg(new QWidget());
        QCheckBox* applyCb(new QCheckBox(this));
        QCheckBox* removeCb(new QCheckBox(this));

        hbLayoutPtrVector.push_back(hbLayout);
        wdgPtrVector.push_back(wdg);
        cbAcceptPtrVector.push_back(applyCb);
        cbRemovePtrVector.push_back(removeCb);
    }


    for(auto itr = hbLayoutPtrVector.begin() ; itr <= hbLayoutPtrVector.end() && j < hbLayoutPtrVector.length(); itr++,j++ )
    {
        cbAcceptPtrVector.at(j)->setText("Opt 1");cbAcceptPtrVector.at(j)->setProperty("row",j);cbAcceptPtrVector.at(j)->setProperty("column",1);
        cbRemovePtrVector.at(j)->setText("Opt 2");cbRemovePtrVector.at(j)->setProperty("row",j);cbRemovePtrVector.at(j)->setProperty("column",1);
        (*itr)->addWidget(cbAcceptPtrVector.at(j));
        (*itr)->addWidget(cbRemovePtrVector.at(j));
        (*itr)->setAlignment(Qt::AlignCenter);
        wdgPtrVector.at(j)->setLayout(*itr);
        ui->tableWidget->setCellWidget(j,1,wdgPtrVector.at(j));
        ui->tableWidget->verticalHeader()->setSectionResizeMode(QHeaderView::Fixed);
        ui->tableWidget->verticalHeader()->setDefaultSectionSize(40);
        ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Fixed);
        ui->tableWidget->horizontalHeader()->setDefaultSectionSize(200);
        ui->tableWidget->setItem(j,new QTableWidgetItem(this->subfolders.at(j)));

        connect(cbAcceptPtrVector.at(j),SIGNAL(toggled(bool)),this,SLOT(checkBoxAcceptToggeled(bool)));
        connect(cbRemovePtrVector.at(j),SLOT(checkBoxRejectToggeled(bool)));
    }

:
}

要删除动态创建的项目,请在析构函数中执行以下操作,但我认为这样做不正确:

tableFramer::~tableFramer
{
:
if ( rows != 0)
    {
        for(int i = 0; i < rows ; ++i)
        {
            
            if(hbLayoutPtrVector[i]) 
                delete hbLayoutPtrVector[i];
            if(wdgPtrVector[i]) 
                delete wdgPtrVector[i];
            if(cbAcceptPtrVector[i]) 
                delete cbAcceptPtrVector[i];
            if(cbRemovePtrVector[i]) 
                delete cbRemovePtrVector[i];
            
            for(int j=0; j < 3; ++j)  // there are 3 columns in total
            {
                delete ui->tableWidget->item(i,j);
            }
        }
        hbLayoutPtrVector.clear();
        wdgPtrVector.clear();
        cbAcceptPtrVector.clear();
        cbRemovePtrVector.clear();
    }
    rows = 0;
:
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...