lundi 2 mars 2015

Qt Segmentation Fault QTableWidget->SetItem(int,int,QTableWidgetItem)


FIXED


I'm writing a program that loads a file with 400 characters, and depending on the character changes the backgroundcolor of a tablecell on load.


This is how I create the QTableWidget:



QTableWidget* mapTableWidget = new QTableWidget(this);
mapTableWidget->setRowCount(20);
mapTableWidget->setColumnCount(20);
for (int i = 0; i<20; i++)
{
mapTableWidget->setRowHeight(i,24);
mapTableWidget->setColumnWidth(i,24);
}
mapTableWidget->setShowGrid(false);
mapTableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
mapTableWidget->setSortingEnabled(false);


I add the widget to the current layout with a simple:



QVBoxLayout* layout = new QVBoxLayout;
layout->setMargin(5);
layout->addWidget(mapTableWidget);


And this is where it goes wrong:



for (int k=0; k<20; k++)
{
for(int l=0; l<20; l++)
{
QTableWidgetItem* tempitem = new QTableWidgetItem();
tempitem->setBackgroundColor(mb->colorAt(l,k));
mapTableWidget->setItem(k,l,tempitem);
}
}


The problem I have is that I keep running into a segmentation fault at mapTableWidget->setItem(k,l,tempitem) when debugging, and the application crashes at that point too.


Does any one have any pointers (:]) to helping me out here?


PS:


This also happens when not using the color part, so commenting the setBackgroundColor line out (I know it's deprecated, but it worked on a different project) doesn't change anything.


FIX


The QTableWidget was allocated locally and was outside the scope where the additem had access to. Defining the table higher was the fix.




Aucun commentaire:

Enregistrer un commentaire