我有一个QTableWidget,在单击某个单元格时会弹出一个QDialog。关闭QDialog后,QDialog将被删除。当我再次尝试单击该单元时,我的程序崩溃了。 getDaInx()和getDaSMAC()返回QStringLists。它们应该与我所拥有的问题完全无关。这是源代码:
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;
}
}
}
答案 0 :(得分:3)
尝试创建这些:
QPushButton *okBtn = new QPushButton("OK", removeDialog);
QPushButton *cancelBtn = new QPushButton("Cancel", removeDialog);
之后:
removeDialog = new QDialog(this);
答案 1 :(得分:2)
您在初始化之前使用 removeDialog 指针会出现错误:
//...
checkBox->setParent(removeDialog);
//...
QPushButton *okBtn = new QPushButton("OK", removeDialog);
QPushButton *cancelBtn = new QPushButton("Cancel", removeDialog);
//...
removeDialog = new QDialog(this);