Important: Please read the Qt Code of Conduct -

best practice to "replace" a widget in a qtdesigner UI

  • OK. This has been an issue that I've always just "worked around" in the past, which is ugly, and bothers my sensibilities. I frequently need to implement widgets in UI code that are subclasses of the standard available palette of widgets in qt-designer. I don't necessarily want to add a QSlider, but a specialized slider that meets my needs for a project, and this is frequent enough, but varied enough, that it does not justify creating a qt-designer aware custom widget and adding to the widget palette.

    My work-around has always been to leave a simple QWidget placeholder and add my subclassed widget to that placeholder...However!!!! I consider this a really lame practice that very much negates the intended benefit of using a UI designer in the first place.

    What is the accepted best-practice for adding subclassed widgets in designer, given my uses cases? Am I stuck with using placeholder widgets and inserting the sublcass underneath them? It would be nice to be able to do something like inerst a QSlider in designer, but then "replace" the slider with my custom one at runtime.

    I'm typically realizing the interface using the ui.setup(this) methodology, so the ui object holds the processed designer code.


  • Lifetime Qt Champion

    When you say placeholder, you do mean the Promote feature? Correct ?
    There is really not that many options.
    Either use promotion and possible dynamic variable for a bit of Editor enabled setup or
    make it a full blown Designer plugin like the build-ins Widgets are.

  • @Kent-Dorfman have you tried promoting widgets in Qt Designer? From documentation:

    For specialized widgets that subclass standard classes, the obvious choice of placeholder is the base class of the custom widget; for example, QSlider might be used for specialized QSlider subclasses.

  • Promote doesn't work for me in pyQt... :^O At least I haven't figured it out in pyQt.

    I generally have to insert a QWidget where I want a subclass to exist, set the size constraints and policy for that region of the canvas, and then instantiate by custom widget at runtime, making the placeholder its parent. Ugle, but that's what I generally end up having to do...since I work in C++ and/or python.

  • Lifetime Qt Champion

    ahh, sorry didn't catch it was with python.
    Its a shame promotion is not available in python. it really is a fine compromise.

  • @mrjj OK. I will investigate the promote feature further. Hopefully it doesn't make hard assumptions about the language being C++.

  • Lifetime Qt Champion

    Well its a Creator feature.
    So it should generate python code for it to work and as far as i know its not possible.

  • @mrjj said in best practice to "replace" a widget in a qtdesigner UI:

    Well its a Creator feature.

    well, for me it's more a uic (in this case pyuic) thing, generating code for the promoted widget (that Qt Creator wrote into the .ui file). Remember that eventually a .ui file could be hand-written...

    @Kent-Dorfman you may want to look at these posts:

    1. Promote PyQt Widget
    2. How do I use promote to in Qt Designer in pyqt4?

    it seems it should work, I guess you'll figure out what you might be doing not right

  • what I found was an issue for me, was how pyuic generated code based on the promotion rules. When it asks for promotion class header, don't use the complete class name with .py extension. If you do then you'll end up with bad imports in your generated py file. Anyway, working on it...As I say, features are definitely skewed toward c++ and python is hit or miss. The big gotcha is ending up with any generated modules that you have to hand edit. Should never have to hand edit something something generated for you in this case.

  • Lifetime Qt Champion

    You are right. Creator just inserts the promoted nameclass and uic does the magic.
    I was not aware it could still work with python but i see if ui has the name, then
    pyuic works the same

Log in to reply