QVariant: unknown user type
- 
Hello, I want to store data by QSettings. Converting and storing the data with QSettings is possible. But if I want to load the data from QSettings I get the error: "QVariant::load: unknown user type with name HostList."I've already seen, that I have to register the type with Q_DECLARE_METATYPE. 
 But it's still not working.That's my code to load the data with QSettings: void MainForm::loadSettings() { QSettings settings; QVariant hosts = settings.value("hosts"); tblHosts->setHosts(hosts.value<HostsTableWidget::HostList>()); // Thats where the error occures }And thats the class where the types are defined: namespace Widget { class HostsTableWidget : public QTableWidget { Q_OBJECT public: typedef QPair<QString, quint16> Host; typedef QList<Host> HostList; HostsTableWidget(QWidget *parent = nullptr); void addHost(QString hostname = "example.com", quint16 port = 80); void removeCurrentHost(); HostList hosts(); void setHosts(HostList hosts); signals: void hostsChanged(); }; } // namespace Widget Q_DECLARE_METATYPE(Widget::HostsTableWidget::Host) Q_DECLARE_METATYPE(Widget::HostsTableWidget::HostList)Thank you! 
- 
This works fine for me without any errors with Qt6.6, looks like qRegisterMetaType<>() is needed even though I get the correct values (but a runtime warning). namespace Widget { class HostsTableWidget { public: typedef QPair<QString, quint16> Host; typedef QList<Host> HostList; }; } Q_DECLARE_METATYPE(Widget::HostsTableWidget::Host) Q_DECLARE_METATYPE(Widget::HostsTableWidget::HostList) int main(int argc, char* argv[]) { QCoreApplication a(argc, argv); qRegisterMetaType<Widget::HostsTableWidget::HostList>("Widget::HostsTableWidget::HostList"); Widget::HostsTableWidget::Host h1{ "host1", 4711 }; Widget::HostsTableWidget::Host h2{ "host2", 4712 }; Widget::HostsTableWidget::HostList hlIn = { h1, h2 }; QSettings s("D:\\temp.ini", QSettings::IniFormat); QVariant v = QVariant::fromValue(hlIn); auto hlOut1 = v.value<Widget::HostsTableWidget::HostList>(); s.setValue("hostlist", v); s.sync(); v = s.value("hostlist"); auto hlOut2 = v.value<Widget::HostsTableWidget::HostList>(); qDebug() << hlIn.size() << hlOut1.size() << hlOut2.size(); return 0; }@Christian-Ehrlicher said in QVariant: unknown user type: qRegisterMetaType 
 Thank you very much. That's working. I've already played around with qRegisterMetaType but I missed to pass the right argument to the function ("Widget::HostsTableWidget::HostList")
- 
Hello, I want to store data by QSettings. Converting and storing the data with QSettings is possible. But if I want to load the data from QSettings I get the error: "QVariant::load: unknown user type with name HostList."I've already seen, that I have to register the type with Q_DECLARE_METATYPE. 
 But it's still not working.That's my code to load the data with QSettings: void MainForm::loadSettings() { QSettings settings; QVariant hosts = settings.value("hosts"); tblHosts->setHosts(hosts.value<HostsTableWidget::HostList>()); // Thats where the error occures }And thats the class where the types are defined: namespace Widget { class HostsTableWidget : public QTableWidget { Q_OBJECT public: typedef QPair<QString, quint16> Host; typedef QList<Host> HostList; HostsTableWidget(QWidget *parent = nullptr); void addHost(QString hostname = "example.com", quint16 port = 80); void removeCurrentHost(); HostList hosts(); void setHosts(HostList hosts); signals: void hostsChanged(); }; } // namespace Widget Q_DECLARE_METATYPE(Widget::HostsTableWidget::Host) Q_DECLARE_METATYPE(Widget::HostsTableWidget::HostList)Thank you! @sp3x 
 What version of Qt are you using, is it Qt 6.x? Recently @Christian-Ehrlicher fielded a similar question and found a bug/regression in Qt 6.x handling of enums withQSettingswhich I think was just like this. I will try to locate it.... Ah, see https://forum.qt.io/topic/142601/wrong-enum-value-from-qsettings-using-qt- and https://bugreports.qt.io/browse/QTBUG-109744. Is this your issue too? There is an accepted solution/workaround in that thread.
- 
@sp3x 
 What version of Qt are you using, is it Qt 6.x? Recently @Christian-Ehrlicher fielded a similar question and found a bug/regression in Qt 6.x handling of enums withQSettingswhich I think was just like this. I will try to locate it.... Ah, see https://forum.qt.io/topic/142601/wrong-enum-value-from-qsettings-using-qt- and https://bugreports.qt.io/browse/QTBUG-109744. Is this your issue too? There is an accepted solution/workaround in that thread.
- 
@JonB Hi, thank you. Sorry, yes of course I'am using Qt6.4.2. I will take a look at it an reply later. //EDIT: No i think it's not the same issue :( Q_DECLARE_METATYPE(Widget::HostsTableWidget::Host) hosts.valueHostsTableWidget::HostList() I would try the correct full namespace here. 
- 
Q_DECLARE_METATYPE(Widget::HostsTableWidget::Host) hosts.valueHostsTableWidget::HostList() I would try the correct full namespace here. @Christian-Ehrlicher Thank you. I've already tried it with the full namespace, but without success. 
- 
@Christian-Ehrlicher Thank you. I've already tried it with the full namespace, but without success. This works fine for me without any errors with Qt6.6, looks like qRegisterMetaType<>() is needed even though I get the correct values (but a runtime warning). namespace Widget { class HostsTableWidget { public: typedef QPair<QString, quint16> Host; typedef QList<Host> HostList; }; } Q_DECLARE_METATYPE(Widget::HostsTableWidget::Host) Q_DECLARE_METATYPE(Widget::HostsTableWidget::HostList) int main(int argc, char* argv[]) { QCoreApplication a(argc, argv); qRegisterMetaType<Widget::HostsTableWidget::HostList>("Widget::HostsTableWidget::HostList"); Widget::HostsTableWidget::Host h1{ "host1", 4711 }; Widget::HostsTableWidget::Host h2{ "host2", 4712 }; Widget::HostsTableWidget::HostList hlIn = { h1, h2 }; QSettings s("D:\\temp.ini", QSettings::IniFormat); QVariant v = QVariant::fromValue(hlIn); auto hlOut1 = v.value<Widget::HostsTableWidget::HostList>(); s.setValue("hostlist", v); s.sync(); v = s.value("hostlist"); auto hlOut2 = v.value<Widget::HostsTableWidget::HostList>(); qDebug() << hlIn.size() << hlOut1.size() << hlOut2.size(); return 0; }
- 
This works fine for me without any errors with Qt6.6, looks like qRegisterMetaType<>() is needed even though I get the correct values (but a runtime warning). namespace Widget { class HostsTableWidget { public: typedef QPair<QString, quint16> Host; typedef QList<Host> HostList; }; } Q_DECLARE_METATYPE(Widget::HostsTableWidget::Host) Q_DECLARE_METATYPE(Widget::HostsTableWidget::HostList) int main(int argc, char* argv[]) { QCoreApplication a(argc, argv); qRegisterMetaType<Widget::HostsTableWidget::HostList>("Widget::HostsTableWidget::HostList"); Widget::HostsTableWidget::Host h1{ "host1", 4711 }; Widget::HostsTableWidget::Host h2{ "host2", 4712 }; Widget::HostsTableWidget::HostList hlIn = { h1, h2 }; QSettings s("D:\\temp.ini", QSettings::IniFormat); QVariant v = QVariant::fromValue(hlIn); auto hlOut1 = v.value<Widget::HostsTableWidget::HostList>(); s.setValue("hostlist", v); s.sync(); v = s.value("hostlist"); auto hlOut2 = v.value<Widget::HostsTableWidget::HostList>(); qDebug() << hlIn.size() << hlOut1.size() << hlOut2.size(); return 0; }@Christian-Ehrlicher said in QVariant: unknown user type: qRegisterMetaType 
 Thank you very much. That's working. I've already played around with qRegisterMetaType but I missed to pass the right argument to the function ("Widget::HostsTableWidget::HostList")
- 
S sp3x has marked this topic as solved on
- 
@Christian-Ehrlicher said in QVariant: unknown user type: qRegisterMetaType 
 Thank you very much. That's working. I've already played around with qRegisterMetaType but I missed to pass the right argument to the function ("Widget::HostsTableWidget::HostList")@sp3x said in QVariant: unknown user type: @Christian-Ehrlicher said in QVariant: unknown user type: qRegisterMetaType 
 Thank you very much. That's working. I've already played around with qRegisterMetaType but I missed to pass the right argument to the function ("Widget::HostsTableWidget::HostList")Yes, it should/must be the fully qualified name. 
- 
R Ronel_qtmaster referenced this topic on 
 

