napari-clusters-plotter icon indicating copy to clipboard operation
napari-clusters-plotter copied to clipboard

Exchange Qt code file with a file from Qt Designer

Open lazigu opened this issue 2 years ago • 6 comments
trafficstars

https://doc.qt.io/qt-6/qtdesigner-manual.html

lazigu avatar Feb 15 '23 12:02 lazigu

Before working on this, it would be good to understand better how magicgui and qt can be combined. E.g. designing a pulldown with all image layers that are open in Napari might be tricky with the Qt designer, however it's a one-liner using magicgui.

haesleinhuepf avatar Feb 17 '23 10:02 haesleinhuepf

@haesleinhuepf, I think @jo-mueller combined Designer with magicgui in napari-stress, it is possible to "reserve" some space for magicgui widgets that are added then later if I got the explanation right :)

lazigu avatar Feb 17 '23 10:02 lazigu

You can do it the same way you already do it (e.g., here where a magicgui widget is created which is later added to the bigger widget here . It would work analogously if you used the designer.

jo-mueller avatar Feb 17 '23 12:02 jo-mueller

But how can I include a magicgui-based pulldown into a user-interface that comes from a .ui file?

haesleinhuepf avatar Feb 18 '23 17:02 haesleinhuepf

I do it here, for instance. The general code base doesn't change that much and follows the following pattern:

class my_widget(QWidget):

    def __init__(self, napari_viewer):
        super().__init__()

        self.viewer = napari_viewer

        uic.loadUi(os.path.join(Path(__file__).parent, './toolbox.ui'), self)  # this loads the ui file

        #  this adds a magicgui widget to the widget
        self.image_layer_select = create_widget(annotation=Image, label="Image_layer")
        self.layout().addWidget(self.image_layer_select.native, 0, 1)

In principle, the loadUi function just wraps up all the widget creation stuff (adding buttons, texts, size policies, tooltips, default values, etc) - connecting them to actual functionality still needs to be done inside the __init__ of the widget definition.

The only thing to be kept in mind is to leave a blank space in the designer (i.e. an empty widget) where the magicgui should later go.

jo-mueller avatar Feb 19 '23 16:02 jo-mueller

The only thing to be kept in mind is to leave a blank space in the designer (i.e. an empty widget) where the magicgui should later go.

That's the interesting part! How can I replace an empty widget, that was created using the designer, with a magicgui generated widget? I imagine it's hard to replace a widget in the middle of other widgets surrounded by buttons and tabs with a custom widget and make sure the magicgui functionality still works. Do you think it makes sense to demonstrate this in a short blog post? 🙃

haesleinhuepf avatar Feb 19 '23 16:02 haesleinhuepf