pyvistaqt icon indicating copy to clipboard operation
pyvistaqt copied to clipboard

QT Designer Widget Reference

Open zoner72 opened this issue 3 years ago • 3 comments

Gday all

I just thought I would bother you with another little problem for which I cant seem to find a proper resolution.

I have created a QT Designer GUI which accesses a form. In this form, data can be uploaded. After import, I want to create an image view of the data in a frame (or graphicsview). I have created a quickframe from the class Qframe for this purpose as shown in the pic below. image

However, when use the code example, I create a new instance (at the proper location), as shown below image

some sample code: Open_File_UWN_Model.txt .ui has been renamed to .txt to be able to upload Open_File_UWN_Model.txt

# create the frame
# self.frame = QtWidgets.QFrame()
vlayout = QtWidgets.QVBoxLayout()

     # add the pyvista interactor object
     self.plt = QtInteractor(self.quickframe)
     vlayout.addWidget(self.plt.interactor)
#call definition add_box somewhere

def add_box(self):
    box = pv.Box()
    self.plt.add_mesh(box)
    self.plt.reset_camera()`

As you can see, I've created an new instance overlapping the QT-design widget. I cant seem to figure out how can I use the QT-design instance without creating a new instance but just using the Widget reference?

zoner72 avatar Mar 02 '21 06:03 zoner72

Hello @zoner72 sorry for jumping in so late, did you find a solution?

I think I have an idea of you want to accomplish but I am not quite sure I understand the issue. If I understand correctly, you embed the QtInteractor in a QtDesigner UI and you want to avoid re-creating the interactor everytime you upload a dataset? :thinking:

GuillaumeFavelier avatar Apr 14 '21 10:04 GuillaumeFavelier

Hi @GuillaumeFavelier

Ha, yeah its been a wee while, not a problem though. I put this part to the side and started focussing on other parts of the code. Your description is correct, that was/is indeed what I am after. Somehow I couldn't get it to work.

zoner72 avatar Apr 15 '21 02:04 zoner72

A bit late but maybe this'll be helpful to others landing here trying to do the same thing.

This can be done with the "Promote to ..." menu when you right click on the placeholder widget. Add a promoted class like this (might be able to use a more specific base class, not sure if it matters):

image

Then Add, then Promote. Then when you load the UI, it'll have an attribute with the name you give that widget in Designer that is a QtInteractor you can re-use.

An example - assumes you at least have a QPushButton named button and a widget promoted to QtInteractor named plot:

from PyQt6 import QtWidgets, uic
import pyvista as pv


class Widget(QtWidgets.QWidget):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        uic.loadUi("example.ui", self)
        # self.plot is now a QtInteractor instance
        # self.button is a QPushButton

        self.x = 0
        self.button.clicked.connect(self.add_sphere)

    def add_sphere(self):
        mesh = pv.Sphere(center=(self.x, 0, 0))
        self.plot.add_mesh(mesh)
        self.x += 1


if __name__ == "__main__":
    app = QtWidgets.QApplication([])
    win = Widget()
    win.show()
    app.exec()
example.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>ExampleUI</class>
 <widget class="QWidget" name="ExampleUI">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>400</width>
    <height>300</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Form</string>
  </property>
  <layout class="QVBoxLayout" name="verticalLayout">
   <item>
    <widget class="QtInteractor" name="plot" native="true"/>
   </item>
   <item>
    <widget class="QPushButton" name="button">
     <property name="text">
      <string>Add Mesh</string>
     </property>
    </widget>
   </item>
  </layout>
 </widget>
 <customwidgets>
  <customwidget>
   <class>QtInteractor</class>
   <extends>QWidget</extends>
   <header>pyvistaqt</header>
   <container>1</container>
  </customwidget>
 </customwidgets>
 <resources/>
 <connections/>
</ui>

image

ixjlyons avatar Nov 05 '22 14:11 ixjlyons