无法从 QMdiSubWindow 投射 QMainWindow

发布于 2024-12-03 19:52:35 字数 1156 浏览 0 评论 0原文

所以我有一个程序,它使用 QMainWindow 的子类作为 QMdiArea 小部件。我这样做只是因为 QDockWidgets 只能在 QMainWindow 中使用(并且我的子类需要停靠小部件)。

我正在测试类的序列化以进行保存,但我的指针不正确。下面是代码

    if (ui->mdiMain->subWindowList().length() > 0)
    {
        QString path = QFileDialog::getSaveFileName(this, "Save Build Order", "" ,"*.cbo");

        if (path > 0)
        {
            QFile file(path, this);
            if (file.open(QFile::WriteOnly) == true)
            {
                QWidget* widget = reinterpret_cast<QWidget*>(ui->mdiMain->activeSubWindow());
                WidgetBuildOrder* widgetBuildOrder = reinterpret_cast<WidgetBuildOrder*>(widget);

                QDataStream stream(&file);
                stream << widgetBuildOrder;

                file.close();
                ui->statusBar->showMessage("Save Successful.", 5000);
            }
        }

    }
}

WidgetBuildOrder 是 MainWindow 的子类。

所以我的想法是我可以继续将指针投射到它的实际位置。当我创建MDI区域时,我传递了一个新的WidgetBuildOrder。有办法做到这一点吗?

澄清一下,在转换之后,我确实得到了一个指针,它只是指向垃圾。不是我实际打开的 WidgetBuildOrder 中设置的值。

任何帮助将不胜感激,

谢谢,

Jec

So i have a program that uses a subclass of QMainWindow for QMdiArea widgets. I'm only doing this because QDockWidgets can only be used in a QMainWindow (and my subclass needed dock widgets).

I was testing my class's serialization for saving purposes and my pointer wasn't correct. Here is the code

    if (ui->mdiMain->subWindowList().length() > 0)
    {
        QString path = QFileDialog::getSaveFileName(this, "Save Build Order", "" ,"*.cbo");

        if (path > 0)
        {
            QFile file(path, this);
            if (file.open(QFile::WriteOnly) == true)
            {
                QWidget* widget = reinterpret_cast<QWidget*>(ui->mdiMain->activeSubWindow());
                WidgetBuildOrder* widgetBuildOrder = reinterpret_cast<WidgetBuildOrder*>(widget);

                QDataStream stream(&file);
                stream << widgetBuildOrder;

                file.close();
                ui->statusBar->showMessage("Save Successful.", 5000);
            }
        }

    }
}

WidgetBuildOrder is a subclass of MainWindow.

so my thought was that I could keep casting the pointer to what it actually is. When I created the MDI area, i passed a new WidgetBuildOrder. Is there a way to do this?

To Clarify, after the casting I do get a pointer its just pointing to garbage. Not the values set in the WidgetBuildOrder I actually have open.

Any help will be appreciated,

Thanks,

Jec

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

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

发布评论

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

评论(2

梦忆晨望 2024-12-10 19:52:35

QMdiArea::activeSubWindow 返回一个 QMdiSubWindow*,如果 QMdiArea 没有焦点,则返回 0。

由于您不可能有一个继承自 QMainWindowQMdiSubWindow 的类,因此您想要的是在 current 子窗口中获取小部件窗户:

QMdiSubWindow *subWindow = ui->mdiMain->currentSubWindow();
WidgetBuildOrder* widgetBuildOrder = 
    static_cast<WidgetBuildOrder*>(subwindow->widget());

QMdiArea::activeSubWindow returns a QMdiSubWindow*, or 0 it the QMdiArea doesn't have the focus.

As you can't possibly have a class that inherits from QMainWindow and QMdiSubWindow, what you want is to get the widget inside the current sub-window:

QMdiSubWindow *subWindow = ui->mdiMain->currentSubWindow();
WidgetBuildOrder* widgetBuildOrder = 
    static_cast<WidgetBuildOrder*>(subwindow->widget());
墨洒年华 2024-12-10 19:52:35

您是否尝试过不向 WidgetBuildOrder 指针提供 QWidget 的指针,而是向activatedSubWindow 提供正确的对象?

像这样的东西:

//QWidget* widget = reinterpret_cast<QWidget*>(ui->mdiMain->activeSubWindow());
WidgetBuildOrder* widgetBuildOrder = reinterpret_cast<WidgetBuildOrder*>(ui->mdiMain->activeSubWindow());

have you tried to not give the WidgetBuildOrder pointer the pointer of the QWidget but the activatedSubWindow to get the right object back?

Something like:

//QWidget* widget = reinterpret_cast<QWidget*>(ui->mdiMain->activeSubWindow());
WidgetBuildOrder* widgetBuildOrder = reinterpret_cast<WidgetBuildOrder*>(ui->mdiMain->activeSubWindow());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文