Important: Please read the Qt Code of Conduct -

[SOLVED]Best coding practice

  • 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:

    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?

  • To perform that type off casts in C++ you usually use "dynamic_cast": , but with QObject subclasses (all widgets are derived indirectly from QObject) the recommended way is "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":

  • [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":[/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.

  • 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:
    @dynamic_cast<QCheckBox *>(prefsWidgets[widgnum])->setChecked(onoff);@
    @qobject_cast<QCheckBox *>(prefsWidgets[widgnum])->setChecked(onoff);@

    The first is slightly less typing!

  • 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.

  • 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.

  • Lifetime Qt Champion


    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

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

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


    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!

Log in to reply