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

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 12 Feb 2023, 07:08 last edited by sp3x 2 Dec 2023, 07:08
    #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!

    J 1 Reply Last reply 12 Feb 2023, 08:16
    0
    • C Christian Ehrlicher
      12 Feb 2023, 10:13

      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 12 Feb 2023, 11:10 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")

      C 1 Reply Last reply 12 Feb 2023, 11:11
      0
      • S sp3x
        12 Feb 2023, 07:08

        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!

        J Offline
        J Offline
        JonB
        wrote on 12 Feb 2023, 08:16 last edited by JonB 2 Dec 2023, 08:18
        #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 12 Feb 2023, 08:40
        0
        • J JonB
          12 Feb 2023, 08:16

          @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 12 Feb 2023, 08:40 last edited by sp3x 2 Dec 2023, 08:42
          #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 :(

          C 1 Reply Last reply 12 Feb 2023, 08:42
          0
          • S sp3x
            12 Feb 2023, 08:40

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

            C Offline
            C Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on 12 Feb 2023, 08:42 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 12 Feb 2023, 08:58
            0
            • C Christian Ehrlicher
              12 Feb 2023, 08:42

              Q_DECLARE_METATYPE(Widget::HostsTableWidget::Host)

              hosts.valueHostsTableWidget::HostList()

              I would try the correct full namespace here.

              S Offline
              S Offline
              sp3x
              wrote on 12 Feb 2023, 08:58 last edited by
              #5

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

              C 1 Reply Last reply 12 Feb 2023, 10:13
              0
              • S sp3x
                12 Feb 2023, 08:58

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

                C Offline
                C Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on 12 Feb 2023, 10:13 last edited by Christian Ehrlicher 2 Dec 2023, 10:14
                #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 12 Feb 2023, 11:10
                0
                • C Christian Ehrlicher
                  12 Feb 2023, 10:13

                  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 12 Feb 2023, 11:10 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")

                  C 1 Reply Last reply 12 Feb 2023, 11:11
                  0
                  • S sp3x has marked this topic as solved on 12 Feb 2023, 11:10
                  • S sp3x
                    12 Feb 2023, 11:10

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

                    C Offline
                    C Offline
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on 12 Feb 2023, 11:11 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 11 Mar 2024, 13:43

                    2/8

                    12 Feb 2023, 08:16

                    topic:navigator.unread, 6
                    • Login

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