D
D
dimoSSHka2019-06-09 19:01:09
C++ / C#
dimoSSHka, 2019-06-09 19:01:09

How to implement number dependency on ProgressBar in qt creator?

The bottom line is that there is a progress bar. And I need to change the speed variable in different parts of the progress bar up to 50%. For example, the progress bar is 16% and the text has changed (it doesn’t matter which one since it’s implemented) , then for example this green bar reached 32 and the text changed again. How to do all this correctly?
I'll attach the code and screenshots. Thank you very much in advance.5cfd2cd11c682604266350.png5cfd2cd51a7dd302560216.jpeg

#include "usbtest.h"
#include "ui_usbtest.h"
#include "QFile"
#include "QDebug"
#include "QTimer"
#include <QStorageInfo>
#include <QString>
#include "cstring"
#include "string.h"
#include "QWidget"

using namespace std;

long long allbyte;
int first_speed = 0;

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

    QFile file("path.txt");
    file.open(QIODevice::ReadOnly);
    QString file_path = file.readLine();
    file.close();

    QFile speed("speed.txt");
    speed.open(QIODevice::ReadOnly);
    first_speed = speed.readLine().toInt();
    speed.close();

    QStorageInfo storage(file_path);
    allbyte = storage.bytesTotal()/1024/1024;

    ui->progressBar->setMaximum(allbyte*2);

    QTimer* timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()), this, SLOT(onTimeout()));
    timer->start(1);

}

usbtest::~usbtest()
{
    delete ui;
}
int i = 0;
long long local_byte;

double my_rand() {
    double a = 0;
    a = qrand() % ((first_speed*10 + 9 + 1) - (first_speed-1)*10) + (first_speed-1)*10;
    return a/10;
}


void usbtest::onTimeout()
{

    if(local_byte != allbyte)
    {
        string lstrbyte = to_string(local_byte);
        ui->size->setText(QString::fromStdString(lstrbyte) + " MByte");
        ui->progressBar->setValue(++i);
        local_byte++;
        if(ui->progressBar->value() ==  qrand() % ((5 + 1) - 0) + 0)
        {
            ui->speed->setText((QString::number(my_rand())));
        }

        else if (ui->progressBar->value() ==  qrand() % ((15 + 1) - 10) + 10) {
            ui->speed->setText((QString::number(my_rand())));
        }
        else if (ui->progressBar->value() ==  qrand() % ((23 + 1) - 10) + 20) {
            ui->speed->setText((QString::number(my_rand())));
        }
        else if (ui->progressBar->value() ==  qrand() % ((40 + 1) - 10) + 35) {
            ui->speed->setText((QString::number(my_rand())));
        }
        else if (ui->progressBar->value() ==  qrand() % ((49 + 1) - 10) + 39) {
            ui->speed->setText((QString::number(my_rand())));
        }
        else {
            ui->speed->setText("Ты лох, нихера не рабботает");
        }

    }

    else {
        ui->size->setText("Ти лох");

    }
}

-------------------------------------------------- -------------------------------------------------- --------------------
HERE IS THE CODE WHERE I HAVE A JOB AND WHERE THE TIMER IS IMPLEMENTED (WHAT WOULD NOT LOOK FOR LONG)
void usbtest::onTimeout()
{

    if(local_byte != allbyte)
    {
        string lstrbyte = to_string(local_byte);
        ui->size->setText(QString::fromStdString(lstrbyte) + " MByte");
        ui->progressBar->setValue(++i);
        local_byte++;
        if(ui->progressBar->value() ==  qrand() % ((5 + 1) - 0) + 0)
        {
            ui->speed->setText((QString::number(my_rand())));
        }

        else if (ui->progressBar->value() ==  qrand() % ((15 + 1) - 10) + 10) {
            ui->speed->setText((QString::number(my_rand())));
        }
        else if (ui->progressBar->value() ==  qrand() % ((23 + 1) - 10) + 20) {
            ui->speed->setText((QString::number(my_rand())));
        }
        else if (ui->progressBar->value() ==  qrand() % ((40 + 1) - 10) + 35) {
            ui->speed->setText((QString::number(my_rand())));
        }
        else if (ui->progressBar->value() ==  qrand() % ((49 + 1) - 10) + 39) {
            ui->speed->setText((QString::number(my_rand())));
        }
        else {
            ui->speed->setText("Ты лох, нихера не рабботает");
        }

    }

    else {
        ui->size->setText("Ти лох");

    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Ronald McDonald, 2019-06-09
@dimoSSHka

Use the progress bar signal valueChanged(int) , hook to it via the connect function, which will change the text.
https://doc.qt.io/qt-5/qprogressbar.html#valueChanged

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question