简体   繁体   中英

Application crashes when clearing the QTreeWidget

My application is crashing with a BAD_ACCESS when quitting and when clearing the QTreeWidget.

This is how I'm populating the first level of the tree:

std::set<UrlItem>::iterator i;

for(i = crawler->getUrls()->begin() ; i != crawler->getUrls()->end() ; i++) {
    QList<QString> cells;
    cells.append(i->url);
    cells.append(i->httpStatusMessage);
    cells.append(QString("%1").arg(i->statusCode));

    QTreeWidgetItem *item = new QTreeWidgetItem(ui->resultTreeView, QStringList(cells));

    ui->resultTreeView->addTopLevelItem(item);
}

I believe that the header item is causing the crash:

ui->resultTreeView->setHeaderItem(new QTreeWidgetItem(ui->resultTreeView, QStringList(headers)));

What am I doing to cause this crash? The item that is dynamically allocated has the tree widget as it's parent so it should only be destroyed when the tree widget is.

It seems I was setting the header the wrong way.

This works fine:

QList<QString> headers;
headers.append(tr("Url"));
headers.append(tr("Message"));
headers.append(tr("Status code"));

ui->resultTreeView->setHeaderLabels(QStringList(headers));

Now, what setHeaderItem was supposed to do and why it crashed my application I don't know but the code above achieved the desired effect.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM