Make a simple answer interface with QT

Keywords: Qt

After learning the QT interface for two days, today the teacher assigned the homework to let us use QT to make a simple single answer interface

Similar to the king of mind in wechat applications, answer one question every 10 seconds, a total of 5 questions, and finally output the score.

Here are my own steps, interfaces and procedures: (try to be as simple as possible)

First open QT and create a new application

Create designer interface class after creation

Insert lcdNumber for countdown, staked widget for page switching, required text (Label), etc. in the interface.

This is roughly the case. Then add a page in the Stacked widget. The interface is the same.

Next is the code

First, add a container to the header file to store the single choice questions and add a time parameter

The main function does not move. The code in the widget is as follows:

#include "widget.h"
#include "ui_widget.h"
#include <QPixmap>
#include <QMovie>
#include <QDebug>
#include <QMessageBox>

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);


    v.push_back(ui->radioButton_5);          //Preset 5 answers first
    v.push_back(ui->radioButton_11);
    v.push_back(ui->radioButton_14);
    v.push_back(ui->radioButton_17);
    v.push_back(ui->radioButton_8);

    ui->lcdNumber->display(10);  //The initial number is 10


    //ui->progressBar->setValue(100);

    timerId = startTimer(1000);   // 1s
}

Widget::~Widget()
{
    delete ui;
}

void Widget::timerEvent(QTimerEvent *event)
{
    static int i = 1;
    static int k = 0;   //Counter
    int score = 0;      //Score
    ui->lcdNumber->display(ui->lcdNumber->value() - 1);    //Decreasing time
    if (ui->lcdNumber->value() == 0)                       //0s time
    {
        k++;                                               //Count + 1
        ui->lcdNumber->display(10);                        //Time back to 10s
        ui->stackedWidget->setCurrentIndex(i++);           //Stack widget cut to next page
    }
    for (unsigned int i = 0; i< v.size(); i++)
    {
        if (v[i]->isChecked())                             //When the options are correct
            score++;                                       //Score + 1
    }
    if(k == 5)                                             //Output after 5 runs
    {
        QString str = QString("congratulations! You're right %1 Question!").arg(score);

        QMessageBox::information(this, "Score", str);

        killTimer(this->timerId);                           //Stop time
    }
}

The final operation is as follows:

Posted by user___ on Tue, 05 May 2020 07:58:31 -0700