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. Interconversion of Qt types
Forum Updated to NodeBB v4.3 + New Features

Interconversion of Qt types

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 4 Posters 514 Views 1 Watching
  • 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.
  • Swati777999S Offline
    Swati777999S Offline
    Swati777999
    wrote on last edited by
    #1

    Hi All,

    Just want to if it is possible to convert a variable of QVariant type to QWidget type.

    “ In order to be irreplaceable, one must always be different” – Coco Chanel

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      If you hold a pointer to QWidget in your variant, then yes. Otherwise - no.

      (Z(:^

      Swati777999S 1 Reply Last reply
      1
      • sierdzioS sierdzio

        If you hold a pointer to QWidget in your variant, then yes. Otherwise - no.

        Swati777999S Offline
        Swati777999S Offline
        Swati777999
        wrote on last edited by
        #3

        @sierdzio Can you give an example of it?

        “ In order to be irreplaceable, one must always be different” – Coco Chanel

        jsulmJ 1 Reply Last reply
        0
        • Swati777999S Swati777999

          @sierdzio Can you give an example of it?

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Swati777999 There are examples in the documentation (https://doc.qt.io/qt-6/qvariant.html and https://doc.qt.io/qt-6/qvariant.html#value), like:

          QVariant variant;
          ...
          QColor color = variant.value<QColor>();
          

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          JonBJ 1 Reply Last reply
          1
          • jsulmJ jsulm

            @Swati777999 There are examples in the documentation (https://doc.qt.io/qt-6/qvariant.html and https://doc.qt.io/qt-6/qvariant.html#value), like:

            QVariant variant;
            ...
            QColor color = variant.value<QColor>();
            
            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #5

            @jsulm
            QVariant is not a QWidget!? :)

            Swati777999S 1 Reply Last reply
            0
            • JonBJ JonB

              @jsulm
              QVariant is not a QWidget!? :)

              Swati777999S Offline
              Swati777999S Offline
              Swati777999
              wrote on last edited by
              #6

              @JonB

              QVariant *l = new QVariant;
                         QWidget *window = new QWidget;
                         QHBoxLayout *lay = new QHBoxLayout(window);
                          lay->addWidget(l,0,0);
                          lay->addWidget(img,0,1);
              

              What's wrong in the above lines , in terms of conversion?

              “ In order to be irreplaceable, one must always be different” – Coco Chanel

              JonBJ 1 Reply Last reply
              0
              • sierdzioS Offline
                sierdzioS Offline
                sierdzio
                Moderators
                wrote on last edited by
                #7

                addWidget() accepts a QWidget, not QVariant. These are 2 completely unrelated classes. If your l variant is holding a QWidget, then you can extract it like @jsulm mentioned:

                l = QVariant::fromValue(window);
                lay->addWidget(l.value<QWidget*>(),0,0);
                

                (Z(:^

                1 Reply Last reply
                4
                • Swati777999S Swati777999

                  @JonB

                  QVariant *l = new QVariant;
                             QWidget *window = new QWidget;
                             QHBoxLayout *lay = new QHBoxLayout(window);
                              lay->addWidget(l,0,0);
                              lay->addWidget(img,0,1);
                  

                  What's wrong in the above lines , in terms of conversion?

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

                  @Swati777999

                  • l points to an invalid variant.
                  • addWidget(QWidget *) will not accept a QVariant *.
                  1 Reply Last reply
                  3

                  • Login

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