Я разобрался, как это сделать. Из-за моего недостатка знаний в QT я не знаю, как это работает, только то, что это работает. Мой пользовательский интерфейс содержит окно с вкладками. На первой вкладке «Обзор» есть кнопки и номера LCD, которые мне нужно переместить.
Как показано здесь , я последовал его примеру и создал еще один «объект класса», который вызывает исходный пользовательский интерфейс, созданный QT Designer. Остальные комментарии в следующем коде.
class MainWindow(QtWidgets.QTabWidget):
resized = QtCore.pyqtSignal()
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent=parent)
# Here import the QTdesigner created ui class
ui = Ui_TabWidget1()
ui.setupUi(self)
# First get the relative positions of the childwidgets wrt original screen.
relative_widget_positions,widgetlist = self.get_relative_widget_positions()
#Then connect the resized event with a function that manipulates the widgets
self.resized.connect(lambda: self.reposition_widgets_on_resize(relative_widget_positions,widgetlist))
#-----------------------------------------------------------------
# Find the various children widgets of 'Overviewtab' of the imported UI.
def get_relative_widget_positions(self):
# get list of widgets along with their positions, sizes
widgetlist = self.findChildren((QtWidgets.QLCDNumber , QtWidgets.QPushButton))
# Compute the background image coordinates
I_x,I_y,I_w,I_h = self.get_image_coordinates()
# initate the relative positions array
relative_widget_positions =[]
# iterate over each widget to compute its relative position wrt image width and height
for w in widgetlist:
pos = w.pos()
relative_x = (pos.x()-I_x)/I_w
relative_y = (pos.y()-I_y)/I_h
relative_widget_positions.append([relative_x,relative_y])
# print(relative_widget_positions)
return relative_widget_positions,widgetlist
#-----------------------------------------------------------------
def resizeEvent(self, event):
self.resized.emit()
return super(MainWindow, self).resizeEvent(event)
#-----------------------------------------------------------------
def reposition_widgets_on_resize(self,relative_widget_positions,widgetlist):
I_x,I_y,I_w,I_h = self.get_image_coordinates()
for w,r in zip(widgetlist,relative_widget_positions):
w.move(r[0]*I_w + I_x , r[1]*I_h + I_y)
#-----------------------------------------------------------------
def get_image_coordinates(self):
# find area sizes. Note that the top tabs need to be removed fromt
# the area where image is drawn. Also all child widgets are wrt to geometry of parent rather than frame.
# so use geometry() rather than frameGeometry(). See:
# https://doc-snapshots.qt.io/4.8/application-windows.html#window-geometry
# Also, the 'corrections' below are necessary to adjust for the 'Tabs' on the top,
# as well as some arbitrary adjustments i had to make for positioning to be right.
g = self.geometry()
g_x = g.x() + 10
g_y = g.y() + 43
g_w = g.width() - 20
g_h = g.height() - 43
# From the image file's widthe/height
I_w_to_h_ratio = 1.277
# Based on the window, the Image's either width or height is the same as that of the 'client area' as the image scales without changing its aspect ratio.
if(g_w/g_h > I_w_to_h_ratio): # normal screen orientation
I_h = g_h
I_w = I_w_to_h_ratio * I_h
elif(g_w/g_h <= I_w_to_h_ratio): # abnormal screen orientation
I_w = g_w
I_h = I_w/I_w_to_h_ratio
# Compute image origins - very important parameter.
I_x = (g_w - I_w)/2 #+ g_x
I_y = (g_h - I_h)/2 #+ g_y
# print(g_x,g_y,g_w,g_h,I_x,I_y,I_w,I_h)
return (I_x,I_y,I_w,I_h)
Надеюсь, это поможет кому-то вроде меня. спасибо!