2013-11-20 117 views
1

我有一个QTableWidget,当某个单元格被点击时调出一个QDialog。关闭QDialog后,QDialog被删除。当我尝试再次单击单元格时,我的程序崩溃。 getDaInx()和getDaSMAC()返回QStringLists。它们应该与我所遇到的问题完全无关。这里是源代码:Qt对象删除导致崩溃

QDialog *removeDialog; 

// connect in MainWindow constructor 
connect(ui->theTable, SIGNAL(cellClicked(int,int)), this, SLOT(handleCellClick(int,int))); 

void MainWindow::handleCellClick(int row, int col) 
{ 
    if (col == 9) 
    { 
     if (row > 0) 
     { 
      QGridLayout *removeLayout = new QGridLayout(); 

      for (int x = 1; x < getDaInx().length(); x++) 
      { 
       if (getDaInx().length() != getDaSMAC().length()) break; 

       QString device = getDaSMAC()[x]; 
       QString inx = getDaInx()[x]; 

       QCheckBox *checkBox = new QCheckBox(QString("Remove %1 %2").arg(inx).arg(device)); 
       if (x == row) checkBox->setChecked(true); 
       checkBox->setParent(removeDialog); 

       removeLayout->addWidget(checkBox, x, 0); 
      } 

      QPushButton *okBtn = new QPushButton("OK", removeDialog); 
      QPushButton *cancelBtn = new QPushButton("Cancel", removeDialog); 

      connect(okBtn, SIGNAL(clicked()), this, SLOT(handleRemoveDialogOk())); 
      connect(cancelBtn, SIGNAL(clicked()), this, SLOT(handleRemoveDialogCancel())); 

      int rowCount = removeLayout->rowCount(); 

      removeLayout->addWidget(okBtn, rowCount, 0); 
      removeLayout->addWidget(cancelBtn, rowCount, 1); 

      removeDialog = new QDialog(this); 

      removeDialog->setLayout(removeLayout); 

      removeDialog->exec(); 

      disconnect(okBtn, SIGNAL(clicked()), this, SLOT(handleRemoveDialogOk())); 
      disconnect(cancelBtn, SIGNAL(clicked()), this, SLOT(handleRemoveDialogCancel())); 

      delete removeDialog; 
     } 
    } 
} 
+1

你在调试器中运行过吗?为什么不是removeDialog一个局部变量? –

+0

removeDialog不是局部变量,因为它在MainWindow :: handleRemoveDialogOk()中被使用。 –

+1

调试器说什么?崩溃报告/堆栈跟踪? – Till

回答

2

因为你用removeDialog指针你得到错误,你初始化之前:

//... 
checkBox->setParent(removeDialog); 
//... 
QPushButton *okBtn = new QPushButton("OK", removeDialog); 
QPushButton *cancelBtn = new QPushButton("Cancel", removeDialog); 
//... 
removeDialog = new QDialog(this); 
+0

哈耶,我刚刚看到。不知道为什么我以前没有看到。 –

3

尝试创建这些:

QPushButton *okBtn = new QPushButton("OK", removeDialog); 
    QPushButton *cancelBtn = new QPushButton("Cancel", removeDialog); 

在此之后:

removeDialog = new QDialog(this); 
+3

此代码处于相同的情况:_checkBox-> setParent(removeDialog); _ – Zlatomir