BiAPoL / napari-clusters-plotter

A napari plugin for clustering objects according to their properties.
BSD 3-Clause "New" or "Revised" License
77 stars 10 forks source link

Exchange Qt code file with a file from Qt Designer #175

Open lazigu opened 1 year ago

lazigu commented 1 year ago

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

haesleinhuepf commented 1 year ago

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.

lazigu commented 1 year ago

@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 :)

jo-mueller commented 1 year ago

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.

haesleinhuepf commented 1 year ago

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

jo-mueller commented 1 year ago

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.

haesleinhuepf commented 1 year ago

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? 🙃