Frage

I have a QTableWidget that brings up a QDialog when a certain cell is clicked. After closing the QDialog, the QDialog gets deleted. When I try clicking the cell again my program is crashing. getDaInx() and getDaSMAC() return QStringLists. They should be entirely irrelevant to the issue that I'm having. Here is the source code:

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;
        }
    }
}
War es hilfreich?

Lösung 2

You get errors because you use removeDialog pointer before you initialize it:

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

Andere Tipps

Try creating these:

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

After this:

    removeDialog = new QDialog(this);
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top