2013-04-04 76 views
1

根据标题,如何删除多个QIntValidator指针本地创建的对象。我坚持内存泄漏的问题。Qt:如何删除在QlineEdit上实现的本地创建的多个QIntValidator指针对象?

我有如下的功能:

void ABC::setTableDataItems(QStringList headerData) 
{ 
    int row = headerData.size(); 

    int column = 0; 

    if (_txtLineEdit != NULL) { 
      delete _txtLineEdit; 
      _txtLineEdit = 0; 
    } 


for (int i = 0; i < row ; i++) 
{ 
    _txtLineEdit = new QLineEdit(); 
    _txtLineEdit->setMaxLength(_pktStruct[i].ItemDataLength); 
    _txtLineEdit->setText(headerData.at(i)); 
    _pktStruct[i].currentLine = _txtLineEdit; 

    QString regExp = "[01]{1,"; 
    regExp.append(QString("%1").arg(_pktStruct[i].ItemDataLength)).append("}"); 
    long maxDigit = getMaxValueForDigit(_pktStruct[i].ItemDataLength); 

    QIntValidator* decValidator = new QIntValidator(0, maxDigit, _txtLineEdit  ); 
    QRegExpValidator* binValidator = new QRegExpValidator(QRegExp(regExp),_txtLineEdit); 


    switch (_pktStruct[i].ItemDataType) 
    { 
    case DATA_TYPE_ASCII: 
     break; 

    case DATA_TYPE_HEX: 
     break; 

    case DATA_TYPE_NUM: 
     _txtLineEdit->setValidator(decValidator); 
     break; 

    case DATA_TYPE_BINARY: 
     _txtLineEdit->setValidator(binValidator); 
     break; 

    case DATA_TYPE_MAX: 
     break; 
    } 

    ui->pcusim_cmd_task_tableWidget->setCellWidget(i, column, _txtLineEdit); 
    connect(_txtLineEdit, SIGNAL(textChanged(QString)), this, SLOT(on_pcusim_cmd_task_tableWidget_linedit_cellChanged(QString))); 
} 

} 

在上述功能,我需要在循环之前删除动态创建的(内部For循环)中的所有多个QIntValidator每当上述函数被调用。

不知道路。请给出一些建议/想法继续进行下去?

在此先感谢

回答

1

你为什么不这样做

QValidator* pValidator = NULL; 
    switch (_pktStruct[i].ItemDataType) 
    { 
    case DATA_TYPE_NUM: 
    // create the proper int validator here   
    // ... 
    pValidator = new QIntValidator(0, maxDigit, _txtLineEdit); 
    break; 

    case DATA_TYPE_BINARY: 
    // create the proper regexp validator here 
    // ... 
    pValidator = new QRegExpValidator(QRegExp(regExp),_txtLineEdit);   
    break; 
    } 

    _txtLineEdit->setValidator(pValidator); 

这样,你不要创建不使用验证。
由于您将_txtLineEdit作为构建验证器的父项来传递,因此当它们的父QLineEdit对象被销毁时,它们将被删除。
顺便说一句,setCellWidget()接管小部件的所有权,所以你不需要delete _txtLineEdit;(假设这是你传递给它的那个,你应该在这种情况下创建它们的列表)