Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct


    Qt World Summit: Early-Bird Tickets

    [SOLVED]Best coding practice

    General and Desktop
    4
    8
    2031
    Loading More Posts
    • 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.
    • K
      Keith Hedger last edited by

      I am reasonably new to qt and c++ but am quite an experienced programmer.

      My ( sort of ) problem is this I have been used to casting widgets ( gtk ) from the base widget to the specific via this sort of code:
      @
      combobox=(GtkCombBox*)widget
      @

      But I have come across this sort of thing in the various QT docs:
      @
      QPushButton *button = parentWidget->findChild<QPushButton *>("button1");
      @

      What is the better/best way to cast widgets in QT?

      The large print givith and the small print taketh away. - Tom Waites.

      1 Reply Last reply Reply Quote 0
      • Zlatomir
        Zlatomir last edited by

        To perform that type off casts in C++ you usually use "dynamic_cast":http://en.cppreference.com/w/cpp/language/dynamic_cast , but with QObject subclasses (all widgets are derived indirectly from QObject) the recommended way is "qobject_cast ":http://qt-project.org/doc/qt-4.8/qobject.html#qobject_cast

        LE: findChild is a way to get a child by name (if you have the parent), is not a casting method by itself, see the documentation "here":http://qt-project.org/doc/qt-4.8/qobject.html#findChild

        https://forum.qt.io/category/41/romanian

        1 Reply Last reply Reply Quote 0
        • K
          Keith Hedger last edited by

          [quote author="Zlatomir" date="1420548624"]...
          LE: findChild is a way to get a child by name (if you have the parent), is not a casting method by itself, see the documentation "here":http://qt-project.org/doc/qt-4.8/qobject.html#findChild[/quote]

          Thanks for the links, yes I know what this function does it was just the first example I could easily find using "<QPushButton *>", sorry for any confusion.

          The large print givith and the small print taketh away. - Tom Waites.

          1 Reply Last reply Reply Quote 0
          • K
            Keith Hedger last edited by

            Ok bit more info, I have an array of Qwidgets for the prefs some of which are boolean some are text etc so it is convenient to use a generic type and type cast the widget to get the value, all 3 ways of casting ( for instance a check box ), seem to work fine and consistently but give a few minor diffs in the size of the executable by just a few bytes, not worth worrying about.

            So what is the best way:
            @((QCheckBox*)prefsWidgets[widgnum])->setChecked(onoff);@
            @dynamic_cast<QCheckBox *>(prefsWidgets[widgnum])->setChecked(onoff);@
            @qobject_cast<QCheckBox *>(prefsWidgets[widgnum])->setChecked(onoff);@

            The first is slightly less typing!

            The large print givith and the small print taketh away. - Tom Waites.

            1 Reply Last reply Reply Quote 0
            • C
              ckakman last edited by

              Hi,

              You should prefer the third option but with a pointer check:
              @
              QCheckBox checkBox = qobject_cast<QCheckBox>(prefsWidgets[widgnum]);
              if (checkBox) checkBox->setChecked(onoff);
              @

              Of course you can skip the null-ptr test if you are sure of the type.

              First one is good, old but too powerful C-style cast which most of the C++ programmers would advise to avoid.

              Dynamic cast is costly. That's the reason there is a qobject_cast.

              1 Reply Last reply Reply Quote 0
              • K
                Keith Hedger last edited by

                In this instance I will probably go with the qobject_cast as I don't need to do any run time checking that I gather dynamic_cast does.

                Thanks for the help guys.

                The large print givith and the small print taketh away. - Tom Waites.

                1 Reply Last reply Reply Quote 0
                • SGaist
                  SGaist Lifetime Qt Champion last edited by

                  Hi,

                  Indeed, don't do C style cast, there's no check at all with and you're in for a lot of problem. Also object_cast works across library boundaries.

                  Anyway, since it's only a matter of calling setChecked, you can even reduce your code by using the property system.

                  Just call:

                  @prefsWidgets[widgnum]->setProperty("checked", onoff);@

                  and you should be good to go

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply Reply Quote 0
                  • K
                    Keith Hedger last edited by

                    [quote author="SGaist" date="1420585048"]...Just call:

                    @prefsWidgets[widgnum]->setProperty("checked", onoff);@

                    ...[/quote]

                    That's a much more elegant solution to the above bit of code, I didn't know I could access the property data directly, learned something new. Thanks!

                    The large print givith and the small print taketh away. - Tom Waites.

                    1 Reply Last reply Reply Quote 0
                    • First post
                      Last post