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.ap开发者_运维百科pend(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.
精彩评论