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. best practice to "replace" a widget in a qtdesigner UI

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

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 3 Posters 5.2k 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.
  • Kent-DorfmanK Offline
    Kent-DorfmanK Offline
    Kent-Dorfman
    wrote on last edited by
    #1

    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.

    Thoughts?

    Pablo J. RoginaP 1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

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

      1 Reply Last reply
      1
      • Kent-DorfmanK Kent-Dorfman

        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.

        Thoughts?

        Pablo J. RoginaP Offline
        Pablo J. RoginaP Offline
        Pablo J. Rogina
        wrote on last edited by
        #3

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

        Upvote the answer(s) that helped you solve the issue
        Use "Topic Tools" button to mark your post as Solved
        Add screenshots via postimage.org
        Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

        1 Reply Last reply
        1
        • Kent-DorfmanK Offline
          Kent-DorfmanK Offline
          Kent-Dorfman
          wrote on last edited by
          #4

          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.

          mrjjM 1 Reply Last reply
          0
          • Kent-DorfmanK Kent-Dorfman

            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.

            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #5

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

            Kent-DorfmanK 1 Reply Last reply
            1
            • mrjjM mrjj

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

              Kent-DorfmanK Offline
              Kent-DorfmanK Offline
              Kent-Dorfman
              wrote on last edited by
              #6

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

              mrjjM 1 Reply Last reply
              1
              • Kent-DorfmanK Kent-Dorfman

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

                mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @Kent-Dorfman
                Well its a Creator feature.
                https://doc.qt.io/qt-5/designer-using-custom-widgets.html
                So it should generate python code for it to work and as far as i know its not possible.

                Pablo J. RoginaP 1 Reply Last reply
                0
                • mrjjM mrjj

                  @Kent-Dorfman
                  Well its a Creator feature.
                  https://doc.qt.io/qt-5/designer-using-custom-widgets.html
                  So it should generate python code for it to work and as far as i know its not possible.

                  Pablo J. RoginaP Offline
                  Pablo J. RoginaP Offline
                  Pablo J. Rogina
                  wrote on last edited by
                  #8

                  @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

                  Upvote the answer(s) that helped you solve the issue
                  Use "Topic Tools" button to mark your post as Solved
                  Add screenshots via postimage.org
                  Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

                  mrjjM 1 Reply Last reply
                  4
                  • Kent-DorfmanK Offline
                    Kent-DorfmanK Offline
                    Kent-Dorfman
                    wrote on last edited by
                    #9

                    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.

                    1 Reply Last reply
                    2
                    • Pablo J. RoginaP Pablo J. Rogina

                      @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

                      mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      @Pablo-J.-Rogina
                      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

                      1 Reply Last reply
                      0

                      • Login

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