无法从 QMdiSubWindow 投射 QMainWindow
所以我有一个程序,它使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
QMdiArea::activeSubWindow
返回一个QMdiSubWindow*
,如果QMdiArea
没有焦点,则返回 0。由于您不可能有一个继承自
QMainWindow
和QMdiSubWindow
的类,因此您想要的是在 current 子窗口中获取小部件窗户:QMdiArea::activeSubWindow
returns aQMdiSubWindow*
, or 0 it theQMdiArea
doesn't have the focus.As you can't possibly have a class that inherits from
QMainWindow
andQMdiSubWindow
, what you want is to get the widget inside the current sub-window:您是否尝试过不向 WidgetBuildOrder 指针提供 QWidget 的指针,而是向activatedSubWindow 提供正确的对象?
像这样的东西:
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: