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

QT/C++ - 從不同的類訪問(wèn) MainWindow UI

QT/C++ - Accessing MainWindow UI from a different class(QT/C++ - 從不同的類訪問(wèn) MainWindow UI)
本文介紹了QT/C++ - 從不同的類訪問(wèn) MainWindow UI的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

我是 C++ 和 Qt 的初學(xué)者,所以這可能是微不足道的.這當(dāng)然感覺(jué)應(yīng)該很簡(jiǎn)單,但我一直在尋找答案幾個(gè)小時(shí),但找不到解決方案.我正在制作一個(gè)簡(jiǎn)單的棋盤(pán)游戲,其中 MainWindow 的 ui(在 QtDesigner 中制作)包含游戲棋盤(pán)的畫(huà)布(QGraphicsView).現(xiàn)在,main.cpp 非常簡(jiǎn)單:

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();
}

由于我需要從另一個(gè)完全不相關(guān)的類訪問(wèn)和編輯 MainWindow 小部件,我認(rèn)為最簡(jiǎn)單的方法是將 MainWindow 設(shè)為全局變量.不過(guò),這種方法似乎是非常錯(cuò)誤的.嘗試在 QtDesigner 中運(yùn)行項(xiàng)目時(shí),我收到 Microsoft Visual C++ 運(yùn)行時(shí)庫(kù)錯(cuò)誤:應(yīng)用程序已請(qǐng)求運(yùn)行時(shí)以異常方式終止它.

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 之外,我還有一個(gè)新游戲的對(duì)話框(QDialog,從 QtDesigner 生成),在 MainWindow 中單擊菜單項(xiàng)后會(huì)顯示該對(duì)話框.當(dāng)用戶輸入游戲的所有參數(shù)并在對(duì)話框中單擊確定"時(shí),我會(huì)實(shí)例化一個(gè)名為 GameState 的自定義非 Qt 類.這個(gè)類的目的是操作游戲本身,畫(huà)板,提示用戶等. 但是,由于這個(gè)類是在 QDialog 中創(chuàng)建的,它不知道 MainWindow 的存在,所以我不能對(duì) MainWindow 做任何事情從這個(gè)班級(jí).那么如何從不相關(guān)的類修改 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() 函數(shù)是如何工作的?它似乎永遠(yuǎn)不會(huì)做任何事情.我在 QtDesigner 中設(shè)置為禁用的任何小部件然后嘗試通過(guò)此功能啟用仍然在 GUI 中保持禁用狀態(tài)...

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...

推薦答案

首先,在創(chuàng)建 QApplication 對(duì)象之前創(chuàng)建 MainGame 是個(gè)壞主意.如果你想讓你的 MainGame 對(duì)象像這樣全局可用,它應(yīng)該是一個(gè)指針:

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;
}

然而,這種方法并不是最優(yōu)雅的.有兩個(gè)更好的選擇.

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

  1. QApplication 對(duì)象實(shí)際上存儲(chǔ)了所有頂級(jí)窗口,例如您的 MainGame,這意味著您始終可以通過(guò) QApplication::topLevelWidgets() 這是一個(gè)靜態(tài)函數(shù)并返回一個(gè)包含所有頂級(jí)小部件的列表.由于您只有一個(gè),因此第一個(gè)是您的 MainGame.缺點(diǎn)是你必須投射它,但使用 Qts qobject_cast(...) 是相當(dāng)安全的.您必須檢查結(jié)果以確保它不是 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.

使用單例設(shè)計(jì)模式.您應(yīng)該將全局 Game 指針存儲(chǔ)在 Game 類本身(子類 QMainWindow)的源 (cpp) 文件中,并且您的 Game 類應(yīng)該實(shí)現(xiàn)一個(gè)返回此全局指針的靜態(tài)公共方法.因此,如果任何其他類需要 Game 指針,它只需調(diào)用:

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();

例如.

關(guān)于您的 setEnabled() 問(wèn)題.請(qǐng)貼出相關(guān)代碼.如果太多,請(qǐng)通過(guò)郵件向我發(fā)送 *.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.

最好的問(wèn)候
D

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

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

相關(guān)文檔推薦

How can I read and manipulate CSV file data in C++?(如何在 C++ 中讀取和操作 CSV 文件數(shù)據(jù)?)
In C++ why can#39;t I write a for() loop like this: for( int i = 1, double i2 = 0; (在 C++ 中,為什么我不能像這樣編寫(xiě) for() 循環(huán): for( int i = 1, double i2 = 0;)
How does OpenMP handle nested loops?(OpenMP 如何處理嵌套循環(huán)?)
Reusing thread in loop c++(在循環(huán) C++ 中重用線程)
Precise thread sleep needed. Max 1ms error(需要精確的線程睡眠.最大 1ms 誤差)
Is there ever a need for a quot;do {...} while ( )quot; loop?(是否需要“do {...} while ()?環(huán)形?)
主站蜘蛛池模板: 四虎影音| 亚洲综合五月天婷婷丁香 | 黄色网在线 | 日韩aaaa| 色天堂视频 | 少妇一级淫片免费观看 | 欧美在线激情 | 日韩精品久久久久久免费 | 一区二区免费 | 国产美女福利 | 久久免费高清视频 | 五月色丁香| 性欧美8khd高清极品 | 欧美特黄| 黄色片aaa| 久久久久久久久久久国产 | 亚洲 欧美 日韩 在线 | 亚洲精品国产精品国自产观看 | 亚洲精品乱码久久久久久动漫 | 91视频亚洲 | 婷婷综合视频 | 青青草原国产 | 午夜黄色小视频 | 国产女人高潮视频 | 黄在线免费观看 | 欧美片网站免费 | 欧美在线视频播放 | 精品1区2区 | 日韩亚洲欧美在线观看 | 超碰精品在线 | 亚洲一区免费 | 黄色一区二区三区 | 97视频免费 | 在线免费黄色 | 日本一级淫片色费放 | 精品视频一区二区三区四区 | 久久精品99久久久久久 | 二区三区在线观看 | 色婷婷18 | 精品免费在线观看 | 日本国产在线观看 |