热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

必须在QWidget之前构建一个QApplication?-MustconstructaQApplicationbeforeaQWidget

EverywhereonlyjustbeforeQPaintDevicequestionsandnowhereismyerror.So,herewego.到处都是“在

Everywhere only just "before QPaintDevice" questions and nowhere is my error. So, here we go.

到处都是“在QPaintDevice之前”的问题,我的错误无处不在。所以,我们开始吧。

I need an extern QWidget to be able to get access to it from outside (because I don't know any other ways to do it). Basically, I need this: Create 2 QWidgets from 1 window, go to first window and from there hide main window and show second window created by main window (although main window is not main(), it is QWidget too).

我需要一个外部QWidget来从外部访问它(因为我不知道还有其他的方法)。基本上,我需要这个:从一个窗口创建两个QWidgets,到第一个窗口,然后从那里隐藏主窗口并显示主窗口创建的第二个窗口(虽然主窗口不是main(),它也是QWidget)。

I added

我添加了

extern QWidget *widget = new QWidget

everywhere and everyhow in possible ways, and I still got this message. I suppose, it means that I need to create my QApplication (in main.cpp) and only then declare any QWidgets. But then HOW can I access those QWidgets from another QWidgets?

无论在哪里,无论如何,我仍然得到了这个信息。我想,这意味着我需要创建我的QApplication(在main.cpp中),然后才声明任何qwidget。但是,我如何从另一个QWidgets访问这些QWidgets呢?

Code is here: https://github.com/ewancoder/game/tree/QWidget_before_QApp_problem

代码是:https://github.com/ewancoder/game/tree/QWidget_before_QApp_problem

P.S. The final goal is to be able show and hide both gamewindow.cpp and world.cpp from battle.cpp (just regular class)

最后的目标是能够显示和隐藏两个游戏窗口。cpp和世界。cpp的战斗。cpp(普通类)

And btw, adding Q_OBJECT and #include both don't work.

另外,添加Q_OBJECT和#include都不起作用。

Anyway, if I cannot use functions from one window to another, than what's the point? I can have one window in another, and then another in that one, and then one in that another... but I can't do anything from the last to the previous. After years on Delphi that seems strange to me.

不管怎样,如果我不能用函数从一个窗口到另一个窗口,这是什么意思呢?我可以有一个窗口在另一个,然后另一个在那个,然后一个在另一个…但我不能做任何事,从最后到上一个。在德尔斐呆了几年之后,我觉得很奇怪。

2 个解决方案

#1


1  

Don't use extern or otherwise static variables which lead to creation of the widget before the QApplication is created in main. The QApplication must exist before the constructor of the QWidget is executed.

在创建QApplication之前,不要使用外部或其他静态变量来创建小部件。q应用程序必须在QWidget的构造函数执行之前存在。

Instead of sharing the variable via extern, either make the other windows members of the main window, and then make the windows known to each other by passing around pointers, or keep them private in MainWindow and request the actions from the subwindows e.g. via signal/slots. As a generic rule, don't use global variables but class members.

不是通过外部共享变量,而是让主窗口的其他窗口成员,然后通过传递指针来让窗口相互了解,或者让它们在主窗口中保持私有,并从子窗口请求操作,例如通过信号/插槽。作为一个通用规则,不要使用全局变量,而是使用类成员。

In the following FirstWindow (which is supposed hide main window and secondWindow) gets the main window and the second window passed via pointers and then just calls show/hide on them directly.

在下面的FirstWindow(应该隐藏主窗口和secondWindow)中,获取主窗口,第二个窗口通过指针传递,然后直接调用show/hide。

int main(int argc, char **argv) {
    QApplication app(argc, argv);

    MainWindow mainWindow;
    mainWindow.show();

    return app.exec();
}

In main window, have two members for the two other windows, say FirstWindow and SecondWindow: class MainWindow : public QMainWindow { ... private: FirstWindow *m_firstWindow; SecondWindow *m_secondWindow; };

在主窗口中,有两个其他窗口的成员,比如FirstWindow和SecondWindow: class MainWindow: public QMainWindow{…私人:FirstWindow * m_firstWindow;SecondWindow * m_secondWindow;};

MainWindow::MainWindow(QWidget *parent) {
     m_firstWindow = new FirstWindow; //not pass this as parent as you want to hide the main window while the others are visible)
     m_secOndWindow= new SecondWindow;
     m_firstWindow->setMainWindow(this);
     m_firstWindow->setSecond(m_secondWindow);
     m_firstWindow->show(); //Show first window immediately, leave second window hidden
}

MainWindow::~MainWindow() {
     //Manual deletion is necessary as no parent is passed. Alternatively, use QScopedPointer
     delete m_firstWindow;
     delete m_secondWindow;
}

FirstWindow, inline for brevity:

FirstWindow,内联简洁:

class FirstWindow : public QWidget {
    Q_OBJECT
public:
    explicit FirstWindow(QWidget *parent = 0) : QWidget(parent) {}

    void setMainWindow(MainWindow *mainWindow) { m_mainWindow = mainWindow); }
    void setSecondWindow(SecondWindow *secondWindow) { m_secOndWindow= secondWindow; }

private Q_SLOTS:
     void somethingHappened() { //e.g. some button was clicked
         m_mainWindow->hide();
         m_secondWindow->show();
     }
private: 
     MainWindow* m_mainWindow;
     SecondWindow* m_secondWindow;  
};

