简体   繁体   English

如何从父QWainWindow的QWidget访问QMainWindow

[英]How to access QMainWindow from a QWidget which parent is not QMainWindow

I am programming a GUI with Qt Creator (Qt5.10) under OSX. 我正在使用OSX下的Qt Creator(Qt5.10)编写GUI。 I have a QMainWindow for the main app's window. 我有一个QMainWindow用于主应用程序的窗口。 Inside this I have a QTab widget, in which I have entire widget (let's call it CaptureTabWidget which contains some additional widgets AND a QFrame (the parent of this QFrame is the QTab ). In this last widget I will be drawing some stuff. The point is that I would like to write in the statusBar of the QMainWindow some values related to the position of the mouse in that QFrame , but I do not have any idea about how to access the main window from a method inside the CaptureTabWidget which contains the QFrame . 在这里我有一个QTab小部件,其中我有整个小部件(让我们称之为CaptureTabWidget ,它包含一些额外的小部件和一个QFrame (这个QFrame的父级是QTab )。在这最后一个小部件中,我将绘制一些东西。关键是我想在QMainWindowstatusBar中写一些与QFrame鼠标位置相关的值,但我不知道如何从CaptureTabWidget里面的方法访问主窗口,其中包含QFrame

The whole picture goes like this: 整个画面如下:

                 X
+-----------+  XXX
|MainWindow | XXXXXXXXX
+-----+-----+  XXX    XXXXXX
      |          X         XXX
+-----v------------+          XX
|QTabWidget        |           XX
|No parent         |            XX
+-----+------------+             XX
      |                           X
+-----v-------------+          ?? X
|CaptureTabQWidget  |             X
|Parent:QTabWidget  |             X
+-----+-------------+            X
      |                         X
+-----v-------------+         XX
|QFrame             |    XXXXXX
|Parent:CaptureTab  | XXX
+-------------------+

I have tried modifying the CaptureTabWidget's constructor in order to receive a QMainWindow* which points to the main window, but it seems Qt Creator doesn't allow custom constructors when promoting widgets in the Designer. 我已经尝试修改CaptureTabWidget的构造函数,以便接收指向主窗口的QMainWindow* ,但似乎Qt Creator在Designer中提升窗口小部件时不允许自定义构造函数。 I am wondering what is the right way to do this. 我想知道这样做的正确方法是什么。

If you just aim to write to the statusBar of the parent QMainWindow , you can post a QStatusTipEvent to any child of your QMainWindow , and the event will keep propagating to the parent QWidget s until it reaches the main window where it gets handled by by changing the content in the status bar. 如果你只需要瞄准写入statusBar父的QMainWindow ,你可以发布QStatusTipEvent您的任何孩子QMainWindow ,事件将继续传播到父QWidget s,至到达那里得到通过改变处理的主窗口状态栏中的内容。 In other words, something like this in your QFrame does the job: 换句话说, QFrame中的这样的东西可以完成这项工作:

QCoreApplication::postEvent(this, new QStatusTipEvent("your tip"));
//                          ^^^^
//                          assuming this is a pointer to your QFrame object

As a side note, this is the same way views in Qt ( QTableView , QTreeView , QListView , ...) are able to change status tips (when their models provide a custom value for the role Qt::StatusTipRole ) while they might be buried deep down in the QObject tree... 作为旁注,这与Qt( QTableViewQTreeViewQListView ,...)中的QListView能够更改状态提示(当它们的模型为角色Qt::StatusTipRole提供自定义值)时的情况相同,而它们可能是埋藏在QObject树的深处......


For purposes other than just writing to the statusBar of a parent QMainWindow , I would try to generalize the above approach by having a custom QEvent (ie one that has a value between QEvent::User and QEvent::MaxUser ) and handle that event in my QMainWindow subclass appropriately. 除了写入父QMainWindowstatusBar之外的其他目的,我会尝试通过自定义QEvent (即具有QEvent::UserQEvent::MaxUser之间的值的QEvent::User来概括上述方法并处理该事件。我的QMainWindow子类是恰当的。 I find this way much cleaner and more maintainable than other solutions that depend on global variables/singletons (that keep track of your supposedly one QMainWindow instance). 我发现这种方式比依赖于全局变量/单例(跟踪你所谓的一个QMainWindow实例)的其他解决方案更清晰,更易于维护。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM