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. QWidget & inheritance
Forum Updated to NodeBB v4.3 + New Features

QWidget & inheritance

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 4 Posters 3.8k 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.
  • S Offline
    S Offline
    shahriar25
    wrote on 24 May 2016, 18:30 last edited by
    #1

    Hi
    I have a QWidget (pointer) and I want to change it in runtime to different widgets that inherit QWidget. I want to be able to use the new widget's functions but I can't do it. can anyone help?
    Thanks in advance.

    QWidget *widget;

    widget = new QProgressBar();

    widget->setRange(0,100); //Error (the function not recognized)

    1 Reply Last reply
    0
    • T Offline
      T Offline
      thEClaw
      wrote on 24 May 2016, 18:39 last edited by thEClaw
      #2

      To make this work, you will have to convert the pointer to your widget first, e.g.

      QProgressBar *p = qobject_cast<QProgressBar *>(widget);
      if(p)
        p->setRange(...);
      

      If you want to do something similar with other types of widgets (QLabel, QSlider, ...), you would have to convert the pointer every time you want to call a function that is not virtual in QWidget itself. I.e.

      QWidget *widget;
      widget = new QLabel();
      
      widget->sizeHint();//works, since QWidgets implementation has been overridden in QLabel
      widget->setText("...");//does *not* work since you are calling this method on a QWidget!
      QLabel *l = qobject_cast<QLabel *>(widget);
      if(l)
        l->setText("...");//works, since you are explicitely calling QLabels implementation of the method
      
      1 Reply Last reply
      1
      • S Offline
        S Offline
        shahriar25
        wrote on 25 May 2016, 04:53 last edited by
        #3

        Hi @thEClaw
        what if I want to delete the widget that is a QProgressbar after a while? if I delete the widget, the new pointer that is a QProgressBar is deleted too?

        1 Reply Last reply
        0
        • J Offline
          J Offline
          jsulm
          Lifetime Qt Champion
          wrote on 25 May 2016, 05:05 last edited by
          #4

          If you delete the object your pointer is pointing to then the object will be deleted even if it is QProgressBar:

          QWidget *widget;
          widget = new QProgressBar();
          delete widget; // Will delete QProgressBar
          

          As far as I know Qt uses virtual destructors for base classes, so even if the pointer is of base class type the correct destructor will be called.

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

          K 1 Reply Last reply 25 May 2016, 18:13
          0
          • J jsulm
            25 May 2016, 05:05

            If you delete the object your pointer is pointing to then the object will be deleted even if it is QProgressBar:

            QWidget *widget;
            widget = new QProgressBar();
            delete widget; // Will delete QProgressBar
            

            As far as I know Qt uses virtual destructors for base classes, so even if the pointer is of base class type the correct destructor will be called.

            K Offline
            K Offline
            kshegunov
            Moderators
            wrote on 25 May 2016, 18:13 last edited by
            #5

            @jsulm said:

            As far as I know Qt uses virtual destructors for base classes

            Yes. QObject's destructor is virtual. Classes that derive from other bases might not have a virtual destructor, though (e.g. QStringList, not that you would want to create that in the heap).

            @shahriar25

            I have a QWidget (pointer) and I want to change it in runtime to different widgets that inherit QWidget. I want to be able to use the new widget's functions but I can't do it. can anyone help?

            You can, as @thEClaw has show, however the rationale behind that somewhat escapes me. Why do you want to do that?

            Read and abide by the Qt Code of Conduct

            1 Reply Last reply
            0
            • S Offline
              S Offline
              shahriar25
              wrote on 26 May 2016, 08:20 last edited by
              #6

              Hi @kshegunov
              I want the user to enter the widget's name an the program to show that widget and change it by entering sth.

              K 1 Reply Last reply 26 May 2016, 09:42
              0
              • S shahriar25
                26 May 2016, 08:20

                Hi @kshegunov
                I want the user to enter the widget's name an the program to show that widget and change it by entering sth.

                K Offline
                K Offline
                kshegunov
                Moderators
                wrote on 26 May 2016, 09:42 last edited by kshegunov
                #7

                @shahriar25
                This is a very unusual requirement. I doubt qobject_cast will help you for this, though. You probably want to create objects by class name, which is possible through the meta-object system, however it's a bit hackish. Something along the lines of:

                QWidget * parent; // The parent of the widget we are creating
                
                QByteArray className = QByteArrayLiteral("QDialog*");
                int typeId = QMetaType::type(className);
                if (typeId == 0)
                    ; // Error - class is not registered
                
                //< Here is needed an additional check if that class actually derives from QObject
                
                const QMetaObject * metaObject = QMetaType::metaObjectForType(typeId);
                QObject * widget = metaObject->newInstance(Q_ARG(QWidget *, parent));
                

                But why do you need to do that in the first place?

                Read and abide by the Qt Code of Conduct

                1 Reply Last reply
                1
                • S Offline
                  S Offline
                  shahriar25
                  wrote on 26 May 2016, 12:01 last edited by
                  #8

                  @kshegunov
                  wow your code really creates an object by it's name. this is an interesting thing. but this is not what I want. my program get predefined commands from user or an external program and does something specific. the commands are limited and I want to create about 10 widgets one at a time. so thEClaw's answer is enough here. thank you all

                  K 1 Reply Last reply 27 May 2016, 00:37
                  0
                  • S shahriar25
                    26 May 2016, 12:01

                    @kshegunov
                    wow your code really creates an object by it's name. this is an interesting thing. but this is not what I want. my program get predefined commands from user or an external program and does something specific. the commands are limited and I want to create about 10 widgets one at a time. so thEClaw's answer is enough here. thank you all

                    K Offline
                    K Offline
                    kshegunov
                    Moderators
                    wrote on 27 May 2016, 00:37 last edited by
                    #9

                    @shahriar25 said:

                    wow your code really creates an object by it's name.

                    It should, but I can't claim merit for it. I think I read about that approach some time ago in the mailing list.

                    but this is not what I want. my program get predefined commands from user or an external program and does something specific.

                    I see. Then I must have misunderstood you. But in any case, I'm glad it worked out in the end.

                    Read and abide by the Qt Code of Conduct

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      shahriar25
                      wrote on 27 May 2016, 16:57 last edited by
                      #10

                      Yes of course but only with your help this becomes possible

                      1 Reply Last reply
                      0

                      5/10

                      25 May 2016, 18:13

                      • Login

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