問(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.
QApplication
對(duì)象實(shí)際上存儲(chǔ)了所有頂級(jí)窗口,例如您的MainGame
,這意味著您始終可以通過(guò)QApplication::topLevelWidgets()
這是一個(gè)靜態(tài)函數(shù)并返回一個(gè)包含所有頂級(jí)小部件的列表.由于您只有一個(gè),因此第一個(gè)是您的MainGame
.缺點(diǎn)是你必須投射它,但使用 Qtsqobject_cast
是相當(dāng)安全的.您必須檢查結(jié)果以確保它不是 NULL 指針.(...)
The
QApplication
object actually stores all top level windows like yourMainGame
which means you can allways aquire it throughQApplication::topLevelWidgets()
which is a static function and returns a list with all top level widgets. Since you only have one, the first one is yourMainGame
. The drawback is you'll have to cast it, but using Qtsqobject_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)!