如何与其他小部件共享小部件指针

发布于 2024-12-07 00:35:21 字数 706 浏览 1 评论 0原文

我有一种用 QT 编写的视频播放器应用程序,其想法是我可以单击视频帧,应用程序将从这个精确的像素中获取坐标和颜色信息,到目前为止,该应用程序已经按预期工作,但是问题是我有一个控制面板希望有一个列表小部件,我用框架中的信息填充它,为此我需要在这两个小部件之间进行某种通信,我现在要做的是使用此方法来查找mainWindow:

QWidget* mainwindow;
QWidgetList list = QApplication::topLevelWidgets();
for (int a = 0; a < list.size(); a++)
    if (list[a]->objectName() == "MainWindow")
    {
        mainwindow = list[a];
        break;
    }

通过这个我可以访问主窗口小部件和主窗口将充当访问另一个小部件的指针的桥梁,我使用这种方法看到的问题是我需要对我需要访问的每个小部件有一个 get 方法。 另一种方法是在小部件中使用 set 方法并使用父小部件希望可以访问我正在寻找的两个小部件以将其正确设置,这样做的问题是在父小部件希望中为此创建方法与这两种方法没有任何关系.. 所以我要问的是最好的或更干净的方法是什么?有一些 QT 方法可以做到这一点吗?谢谢!

PS:需要明确的是,我不是在谈论信号和插槽,我的想法是使用它们,但首先我需要一个指向我正在寻找的小部件的指针,然后连接到它的插槽!

I have a kind of video player application written in QT, the idea is that I can click in an video frame and the app will get the coords and color information from this exact pixels, so far so good the application already works as spected, but the thing is that I have a control panel wish has an List Widget that I fill with the information from the frame, to do that I need to have some kind of communication between these two widgets, what I do now is use this method to find the mainWindow:

QWidget* mainwindow;
QWidgetList list = QApplication::topLevelWidgets();
for (int a = 0; a < list.size(); a++)
    if (list[a]->objectName() == "MainWindow")
    {
        mainwindow = list[a];
        break;
    }

with this I have access to the mainwindow widget and the mainwindow will serve as a bridge to get access to the pointers of another widget, the problem that I see with this approach is that I need to have an get method to every widget I need access..
Another way to do this is to have an set method in the widget and use a parent widget wish has access to the two widgets I'm looking for to set it right, the problem with this is making methods to this in a parent widget wish doesn't have anything to do with the two methods..
So what I'm asking for is what the best or cleaner method to do this? there is some QT-Way to do this? Thanks!

PS: Just to be clear, I'm not talking about SIGNALS and SLOTS, my idea is to use theses, but first I need an pointer to the widget I'm looking for to then connect to it's slots!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

给我一枪 2024-12-14 00:35:21

您试图在较低级别的代码中执行此操作,这就是它很尴尬的原因。两个小部件都没有另一个小部件的上下文。

只需在要通信的两个小部件上创建信号/槽,然后让调用范围在创建它们后将它们连接起来。

You are trying to do it in lower level code, that is why it is awkward. Neither widget has the context of the other.

Just create signal/slots on the two widgets you want to communicate, then have the calling scope connect them after they are created.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文