pyvistaqt
pyvistaqt copied to clipboard
QT Designer Widget Reference
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.
However, when use the code example, I create a new instance (at the proper location), as shown below
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?
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:
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.
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):
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>