1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
|
#include <QMainWindow>
#include <QApplication>
#include <QLabel>
#include <QPushButton>
#include <QVBoxLayout>
#include <QWidget>
#include <QInputDialog>
class Form1 : public QMainWindow
{
Q_OBJECT
public:
explicit Form1(QWidget *parent = 0) : QMainWindow(parent) // Stuff to draw the window
{
centralWidget = new QWidget(this);
verticalLayout = new QVBoxLayout(centralWidget);
Button1 = new QPushButton(centralWidget);
label1 = new QLabel(centralWidget);
Button1->setObjectName(QString::fromUtf8("Button1"));
Button1->setText(QApplication::translate("Form1", "Push me", 0, QApplication::UnicodeUTF8));
verticalLayout->addWidget(Button1);
verticalLayout->addWidget(label1);
this->setCentralWidget(centralWidget);
QMetaObject::connectSlotsByName(this);
}
private slots:
void on_Button1_clicked()
{
Numberhidden = rand() % 100 + 1;
Numberguess = QInputDialog::getInteger(this, "", "Please enter your first guess", 50, 1, 100);
while (Numberhidden != Numberguess && mycounter < 5)
{
if (Numberhidden > Numberguess)
Numberguess = QInputDialog::getInteger(this, "", "Try a higher number, and enter your new guess", 0, 1, 100);
else if (Numberhidden < Numberguess)
Numberguess = QInputDialog::getInteger(this, "", "Try a lower number, and enter your new guess", 0, 1, 100);
++mycounter;
}
if (Numberhidden == Numberguess)
label1->setText("You guessed it correct");
else
label1->setText("You have run out of opportunites");
}
private:
QWidget* centralWidget;
QVBoxLayout* verticalLayout;
QPushButton* Button1;
QLabel* label1;
int Numberhidden;
int Numberguess;
int mycounter = 0;
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Form1 w;
w.show();
return a.exec();
}
|