pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

QT/C++ - 從不同的類訪問 MainWindow UI

QT/C++ - Accessing MainWindow UI from a different class(QT/C++ - 從不同的類訪問 MainWindow UI)
本文介紹了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();
}

由于我需要從另一個完全不相關的類訪問和編輯 MainWindow 小部件,我認為最簡單的方法是將 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.

那么做我需要的正確方法是什么?

So what is the correct way to do what I need?

除了 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?

另外,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(...) 是相當安全的.您必須檢查結果以確保它不是 NULL 指針.

  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.

使用單例設計模式.您應該將全局 Game 指針存儲在 Game 類本身(子類 QMainWindow)的源 (cpp) 文件中,并且您的 Game 類應該實現一個返回此全局指針的靜態公共方法.因此,如果任何其他類需要 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

這篇關于QT/C++ - 從不同的類訪問 MainWindow UI的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How can I read and manipulate CSV file data in C++?(如何在 C++ 中讀取和操作 CSV 文件數據?)
In C++ why can#39;t I write a for() loop like this: for( int i = 1, double i2 = 0; (在 C++ 中,為什么我不能像這樣編寫 for() 循環: for( int i = 1, double i2 = 0;)
How does OpenMP handle nested loops?(OpenMP 如何處理嵌套循環?)
Reusing thread in loop c++(在循環 C++ 中重用線程)
Precise thread sleep needed. Max 1ms error(需要精確的線程睡眠.最大 1ms 誤差)
Is there ever a need for a quot;do {...} while ( )quot; loop?(是否需要“do {...} while ()?環形?)
主站蜘蛛池模板: 澳门精准正版免费大全,2025新澳门全年免费,新澳天天开奖免费资料大全最新,新澳2025今晚开奖资料,新澳马今天最快最新图库 | 「安徽双凯」自动售货机-无人售货机-成人用品-自动饮料食品零食售货机 | 广州监控安装公司_远程监控_安防弱电工程_无线wifi覆盖_泉威安防科技 | 北京百度网站优化|北京网站建设公司-百谷网络科技 | MVE振动电机_MVE震动电机_MVE卧式振打电机-河南新乡德诚生产厂家 | 热闷罐-高温罐-钢渣热闷罐-山东鑫泰鑫智能热闷罐厂家 | 半自动预灌装机,卡式瓶灌装机,注射器灌装机,给药器灌装机,大输液灌装机,西林瓶灌装机-长沙一星制药机械有限公司 | 聚氨酯保温钢管_聚氨酯直埋保温管道_聚氨酯发泡保温管厂家-沧州万荣防腐保温管道有限公司 | 菏泽知彼网络科技有限公司| 防爆电机生产厂家,YBK3电动机,YBX3系列防爆电机,YBX4节防爆电机--河南省南洋防爆电机有限公司 | 旋振筛_不锈钢旋振筛_气旋筛_旋振筛厂家—新乡市大汉振动机械有限公司 | 视频直播 -摄影摄像-视频拍摄-直播分发 | 大_小鼠elisa试剂盒-植物_人Elisa试剂盒-PCR荧光定量试剂盒-上海一研生物科技有限公司 | 安徽千住锡膏_安徽阿尔法锡膏锡条_安徽唯特偶锡膏_卡夫特胶水-芜湖荣亮电子科技有限公司 | 冻干机(冷冻干燥机)_小型|实验型|食品真空冷冻干燥机-松源 | 贴片电容-贴片电阻-二三极管-国巨|三星|风华贴片电容代理商-深圳伟哲电子 | 铝箔袋,铝箔袋厂家,东莞铝箔袋,防静电铝箔袋,防静电屏蔽袋,防静电真空袋,真空袋-东莞铭晋让您的产品与众不同 | 电磁流量计_智能防腐防爆管道式计量表-金湖凯铭仪表有限公司 | 热熔胶网膜|pes热熔网膜价格|eva热熔胶膜|热熔胶膜|tpu热熔胶膜厂家-苏州惠洋胶粘制品有限公司 | 郑州爱婴幼师学校_专业幼师培训_托育师培训_幼儿教育培训学校 | 回转窑-水泥|石灰|冶金-巩义市瑞光金属制品有限责任公司 | 天津仓储物流-天津电商云仓-天津云仓一件代发-博程云仓官网 | 炭黑吸油计_测试仪,单颗粒子硬度仪_ASTM标准炭黑自销-上海贺纳斯仪器仪表有限公司(HITEC中国办事处) | 校园气象站_超声波气象站_农业气象站_雨量监测站_风途科技 | 上海刑事律师|刑事辩护律师|专业刑事犯罪辩护律师免费咨询-[尤辰荣]金牌上海刑事律师团队 | 杰福伦_磁致伸缩位移传感器_线性位移传感器-意大利GEFRAN杰福伦-河南赉威液压科技有限公司 | 铆钉机|旋铆机|东莞旋铆机厂家|鸿佰专业生产气压/油压/自动铆钉机 | FFU_空气初效|中效|高效过滤器_空调过滤网-广州梓净净化设备有限公司 | 退火炉,燃气退火炉,燃气热处理炉生产厂家-丹阳市丰泰工业炉有限公司 | 江苏南京多语种翻译-专业翻译公司报价-正规商务翻译机构-南京华彦翻译服务有限公司 | CTP磁天平|小电容测量仪|阴阳极极化_双液系沸点测定仪|dsj电渗实验装置-南京桑力电子设备厂 | 北京企业宣传片拍摄_公司宣传片制作-广告短视频制作_北京宣传片拍摄公司 | 轻型地埋电缆故障测试仪,频响法绕组变形测试仪,静荷式卧式拉力试验机-扬州苏电 | 耐驰泵阀管件制造-耐驰泵阀科技(天津)有限公司 | 伺服电机维修、驱动器维修「安川|三菱|松下」伺服维修公司-深圳华创益 | 南京泽朗生物科技有限公司| 三轴曲线机-端子插拔力试验机|华杰仪器 | [官网]叛逆孩子管教_戒网瘾学校_全封闭问题青少年素质教育_新起点青少年特训学校 | 桥架-槽式电缆桥架-镀锌桥架-托盘式桥架 - 上海亮族电缆桥架制造有限公司 | 自动售货机_无人售货机_专业的自动售货机运营商_免费投放售货机-广州富宏主官网 | 四探针电阻率测试仪-振实密度仪-粉末流动性测定仪-宁波瑞柯微智能 |