做了一些挖掘Qt的來源。
在qvariant.cpp
唯一的地方,在那裏unable to save type
錯誤調用是在這裏:
if (!QMetaType::save(s, d.type, constData())) {
Q_ASSERT_X(false, "QVariant::save", "Invalid type to save");
qWarning("QVariant::save: unable to save type %d.", d.type);
}
所以我去QMetaType::save
:
bool QMetaType::save(QDataStream &stream, int type, const void *data)
{
...
case QMetaType::QPalette:
case QMetaType::QIcon:
case QMetaType::QImage:
...
if (!qMetaTypeGuiHelper)
return false;
qMetaTypeGuiHelper[type - FirstGuiType].saveOp(stream, data);
break;
...
return true;
}
qMetaTypeGuiHelper
聲明如下:
Q_CORE_EXPORT const QMetaTypeGuiHelper *qMetaTypeGuiHelper = 0;
顯然,在你的情況下qMetaTypeGuiHelper
等於零。所以我決定找到它創建的地方。而在QtGui
模塊發現:
static const QVariant::Handler *qt_guivariant_last_handler = 0;
int qRegisterGuiVariant()
{
qt_guivariant_last_handler = QVariant::handler;
QVariant::handler = &qt_gui_variant_handler;
qMetaTypeGuiHelper = qVariantGuiHelper;
return 1;
}
Q_CONSTRUCTOR_FUNCTION(qRegisterGuiVariant)
int qUnregisterGuiVariant()
{
QVariant::handler = qt_guivariant_last_handler;
qMetaTypeGuiHelper = 0;
return 1;
}
Q_DESTRUCTOR_FUNCTION(qUnregisterGuiVariant)
這意味着,爲了節省QIcon
到QVariant
你只需要調用qRegisterGuiVariant();
。但是,這個功能已經從QApplication::QApplication(int &argc, char **argv)
稱爲QApplicationPrivate::initialize()
,其中,從QApplicationPrivate::construct
調用,這被稱爲(噢,多麼長的名單......)所以我要問,在你main
功能,你創造QApplication
實例?
P.S:這很有趣:)
您可以將圖標轉換爲像素圖,並調用保存()上,將其存儲在磁盤上。問題是,你爲什麼要這樣做?該圖標已存儲在系統中的某個位置,這就是它所屬的位置。 –