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. [SOLVED]Best coding practice
Forum Update on Monday, May 27th 2025

[SOLVED]Best coding practice

Scheduled Pinned Locked Moved General and Desktop
8 Posts 4 Posters 2.4k 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.
  • K Offline
    K Offline
    Keith Hedger
    wrote on 6 Jan 2015, 12:22 last edited by
    #1

    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
    0
    • Z Offline
      Z Offline
      Zlatomir
      wrote on 6 Jan 2015, 12:50 last edited by
      #2

      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
      0
      • K Offline
        K Offline
        Keith Hedger
        wrote on 6 Jan 2015, 13:04 last edited by
        #3

        [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
        0
        • K Offline
          K Offline
          Keith Hedger
          wrote on 6 Jan 2015, 16:47 last edited by
          #4

          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
          0
          • C Offline
            C Offline
            ckakman
            wrote on 6 Jan 2015, 18:26 last edited by
            #5

            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
            0
            • K Offline
              K Offline
              Keith Hedger
              wrote on 6 Jan 2015, 19:36 last edited by
              #6

              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
              0
              • S Offline
                S Offline
                SGaist
                Lifetime Qt Champion
                wrote on 6 Jan 2015, 22:57 last edited by
                #7

                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
                0
                • K Offline
                  K Offline
                  Keith Hedger
                  wrote on 7 Jan 2015, 10:55 last edited by
                  #8

                  [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
                  0

                  1/8

                  6 Jan 2015, 12:22

                  • Login

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