![](/img/trans.png)
[英]PySide - resetting the mainwindow central widget from within the central widget
[英]PySide PyQt MainWindow or Central Widget access from deeply nested widgets
呼吁PyQt專家了解一些最佳做法。
我有一些應用程序通用屬性,這些屬性定義為QApplication上的屬性。 在我的MainWindow初始化中,我為MainWindow分配了一個“ app”屬性。 我想在深度嵌套的小部件中訪問應用程序的變量。 現在,我可以通過調用足夠的“ parent()”調用來達到MainWindow級別。
這似乎很笨拙,我猜測在這種情況下還有另一種最佳實踐方案。 以下是一些代碼片段,可以更好地理解該問題。
應用類別
class App(QtGui.QApplication):
def __init__(self, sim, *args, **kwargs):
super(App, self).__init__(*args, **kwargs)
self.sim = sim
window = MW.BaseUI(digi_thread, app=self)
主窗口類
class BaseUI(QtGui.QMainWindow):
def __init__(self, digi_thread, app, parent=None):
super(BaseUI, self).__init__(parent)
self.app = app
從嵌套窗口小部件(標簽簿中的標簽)進入主窗口的一些代碼示例
@property
def main_window(self):
return self.parent().parent().parent().parent()
def some_function(self):
if self.main_window.app.sim:
print "in simulation mode"
我也不確定CentralWidget是否與解決此類問題有關。
您始終可以通過PySide.QtCore.QCoreApplication.instance()
轉到當前應用程序實例。 在C ++中,全局qApp
變量提供相同的功能。
因此,無論您是在全局應用程序實例上設置python屬性還是Qt屬性,都可以始終使用它,而無需經歷任何層次結構。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.