2011-05-28 53 views
-1

我試圖找出這個錯誤。我有一個使用Qt Creator進行的簡單應用程序。SLOT問題/ C++

我有三個按鈕,其中2個未啓用。然後,當按下第一個按鈕時,我想讓它們可見,但是當我按下按鈕時,Windows錯誤發生:「程序停止工作」。該程序編譯並執行其他任何操作。

QPushButton *dealButton = new QPushButton(tr("Deal cards")); 
dealButton->show(); 

QPushButton *hitButton = new QPushButton(tr("HIT")); 
hitButton->show(); 
hitButton->setEnabled(false); 

QPushButton *standButton = new QPushButton(tr("STAND")); 
standButton->show(); 
standButton->setEnabled(false); 

... 
connect(dealButton, SIGNAL(clicked()), this, SLOT(dealCards())); 

... 
void MainWindow::dealCards() 
{ 
hitButton->setEnabled(true); 
standButton->setEnabled(true); 
} 

那就是代碼。

回答

4

問題是,您正在重新聲明dealButton和其他人在您的構造函數(或任何函數,它是你所顯示的new調用)。

你應該在你的類定義:

private: // probably 
    QPushButton *dealButton; 

而且在構造函數或GUI初始化代碼:

dealButton = new QPushButton(...); // note: not QPushButton *dealButton = ... 

現在你已經創造什麼叫dealButton新的變量,它是本地的該範圍(功能)。該變量隱藏(掩蔽)班級的成員。

+0

謝謝你的工作! – Henry 2011-05-28 12:48:39