久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

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 ()?環形?)
主站蜘蛛池模板: 水蜜桃亚洲一二三四在线 | 日韩在线视频一区 | 久久国产精品99久久久久久丝袜 | 中文字幕日韩一区 | 一道本不卡 | 国产精品视频网 | 午夜成人免费视频 | 求毛片 | 欧美成人免费 | 亚洲精品综合 | 日本不卡免费新一二三区 | 精品久久久久国产免费第一页 | 欧美国产视频 | 免费一区在线 | 成人毛片网站 | 久久久久国产一区二区三区 | 最近日韩中文字幕 | 亚洲www | 视频在线一区二区 | 337p日本欧洲亚洲大胆精蜜臀 | 日韩成人精品在线观看 | 色婷婷国产精品 | 欧美一区2区三区4区公司 | 亚洲第一天堂 | 久久久国产一区 | aaa在线观看| 欧美日韩在线一区二区 | 免费麻豆视频 | 视频一区二区中文字幕日韩 | 91av视频在线播放 | 美女一区二区在线观看 | 欧美一区二区三区小说 | 中文字幕亚洲视频 | 日韩精品一区二区三区中文字幕 | 国产在视频一区二区三区吞精 | 成人一区在线观看 | 毛片99| 久久久久久国产精品 | 亚洲精品中文字幕在线观看 | 在线视频 欧美日韩 | 啪一啪在线视频 |