QT / C ++ - 从不同的类访问MainWindow UI [英] QT/C++ - Accessing MainWindow UI from a different class

查看:1811
本文介绍了QT / C ++ - 从不同的类访问MainWindow UI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C ++和Qt的初学者,所以也许这是微不足道的。它当然觉得应该很简单,但我一直在寻找一个答案几个小时现在,找不到解决方案。我做一个简单的棋盘游戏,其中MainWindow的ui(在QtDesigner中制作)包含一个帆布为游戏板(一个QGraphicsView)。现在main.cpp很简单:

I'm a beginner to both C++ and Qt, so perhaps this is trivial. It certainly feels like it should be simple, but I've been searching for an answer for a few hours now and can't find the solution. I'm making a simple board game where the MainWindow's ui (made in QtDesigner) contains a canvas for the game board (a QGraphicsView). Now, the main.cpp is as simple as can be:

MainWindow Game;

int main(int argc, char *argv[])
{
 QApplication a(argc, argv);

 Game.show();

return a.exec();
}


$ b $ p

由于我需要从另一个完全不相关的类访问和编辑MainWindow Widget,我认为最简单的方法是让MainWindow成为一个全局变量。看来,这种方法是非常错误的,但。在尝试在QtDesigner中运行项目时,我得到一个Microsoft Visual C ++运行时库错误:应用程序请求运行时以不寻常的方式终止它。

Since I need to access and edit the MainWindow Widgets from another totally unrelated class, I thought the easiest way would be to just make MainWindow a global variable. It seems that this approach was very wrong, though. Upon trying to run the project in QtDesigner I get a Microsoft Visual C++ runtime library error: the application has requested runtime to terminate it in an unusual way.

那么什么是

除了MainWindow,我还有一个新的游戏对话框(QDialog,从QtDesigner生成),点击菜单项后显示在MainWindow。当用户输入游戏的所有参数并在对话框中单击确定时,我实例化一个名为GameState的自定义非Qt类。这个类是为了操作游戏本身,绘制板,提示用户等。但是,因为这个类是在QDialog中创建的,它不知道一个MainWindow的存在,所以我不能做任何东西与MainWindow从这个类。如何修改MainWindow从一个不相关的类,然后?

Aside from the MainWindow I have a dialog for a new game (QDialog, generated from QtDesigner) that is displayed after clicking a menu item in MainWindow. When the user inputs all parameters for the game and clicks OK in the dialog, I instantiate a custom non-Qt class called GameState. This class is meant to operate the game itself, draw the board, prompt the user, etc. However, as this class is created in the QDialog, it does not know of the existence of a MainWindow and so I cannot do anything with the MainWindow from this class. How can I modify the MainWindow from an unrelated class, then?

此外,jsut setEnabled()函数如何工作?它从来没有做任何事情。我在QtDesigner中设置为禁用,然后尝试通过此功能启用仍然保持禁用在GUI ...

Also, jsut how does the setEnabled() function work? It never seems to do anything. Any widget I set as disabled in the QtDesigner and then try to enable through this function still stays disabled in the GUI...

推荐答案

首先,在创建 QApplication 对象之前创建 MainGame 是一个坏主意。
如果你想让这个 MainGame 对象全局可用,就像这样:

First off it's a bad idea to create MainGame before you create your QApplication object. If you want to have your MainGame object globally available like this it should be a pointer:

MainWindow *Game;
int main (int argc, char **argv)
{
  QApplication a (argc, argv);

  Game = new MainWindow();
  Game->show();

  int result = a.exec();

  delete Game;
  Game = NULL;

  return result;
}

然而这种方法不是最优雅的。有两个更好的选择。

This approach is however not the most elegant. There are two much better choices.


  1. QApplication 所有顶级窗口像你的 MainGame 这意味着你可以通过 QApplication :: topLevelWidgets()这是一个静态函数,并返回一个包含所有顶级窗口小部件的列表。因为你只有一个,第一个是你的 MainGame 。缺点是你必须使用它,但使用Qts qobject_cast< MainGame *>(...)是相当安全。

  1. The QApplication object actually stores all top level windows like your MainGame which means you can allways aquire it through QApplication::topLevelWidgets() which is a static function and returns a list with all top level widgets. Since you only have one, the first one is your MainGame. The drawback is you'll have to cast it, but using Qts qobject_cast<MainGame*>(...) is fairly safe. You'll have to check the result though to make sure it isn't a NULL pointer.

使用singelton的设计模式,你必须检查结果,以确保它不是一个NULL指针。你应该将游戏指针存储在Game类本身的源代码(cpp)文件中(子类 QMainWindow ),你的Game类应该实现一个静态的public方法,指针。因此,如果任何其他类需要 Game 指针,它只需调用:

Use the singelton design pattern. You should store the global Game pointer in the source (cpp) file of the Game class itself (subclass QMainWindow) and your Game class should implement a static public method which returns this global pointer. So if any other class needs the Game pointer, it simply calls:

MyGame *theGame = MyGame::getInstance();

关于你的 setEnabled()问题。请张贴相关代码。如果太多,可以通过邮件向我发送* .ui文件和一段代码。

Regarding your setEnabled() problem. Please post the relevant code. If it's too much feel free to send me the *.ui file and the piece of code via mail.

最好的问候
D

Best regards
D

这篇关于QT / C ++ - 从不同的类访问MainWindow UI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