Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QVariant: unknown user type
QtWS25 Last Chance

QVariant: unknown user type

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 3 Posters 1.7k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sp3x
    wrote on last edited by sp3x
    #1

    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!

    JonBJ 1 Reply Last reply
    0
    • Christian EhrlicherC Christian Ehrlicher

      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;
      }
      
      S Offline
      S Offline
      sp3x
      wrote on last edited by
      #7

      @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")

      Christian EhrlicherC 1 Reply Last reply
      0
      • S sp3x

        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!

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #2

        @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 with QSettings which 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.

        S 1 Reply Last reply
        0
        • JonBJ JonB

          @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 with QSettings which 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.

          S Offline
          S Offline
          sp3x
          wrote on last edited by sp3x
          #3

          @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 :(

          Christian EhrlicherC 1 Reply Last reply
          0
          • S sp3x

            @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 :(

            Christian EhrlicherC Online
            Christian EhrlicherC Online
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #4

            Q_DECLARE_METATYPE(Widget::HostsTableWidget::Host)

            hosts.valueHostsTableWidget::HostList()

            I would try the correct full namespace here.

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            S 1 Reply Last reply
            0
            • Christian EhrlicherC Christian Ehrlicher

              Q_DECLARE_METATYPE(Widget::HostsTableWidget::Host)

              hosts.valueHostsTableWidget::HostList()

              I would try the correct full namespace here.

              S Offline
              S Offline
              sp3x
              wrote on last edited by
              #5

              @Christian-Ehrlicher Thank you. I've already tried it with the full namespace, but without success.

              Christian EhrlicherC 1 Reply Last reply
              0
              • S sp3x

                @Christian-Ehrlicher Thank you. I've already tried it with the full namespace, but without success.

                Christian EhrlicherC Online
                Christian EhrlicherC Online
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by Christian Ehrlicher
                #6

                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;
                }
                

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                S 1 Reply Last reply
                0
                • Christian EhrlicherC Christian Ehrlicher

                  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;
                  }
                  
                  S Offline
                  S Offline
                  sp3x
                  wrote on last edited by
                  #7

                  @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")

                  Christian EhrlicherC 1 Reply Last reply
                  0
                  • S sp3x has marked this topic as solved on
                  • S sp3x

                    @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")

                    Christian EhrlicherC Online
                    Christian EhrlicherC Online
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on last edited by
                    #8

                    @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.

                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                    Visit the Qt Academy at https://academy.qt.io/catalog

                    1 Reply Last reply
                    0
                    • Ronel_qtmasterR Ronel_qtmaster referenced this topic on

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved