Akcja po kliknięciu anuluj

0

Witam, mam taki problem ponieważ zastanawiam się jak po wciśnięciu przycisku anuluj było zakmnięcie okienka. Kod:

#include <QtWidgets>

int main (int argc, char* argv[]) {                 /* Początek funkcji main zwracającej wartość int. */
    QApplication app(argc, argv);                   /* Początek każdej aplikacji okienkowej Qt. */
    QTextStream cout(stdout);                       /* Utworzenie strumienia standardowego wejścia QTextStream. */

    // Declarations of variables
    int answer = 0;                                 /* Zmienna answer musi zostać zdefiniowana 
			poza pętlą do, ponieważ została użyta w warunku poza blokiem do. */

    do {
       powtorz:
        // zmienne lokalne pętli
        int factArg = 0;
        int fact(1);
        bool ok;
        factArg = QInputDialog::getInt(0, "Kalkulator silni",
            "Factorial of:", 1, &ok);                    /* Wyskakujące okienko,
				które czeka, aż użytkownik poda wartość całkowitą, a następnie ją zwraca.  */
        cout << "User entered: " << factArg << endl;
        int i=2;
        if (factArg > 0){
        while (i <= factArg) {
            fact *= i;
            ++i;
        }
        }
        else
            goto powtorz;
        QString response = QString("%1 silnia to %2.\n%3")
            .arg(factArg).arg(fact)                 /* Każde %n zostanie zastąpione wartością arg(). */
            .arg("Wyliczymy inną silnię?");     	/* Długie instrukcje można dzielić na linie, 
					pod warunkiem że zostaną przerwane na granicy tokenów („słów”). */
        answer = QMessageBox::question(0, "Jeszcze raz?", response,
            QMessageBox::Yes | QMessageBox::No);    /* Operator bitowy OR (lub). */
    } while (answer == QMessageBox::Yes);

    return EXIT_SUCCESS;
}

w dokumentacji wyczytałem:

int QInputDialog::getInt ( QWidget * parent, const QString & title, const QString & label, int value = 0, int min = -2147483647, int max = 2147483647, int step = 1, bool * ok = 0, Qt::WindowFlags flags = 0 ) [static]
Static convenience function to get an integer input from the user.

title is the text which is displayed in the title bar of the dialog. label is the text which is shown to the user (it should say what should be entered). value is the default integer which the spinbox will be set to. min and max are the minimum and maximum values the user may choose. step is the amount by which the values change as the user presses the arrow buttons to increment or decrement the value.

If ok is nonnull *ok will be set to true if the user pressed OK and to false if the user pressed Cancel. The dialog's parent is parent. The dialog will be modal and uses the widget flags.

On success, this function returns the integer which has been entered by the user; on failure, it returns the initial value.
Use this static function like this:

 bool ok;
 int i = QInputDialog::getInt(this, tr("QInputDialog::getInteger()"),
                              tr("Percentage:"), 25, 0, 100, 1, &ok);
 if (ok)
     integerLabel->setText(tr("%1%").arg(i));

Warning: Do not delete parent during the execution of the dialog. If you want to do this, you should create the dialog yourself using one of the QInputDialog constructors.

This function was introduced in Qt 4.5.

I nie wiem jak użyć tego boola w QInputDialog.

btw. wiem, goto jest fe ale nie chcę teraz kombinować inaczej tylko lecieć do przodu.

0

Już go użyłeś, teraz sprawdź jego wartość po wykonaniu dialoga.

0
#include <QtWidgets>

