2009-09-12 4 views
2
#include <QtGui> 

int main (int argc, char* argv[]) 
{ 
    QApplication app(argc, argv); 
    QTextStream cout(stdout, QIODevice::WriteOnly);  

    // Declarations of variables 
    int answer = 0; 

    do { 
     // local variables to the loop: 
     int factArg = 0; 
     int fact(1); 
     factArg = QInputDialog::getInteger(0, "Factorial Calculator", 
      "Factorial of:"); 
     cout << "User entered: " << factArg << endl; 
     int i=2; 
     while (i <= factArg) { 
      fact = fact * i; 
      ++i; 
     } 
     QString response = QString("The factorial of %1 is %2.\n%3") 
      .arg(factArg).arg(fact) 
      .arg("Do you want to compute another factorial?");  
     answer = QMessageBox::question(0, "Play again?", response, 
      QMessageBox::Yes | QMessageBox::No ,QMessageBox::Yes); 
    } while (answer == QMessageBox::Yes); 

    return EXIT_SUCCESS; 
} 

이 프로그램에서는 입력 창 (do-while 루프의 4 번째 줄)에 취소 단추가 없습니다. 어떻게해야합니까? 방금 ​​QT를 배우기 시작했습니다. 죄송합니다. 매우 근본적인 질문 인 경우.QInputDialog에서 "취소"버튼을 숨기려면 어떻게해야합니까?

또한 취소 버튼을 사용하여 응용 프로그램을 중지하는 방법은 무엇입니까? Bcos, 지금 CANCEL 버튼은 아무 것도하지 않습니다.

답변

5

QInputDialog는 입력을 요청할 수있는 빠르고 쉬운 방법을 제공하는 편의 클래스로 제공되며 많은 맞춤 설정을 허용하지 않습니다. 문서 레이아웃에서 윈도우의 레이아웃을 변경할 수 있다는 것을 알 수는 없습니다. QDialog를 확장하여 자신의 대화 상자를 설계하는 것이 좋습니다. 이렇게하는 데는 시간이 더 걸리지 만 양식을 사용자 정의 할 수 있습니다.

QInputDialog에서 취소 버튼을 눌렀는지 확인하려면 bool에 대한 포인터를 getInteger() 함수에 8 번째 인수로 전달해야합니다.

do{ 
    bool ok; 
    factArg = QInputDialog::getInteger(0, "Factorial Calculator", "Factorial of:", 
             value, minValue, maxValue, step, &ok); 
    if(!ok) 
    { 
     //cancel was pressed, break out of the loop 
     break; 
    } 
    // 
    // Do your processing based on input 
    // 
} while (some_condition); 

ok가 false로 반환되면 사용자가 취소를 클릭하고 루프를 벗어날 수 있습니다. 당신은 MINVALUE는 maxValue를이 단계는 문서에 무엇을 의미하는지 값을 볼 수 있습니다 QInputDialog documentation

+0

고맙습니다. 제이슨. 나는 내가 원하는 것을 얻었다. 또한, 나는이 bool * 매개 변수 개념을 제거했습니다. 실제로, 그것은 학습의 단지 두 번째 날입니다. –

0

가 QInputDialog의 도움말 버튼이 올바른 windowFlags 전달하여 작동 숨기기 : Qt는 디자이너의 속성 편집기에서

QInputDialog inputDialog; 
bool ok; 
inputDialog.setWindowFlags(inputDialog.windowFlags() & (~Qt::WindowContextHelpButtonHint)); 
QString text = inputDialog.getText(this, tr("Factorial Calculator"), 
          tr("Enter some text:"), QLineEdit::Normal, QString(), &ok, 
          inputDialog.windowFlags()); 

// Or for integers 
int number = inputDialog.getInt(this, tr("Fractorial Calculator"), 
         tr("Enter a number:"), 0, -10, 10, 1, &ok, 
         inputDialog.windowFlags()); 
0

을, 당신은 standardButtons 속성을 사용자 정의 할 수 있습니다 -

enter image description here

이는 대화 버튼이 발표되고있는 제어 할 수 있도록해야한다.

관련 문제