PDA

View Full Version : Crash caused by QVariant (mis)use



mclark
30th October 2006, 15:11
I am using Qt 4.1: In a QTableWidget I am storing a void* pointer as the data for a QTableWidgetItem. As the application is filling the rows (driven from an outside source) I may get a crash resulting from:

QVariant::compare: type 128 unknown to QVariant. In release mode the program just goes away.

I am not sure what 'type 128' means with respect to QVariant. Every place I use QVariant with the void*, I use QVariant::UserType ( == 127 ).

Using the same input, this does not happen every time nor in the same place (possibly a timing thing.) The code driving the input is mature and well tested by other applications so it's clear the error is in my stuff.

I also get dozens of "QFont: It is not safe to use text and fonts outside the gui thread" debug text, but nowhere in the code is QFont used.


// All data is set using Qt::UserRole
Variant v( QVariant::UserType );
v.setValue( pData ); // pData is a void*
item( nRow, nCol )->setData( Qt::UserRole, v );

// All data is retrieved using Qt::UserRole
void* pData = item( nRow, nCol )->data( Qt::UserRole ).value<void*>();

1) Can someone explain the error: QVariant::compare: type 128 unknown to QVariant?

2) What can cause the QFont message and could this have something to do with the QVariant problem?

jpn
31st October 2006, 08:17
1) Can someone explain the error: QVariant::compare: type 128 unknown to QVariant?

Does passing the void* to QVariant::fromValue() give you any better results?

QVariant v = QVariant::fromValue(pData);
It should basically handle a correct type (VoidStar) for you.



2) What can cause the QFont message and could this have something to do with the QVariant problem?
Are you using multiple threads in your application? That warning message comes when an instance of QFont is attempted to construct from another thread than the main GUI thread.

From qfont.cpp:

void qt_font_tread_test()
{
if (QApplication::instance() && QThread::currentThread() != QApplication::instance()->thread())
qWarning("QFont: It is not safe to use text and fonts outside the gui thread");
}

mclark
31st October 2006, 15:05
Thanks for your reply.

After looking over my code it was revealed that I was setting table values from outside the GUI thread. I'm in the process of correcting that now.

I shall try your QVariant suggestion when the thread issues are fixed.