كيفية إخفاء "إلغاء" الموجود في QInputDialog في QT باستخدام C ++؟

StackOverflow https://stackoverflow.com/questions/1414043

  •  06-07-2019
  •  | 
  •  

سؤال

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

في هذا البرنامج، أنا لا أميل أن يكون نافذة الإدخال (خط 4th من حلقة افعل الوقت) أن يكون إلغاء زر. كيف يمكنني فعل ذلك؟ لقد بدأت للتو تعلم QT. لذلك، آسف إذا به في السؤال الأساسي جدا.

وأيضا كيف يمكنني الاستفادة من إلغاء زر لإيقاف التطبيق .. بكس، إلغاء الآن زر لا يفعل شيئا.

هل كانت مفيدة؟

المحلول

ويعطى QInputDialog كطبقة الراحة التي يوفر وسيلة سهلة وسريعة لطلب مدخلا وعلى هذا النحو، لا يسمح لكثير التخصيص. أنا لا أرى أي شيء في وثائق تشير إلى أنه يمكنك تغيير نسق من النافذة. أود أن أقترح مجرد تصميم الحوار الخاص بك من خلال توسيع QDialog. هذا وسوف يستغرق مزيدا من الوقت، ولكن سوف تسمح لك لتخصيص النموذج.

إذا كنت لا تريد لتحديد ما إذا تم الضغط على زر إلغاء في QInputDialog، يجب تمرير مؤشر إلى منطقي في وظيفة getInteger () كوسيطة 8TH.

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

إذا العوائد موافق كما كاذبة، قام المستخدم بالنقر فوق إلغاء ويمكنك الخروج من حلقة الخاص بك. يمكنك ان ترى ما قيمة، MINVALUE، MAXVALUE، وخطوة تعني في وثائق: وثائق QInputDialog

نصائح أخرى

وإخفاء زر مساعدة في QInputDialog يعمل عن طريق تمرير windowFlags الصحيحين:

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

في محرر الملكية كيو تي مصمم، يمكنك تخصيص الملكية standardButtons -

وهذا يجب أن تسمح لك التحكم التي يتم عرض أزرار الحوار.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top