int main (int argc, char* argv[]) {                 /* Początek funkcji main zwracającej wartość int. */
    QApplication app(argc, argv);                   /* Początek każdej aplikacji okienkowej Qt. */
    QTextStream cout(stdout);                       /* Utworzenie strumienia standardowego wejścia QTextStream. */

    // Declarations of variables
    int answer = 0;
    bool ok;            /* Zmienna answer musi zostać zdefiniowana
			poza pętlą do, ponieważ została użyta w warunku poza blokiem do. */

    do {
       powtorz:
        // zmienne lokalne pętli
        int factArg = 0;
        int fact(1);

        factArg = QInputDialog::getInt(0, "Kalkulator silni",
            "Factorial of:", 1,0,100,1,&ok);                    /* Wyskakujące okienko,
				które czeka, aż użytkownik poda wartość całkowitą, a następnie ją zwraca.  */

        cout << "User entered: " << factArg << endl;
        if(ok){
        int i=2;
        if (factArg > 0){
        while (i <= factArg) {
            fact *= i;
            ++i;
        }
        }
        else
            goto powtorz;
        QString response = QString("%1 silnia to %2.\n%3")
            .arg(factArg).arg(fact)                 /* Każde %n zostanie zastąpione wartością arg(). */
            .arg("Wyliczymy inną silnię?");     	/* Długie instrukcje można dzielić na linie, 
					pod warunkiem że zostaną przerwane na granicy tokenów („słów”). */
        answer = QMessageBox::question(0, "Jeszcze raz?", response,
            QMessageBox::Yes | QMessageBox::No);    /* Operator bitowy OR (lub). */
        }
        else
            goto nq;

    } while (answer == QMessageBox::Yes);
    nq:
    return EXIT_SUCCESS;
}


A jak to zrobić w jakiś ładny sposób? bo poza goto nie mam pomysłu. Próbowałem w pętli do...while dorzucić warunek typu || ok == true ale nie działa

0
if(!ok) continue;
0

mógłbyś to wrzucić w mój kod? bo jakoś mi nie wychodzi.

0
#include <QtWidgets>

int main(int argc, char* argv[])
{ /* Początek funkcji main zwracającej wartość int. */
    QApplication app(argc, argv); /* Początek każdej aplikacji okienkowej Qt. */
    QTextStream cout(stdout); /* Utworzenie strumienia standardowego wejścia QTextStream. */

    // Declarations of variables
    int answer = 0;
    bool ok; /* Zmienna answer musi zostać zdefiniowana
            poza pętlą do, ponieważ została użyta w warunku poza blokiem do. */

    do {
        // zmienne lokalne pętli
        int factArg = 0;
        int fact(1);

        factArg = QInputDialog::getInt(0, "Kalkulator silni",
                                       "Factorial of:", 1, 0, 100, 1, &ok); /* Wyskakujące okienko,
                które czeka, aż użytkownik poda wartość całkowitą, a następnie ją zwraca.  */

        cout << "User entered: " << factArg << endl;
        if (!ok) {
            continue;
        }

        int i = 2;
        if (factArg > 0) {
            while (i <= factArg) {
                fact *= i;
                ++i;
            }

            QString response = QString("%1 silnia to %2.\n%3")
                                   .arg(factArg)
                                   .arg(fact) /* Każde %n zostanie zastąpione wartością arg(). */
                                   .arg("Wyliczymy inną silnię?"); /* Długie instrukcje można dzielić na linie, 
                    pod warunkiem że zostaną przerwane na granicy tokenów („słów”). */
            answer = QMessageBox::question(0, "Jeszcze raz?", response,
                                           QMessageBox::Yes | QMessageBox::No); /* Operator bitowy OR (lub). */
        } else {
            break;
        }

    } while (answer == QMessageBox::Yes);
    return EXIT_SUCCESS;
}

Miałeś ciut nieczytelną indentację, mam nadzieję, że nie zmieniłem sensu programu.

0

Właśnei w tym problem, że jak policzę pare razy silnię i dopiero po tym chcę wcisnąć anuluj to pojawia się problem bo po wciśnięciu przycisku anuluj znika na ułamek sekundy okienko po czym pojawia się to samo i przycisk nie spełnia swojej funkcji.

2

Zamień w kodzie podanym przez @kq słowo continue na break.

1 użytkowników online, w tym zalogowanych: 0, gości: 1