[SOLVED]QFile and QTextStream Question
-
Hi all,
I would like to get my Qslider position value and store in a txt file. i did as following, my i could not obtain any value.
@int h_slide1 = ui->hueSlide1->value();
QFile valueHSV("/home/pi/valueHSV/hsv.txt")
if(!valueHSV.open(QIODevice::WriteOnly| QIODevice::Text))
return;
QTextStream hsv(&valueHSV);
hsv << h_slide1;@is this code correct?
-
Did u check whether file is created ? Did you print the value of slider ? Can you close valueHSV.close at the end ?
-
[quote author="Dheerendra" date="1404193098"]Did u check whether file is created ? Did you print the value of slider ? Can you close valueHSV.close at the end ? [/quote]
Did u check whether file is created ?
A: I did create manually
Did you print the value of slider ?
A: do you mean by " qDebug() << h_slide1 ; " ?
Can you close valueHSV.close at the end ?
A: Close? do i need to do close at the end? Ermm.. -
[quote author="IamSumit" date="1404195583"]hii
you didn't close the file so that's why you are not obtaining any value.
just add a line at the end.
@
valueHSV.close();
@
[/quote]Hi Sumit,
after added in this line to close it,
I make and run, the drag the slider in GUI, the txt file didn't have any value too, it is blank, i think i did wrongly, but i dont know what's wrong .... -
Replace your code with following and let me know if you see any debug output. If you don't see any debug output, for sure, you have file permission issue.
@ int h_slide1 = ui->hueSlide1->value();
QFile valueHSV("/home/pi/valueHSV/hsv.txt");
if(!valueHSV.open(QIODevice::WriteOnly| QIODevice::Text)){
qDebug() << "File not able to open" << endl;
return;
}
QTextStream hsv(&valueHSV);
hsv << h_slide1;
qDebug() << "Value ="<<h_slide1
valueHSV.close();
@ -
[quote]
@ int h_slide1 = ui->hueSlide1->value();
QFile valueHSV("/home/pi/valueHSV/hsv.txt");
if(!valueHSV.open(QIODevice::WriteOnly| QIODevice::Text)){
qDebug() << "File not able to open" << endl;
return;
}
QTextStream hsv(&valueHSV);
hsv << h_slide1;
qDebug() << "Value ="<<h_slide1
valueHSV.close();
@
[/quote]
in order to get slider value you should write above code inside on_horizontalSlider_valueChanged() slot. -
Hii.
void QAbstractSlider::valueChanged ( int value ) [signal]
This signal is emitted when the slider value has changed, with the new slider value as argument.
http://qt-project.org/doc/qt-4.8/qabstractslider.html#valueChanged
This is the signal you need to connect with your slot .just like when you connect your QPushButton's clicked signal to it's Slot_clicked();
hope you know signal and slot
. -
because i am now writing this in a function
:
@void Dialog::getValue()
{
int h_slide1 = ui->hueSlide1->value();
QFile valueHSV("/home/pi/valueHSV/hsv.txt");
if(!valueHSV.open(QIODevice::WriteOnly| QIODevice::Text)){
qDebug() << "File not able to open" << endl;
return;
}
QTextStream hsv(&valueHSV);
hsv << h_slide1;
qDebug() << "Value ="<<h_slide1
valueHSV.close();
}@ -
hi
you can take a reference from the following link
http://www.codeprogress.com/cpp/libraries/qt/qProgressbarSetValueExample.php#.U7J--ZSSwb8[Edited: Remember it is an old example using 4.7]