[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..



  • 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 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();
    @



  • Hi
    Thanks for your reply,

    I use " make " command in LXTerminal,
    and i din see and debug output.

    i din see any "File not able to open" nor " Value = "



  • i build and run after i do some change in Run Setting,
    it show: Cannot connect creatot comm socket /tmp/qt_temp.VO2750/stub-socket: No such file or directory
    Press



  • after i uncheck the " Run in Terminal"
    it can Run but no any Application Output



  • [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.



  • hi Lam,

    Could you please describe more?
    I am too new to Qt ..



  • 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
    .



  • I try it now,
    Feel like, i am going to fail my school Final Year Project ...



  • So, i need to create another .cpp and .h file for this?



  • 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();
    }@



  • yes you can add another .h and .cpp file .inheriting QWidget class.



  • i need to use MainWindow or Dialog?



  • the function in .h file should be under signals?



  • 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]



  • ermm.. this link not working..



  • The example i can understand, but whn the slider move, can i save the current value to a txt file?



  • Hi Lam,

    I dont think your suggestion could work, i could not call ui into the function.



  • [quote author="YDYD" date="1404207347"]ermm.. this link not working..[/quote]
    link is perfectly working.

    [quote author="YDYD" date="1404207723"]The example i can understand, but whn the slider move, can i save the current value to a txt file?[/quote]

    yes that is why this signal is given.

    [quote author="YDYD" date="1404207829"]Hi Lam,

    I dont think your suggestion could work, i could not call ui into the function.[/quote]

    the example does not use Qt designer Widget it is taking on run time.

    I think the problem is you have to understand the basics first like
    @
    creating .h and .cpp file inheriting QWidget/QMainWindow or Qdialog.
    SIGNAL and SLOT
    and many more thing.
    @



  • ok =)
    Thank you.
    I am very rush because i was only given 2weeks time to port a project coding into Qt ...

    Sorry for troubling u all
    and Thanks a lot


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.