PDA

View Full Version : progressbar generate by the label value



CAFFRY
28th December 2016, 10:54
guys i need the progress bar to vary the values...(i.e)i have a label which shows the random numbers. i need the signal from the label to the progressbar...
each time the number changes the progress bar must change the value..

help me with a code..

daud anjum
29th December 2016, 13:27
go to "edit signals/slots" and connect your label to the progress bar by clicking on the label and dragging the cursor to the progress bar. then choose "ValueChanged(int)" and "SetValue(int)" then click OK.

I don't know much of Qt but this should work:):o.

d_stranz
29th December 2016, 18:14
I don't know much of Qt but this should work

No, it won't work. QLabel does not have a signal called "ValueChanged()" (and QProgressBar doesn't have a slot named "SetValue()", but it -does- have one named "setValue()").


each time the number changes the progress bar must change the value.

How are you setting the number as the text for the label? Using a signal emitted by the part of your program that generates the "random numbers"? If so, then if the signal is sending an "int" argument, simply connect this signal to the progress bar's setValue() slot. If your signal sends a QString, then you can implement your own slot that converts the string into a number (QString::toInt() or QString::toLong()) and then calls setValue() directly on the progress bar instance.

CAFFRY
30th December 2016, 03:47
this is my random code...


srand ( time(NULL));
int randnumber = rand() %100 + 1;

for(int i=0;i>=100;i++)
{
i = qrand();
}
QString str;
str.append(QString("%1").arg(randnumber));
ui->percentage->setText(str + "%");
if(randnumber<0)
{
QMessageBox::about(this,"error","runagain");
}

i need this to connect with the progressbar...
it would me nice if u send me the code for that connection

d_stranz
30th December 2016, 04:36
for(int i=0;i>=100;i++)
{
i = qrand();
}


What the heck is this code supposed to do? It's nonsense, and in any case you shouldn't change the value of the loop iterator (i) inside the loop.


i need this to connect with the progressbar...

So how hard is that? You have an int random number, QProgressBar::setValue() is a method that takes an int argument, so if you have a UI widget named "progress" or something like that... You figured out how to set the text on the label, you should be able to figure out how to set the value on the progress bar.


if(randnumber<0)

Integers returned by the rand() function are always between 0 and RAND_MAX, so this statement will never be true. And your random numbers will always be between 1 and 100 because of the modulus operation (%) you do on the value returned by rand().

CAFFRY
30th December 2016, 05:40
can u write me a code for that, for the connection between the progressbar and the label value.

and use the random code which i used ...plzzz..
and thanks for the rply

anda_skoa
30th December 2016, 10:15
can u write me a code for that, for the connection between the progressbar and the label value.

You don't need a connection, you can call the progress bar's setValue() just like you call the label's setText().

Cheers,
_

CAFFRY
30th December 2016, 11:10
its not working

d_stranz
30th December 2016, 16:47
its not working

That doesn't tell us much. Post your latest code and we will take a look.

By the way, this forum is not here to write your code for you. Everyone here is willing to help, but only if you try first and get stuck. We expect you to read the Qt documentation, look at the Qt tutorial and example programs, and try to solve problems yourself first. There are plenty of places in the Qt examples where basic widgets like QProgressBar and QLabel are used. If you study the examples, you might find how to do exactly what you need.