#2


0  

Maybe not helping the former author, but others facing the problem. I simply got this error by mistaking a debug-library with a release one. So check your linker settings, if you are sure the implementation is done right (first instancing application and then using widgets).

也许不是帮助前作者,而是其他人面对问题。我只是错误地将一个调试库与一个发布版本错误地混淆了。因此,检查链接器设置,如果您确定实现是正确的(第一个实例应用程序,然后使用小部件)。


推荐阅读
  • 向QTextEdit拖放文件的方法及实现步骤
    本文介绍了在使用QTextEdit时如何实现拖放文件的功能,包括相关的方法和实现步骤。通过重写dragEnterEvent和dropEvent函数,并结合QMimeData和QUrl等类,可以轻松实现向QTextEdit拖放文件的功能。详细的代码实现和说明可以参考本文提供的示例代码。 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • ZSI.generate.Wsdl2PythonError: unsupported local simpleType restriction ... [详细]
  • 注:根据Qt小神童的视频教程改编概论:利用最新的Qt5.1.1在windows下开发的一个小的时钟程序,有指针与表盘。1.Qtforwindows开发环境最新的Qt已经集 ... [详细]
  • 微软头条实习生分享深度学习自学指南
    本文介绍了一位微软头条实习生自学深度学习的经验分享,包括学习资源推荐、重要基础知识的学习要点等。作者强调了学好Python和数学基础的重要性,并提供了一些建议。 ... [详细]
  • vue使用
    关键词: ... [详细]
  • IhaveconfiguredanactionforaremotenotificationwhenitarrivestomyiOsapp.Iwanttwodiff ... [详细]
  • 本文主要解析了Open judge C16H问题中涉及到的Magical Balls的快速幂和逆元算法,并给出了问题的解析和解决方法。详细介绍了问题的背景和规则,并给出了相应的算法解析和实现步骤。通过本文的解析,读者可以更好地理解和解决Open judge C16H问题中的Magical Balls部分。 ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • 推荐系统遇上深度学习(十七)详解推荐系统中的常用评测指标
    原创:石晓文小小挖掘机2018-06-18笔者是一个痴迷于挖掘数据中的价值的学习人,希望在平日的工作学习中,挖掘数据的价值, ... [详细]
  • Java学习笔记之面向对象编程(OOP)
    本文介绍了Java学习笔记中的面向对象编程(OOP)内容,包括OOP的三大特性(封装、继承、多态)和五大原则(单一职责原则、开放封闭原则、里式替换原则、依赖倒置原则)。通过学习OOP,可以提高代码复用性、拓展性和安全性。 ... [详细]
  • Android开发实现的计时器功能示例
    本文分享了Android开发实现的计时器功能示例,包括效果图、布局和按钮的使用。通过使用Chronometer控件,可以实现计时器功能。该示例适用于Android平台,供开发者参考。 ... [详细]
  • 预备知识可参考我整理的博客Windows编程之线程:https:www.cnblogs.comZhuSenlinp16662075.htmlWindows编程之线程同步:https ... [详细]
  • 本文讨论了一个数列求和问题,该数列按照一定规律生成。通过观察数列的规律,我们可以得出求解该问题的算法。具体算法为计算前n项i*f[i]的和,其中f[i]表示数列中有i个数字。根据参考的思路,我们可以将算法的时间复杂度控制在O(n),即计算到5e5即可满足1e9的要求。 ... [详细]
  • 本文介绍了pack布局管理器在Perl/Tk中的使用方法及注意事项。通过调用pack()方法,可以控制部件在显示窗口中的位置和大小。同时,本文还提到了在使用pack布局管理器时,应注意将部件分组以便在水平和垂直方向上进行堆放。此外,还介绍了使用Frame部件或Toplevel部件来组织部件在窗口内的方法。最后,本文强调了在使用pack布局管理器时,应避免在中间切换到grid布局管理器,以免造成混乱。 ... [详细]
author-avatar
核能裸麦_536
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有