Page 1 of 2 12 LastLast
Results 1 to 20 of 27

Thread: How to store QSlider position Value into .txt file?

  1. #1
    Join Date
    Jun 2014
    Posts
    33
    Thanks
    15
    Qt products
    Qt4
    Platforms
    Unix/X11

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

    Qt Code:
    1. int h_slide1 = ui->hueSlide1->value();
    2. QFile valueHSV("/home/pi/valueHSV/hsv.txt")
    3. if(!valueHSV.open(QIODevice::WriteOnly| QIODevice::Text))
    4. return;
    5. QTextStream hsv(&valueHSV);
    6. hsv << h_slide1;
    To copy to clipboard, switch view to plain text mode 

    is this code correct?

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QFile and QTextStream Question

    That looks about right.
    Did the QFile:pen() work?

    Cheers,
    _

  3. #3
    Join Date
    Jun 2014
    Posts
    33
    Thanks
    15
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QFile and QTextStream Question

    this can make file without error,

    Will this store a value in a txt file that after i drag the slider? after i close the GUI,
    i will can open the text file to see my slider value,
    am i correct?
    but the txt file is blank.

  4. #4
    Join Date
    Jun 2014
    Posts
    33
    Thanks
    15
    Qt products
    Qt4
    Platforms
    Unix/X11

    Exclamation How to store QSlider position Value into .txt file?

    Regarding above title, i am doing a GUI to filter out the colour i do not want,
    I would like to get value of H, S and V using slider and store these value inside a .txt file.
    After that i would call these value into another function to processing and show a filter stream video,
    I did use QFile and QTextStreamer, but could not get through.

    Here is a photo of my GUI,
    snapshot1.jpg
    Please advise,

  5. #5
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,499
    Thanked 277 Times in 272 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QFile and QTextStream Question

    Value in file will be stored after the code execution. Question : how You activate this code ?

  6. #6
    Join Date
    Jun 2014
    Posts
    33
    Thanks
    15
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QFile and QTextStream Question

    Hi,
    I just simply build & Run it,
    i create another thread here : http://www.qtcentre.org/threads/5964...-into-txt-file

  7. #7
    Join Date
    Oct 2009
    Posts
    483
    Thanked 96 Times in 93 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QFile and QTextStream Question

    Please do not create other threads, this only pollutes the forum. Focus on giving as much information as you can, so that we can help you effectively.

    So far we have only seen a snippet of code, but we have no idea when it is executed. This may be dead code for all we know. Have you run a debugger and witnessed your snippet being executed? Another important question: what are the lifetimes of the QTextStream and QFile variables?

    It would help if you posted the whole source of a minimal program reproducing the problem.

  8. #8
    Join Date
    Jun 2014
    Posts
    33
    Thanks
    15
    Qt products
    Qt4
    Platforms
    Unix/X11

    Smile Re: QFile and QTextStream Question

    I am so sorry for that.
    i am doing a GUI to filter out the colour i do not want,
    I would like to get value of H, S and V using slider and store these value inside a .txt file.
    After that i would call these value into another function to processing and show a filter stream video,
    I did use QFile and QTextStreamer, but could not get through.
    below is my code

    Qt Code:
    1. //getCam.pro
    2. ######################################################################
    3. # Automatically generated by qmake (2.01a) Tue Jun 24 17:53:36 2014
    4. ######################################################################
    5. CONFIG += console
    6.  
    7. TEMPLATE = app
    8. TARGET =
    9. DEPENDPATH += .
    10. INCLUDEPATH += .
    11.  
    12. # Input
    13. HEADERS += dialog.h
    14. FORMS += dialog.ui
    15. SOURCES += dialog.cpp main.cpp
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. //main.cpp
    2. #include <QApplication>
    3. #include "dialog.h"
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. QApplication a(argc, argv);
    8. Dialog w;
    9. w.show();
    10.  
    11. return a.exec();
    12. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. //dialog.h
    2. #ifndef DIALOG_H
    3. #define DIALOG_H
    4.  
    5. #include <QDialog>
    6.  
    7. #include "opencv/cv.h"
    8. #include "opencv/highgui.h"
    9. #include <QTimer>
    10. #include <QPixmap>
    11.  
    12. namespace Ui
    13. {
    14. class Dialog;
    15. }
    16.  
    17. class Dialog : public QDialog
    18. {
    19. Q_OBJECT
    20.  
    21. public:
    22. explicit Dialog(QWidget *parent = 0);
    23. ~Dialog();
    24.  
    25. private:
    26. Ui::Dialog *ui;
    27.  
    28. CvCapture *cam;
    29. IplImage *frame,*imgHSV;
    30.  
    31. QTimer *timer;
    32.  
    33. private slots:
    34. void getFrame();
    35. void prcFrame();
    36. void getValue();
    37. };
    38.  
    39. #endif // DIALOG_H
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. //dialog.cpp
    2. #include "dialog.h"
    3. #include "ui_dialog.h"
    4.  
    5. #include <QDebug>
    6. #include <QFile>
    7. #include <QTextStream>
    8.  
    9. Dialog::Dialog(QWidget *parent) :
    10. QDialog(parent),
    11. ui(new Ui::Dialog)
    12. {
    13. ui->setupUi(this);
    14.  
    15. timer = new QTimer(this);
    16.  
    17. cam = cvCaptureFromCAM(-1);
    18.  
    19. if(cam==NULL)
    20. qDebug()<<"error";
    21.  
    22. timer->start(30);
    23. connect(timer,SIGNAL(timeout()),this,SLOT(getFrame()));
    24. }
    25.  
    26. void Dialog::getFrame()
    27. {
    28. frame = cvQueryFrame(cam);
    29. QImage image = QImage ((const uchar*)frame->imageData,frame->width,frame->height,QImage::Format_RGB888).rgbSwapped();//rgbSwapped() make color better
    30. ui->original->setPixmap(QPixmap::fromImage(image));
    31. }
    32.  
    33. void Dialog::prcFrame()
    34. {
    35. imgHSV= cvCreateImage(cvSize(frame->width,frame->height),IPL_DEPTH_8U,3);
    36. cvCvtColor(frame,imgHSV,CV_BGR2HSV);
    37.  
    38. //threshed , get value from slider value
    39. // QImage filterImg = QImage ((const uchar*)imgHSV->imageData,imgHSV->width,imgHSV->height,QImage::Format_RGB888);
    40. // ui->filter->setPixmap(QPixmap::fromImage(filterImg));
    41. }
    42.  
    43. [COLOR="#FFFF00"]void Dialog::getValue()
    44. {
    45. int h_slide1 = ui->hueSlide1->value();
    46. QFile valueHSV("/home/pi/valueHSV/hsv.txt");
    47. if(!valueHSV.open(QIODevice::WriteOnly|QIODevice::Text))
    48. {
    49. qDebug() << "cannot open file for writing"<<endl;
    50. return;
    51. }
    52. QTextStream hsv(&valueHSV);
    53. hsv <<h_slide1;
    54. qDebug() << "Value=" << h_slide1;
    55. valueHSV.close();
    56.  
    57. }[/COLOR]
    58.  
    59.  
    60. Dialog::~Dialog()
    61. {
    62. timer->stop();
    63. cvReleaseCapture(&cam);
    64.  
    65. delete ui;
    66. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. //compile output
    2. 04:45:50: Running steps for project getCam...
    3.  
    4. 04:45:50: Configuration unchanged, skipping qmake step.
    5.  
    6. 04:45:50: Starting: "/usr/bin/make" -w
    7.  
    8. make: Entering directory `/home/pi/qt/getCam'
    9.  
    10. /usr/bin/qmake-qt4 -spec /usr/share/qt4/mkspecs/linux-g++ -o Makefile getCam.pro
    11.  
    12. make: Leaving directory `/home/pi/qt/getCam'
    13.  
    14. make: Entering directory `/home/pi/qt/getCam'
    15.  
    16. g++ -c -pipe -O2 -Wall -W -D_REENTRANT -DQT_WEBKIT -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/share/qt4/mkspecs/linux-g++ -I. -I/usr/include/qt4/QtCore -I/usr/include/qt4/QtGui -I/usr/include/qt4 -I. -I. -I. -I/usr/local/include/opencv -o dialog.o dialog.cpp
    17.  
    18. g++ -c -pipe -O2 -Wall -W -D_REENTRANT -DQT_WEBKIT -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/share/qt4/mkspecs/linux-g++ -I. -I/usr/include/qt4/QtCore -I/usr/include/qt4/QtGui -I/usr/include/qt4 -I. -I. -I. -I/usr/local/include/opencv -o main.o main.cpp
    19.  
    20. /usr/bin/moc-qt4 -DQT_WEBKIT -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/share/qt4/mkspecs/linux-g++ -I. -I/usr/include/qt4/QtCore -I/usr/include/qt4/QtGui -I/usr/include/qt4 -I. -I. -I. -I/usr/local/include/opencv dialog.h -o moc_dialog.cpp
    21.  
    22. g++ -c -pipe -O2 -Wall -W -D_REENTRANT -DQT_WEBKIT -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/share/qt4/mkspecs/linux-g++ -I. -I/usr/include/qt4/QtCore -I/usr/include/qt4/QtGui -I/usr/include/qt4 -I. -I. -I. -I/usr/local/include/opencv -o moc_dialog.o moc_dialog.cpp
    23.  
    24. g++ -Wl,-O1 -o getCam dialog.o main.o moc_dialog.o -L/usr/lib/arm-linux-gnueabihf /usr/local/lib/libopencv_core.so /usr/local/lib/libopencv_highgui.so /usr/local/lib/libopencv_ml.so /usr/local/lib/libopencv_imgproc.so -lQtGui -lQtCore -lpthread
    25.  
    26. make: Leaving directory `/home/pi/qt/getCam'
    27.  
    28. 04:47:35: The process "/usr/bin/make" exited normally.
    To copy to clipboard, switch view to plain text mode 

    and this is the pic of GUI design
    snapshot1.jpg

    Please advise

    regards
    YDYD

  9. #9
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QFile and QTextStream Question

    Could you point out where you call getValue()? I can't seem to find it.

    Cheers,
    _

  10. #10
    Join Date
    Jun 2014
    Posts
    33
    Thanks
    15
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QFile and QTextStream Question

    Hi,
    I didn't call getValue()...
    By right, there should be a connect() in dialog.cpp, but i deleted,
    and getValue() is to act as a SLOT?

    I not really know how to use it....
    Do i need another .cpp and .h file for "on_horizontalSlider_valueChanged()"?


    Added after 45 minutes:


    i just test my code using timer as my signal,

    connect(timer,SIGNAL(timeout()),this,SLOT(getValue ()));
    I was able to get Value and in text file too!

    but, i would like to change only when slider is move, what should i put as SIGNAL?

    Regards
    YDYD
    Last edited by YDYD; 1st July 2014 at 13:27.

  11. #11
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QFile and QTextStream Question

    The slider has a valueChanged signal (declared in QAbstractSlider).

    Qt Code:
    1. connect(ui->hueSlide1, SIGNAL(valueChanged(int)), this, SLOT(getValue()));
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  12. The following user says thank you to anda_skoa for this useful post:

    YDYD (2nd July 2014)

  13. #12
    Join Date
    Oct 2009
    Posts
    483
    Thanked 96 Times in 93 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to store QSlider position Value into .txt file?

    There is a problem at line 55 in dialog.cpp:
    Qt Code:
    1. valueHSV.close();
    To copy to clipboard, switch view to plain text mode 
    This closes the file even though the QTextStream hsv may still have bytes in its internal buffer that have not been written to the file. You can fix the issue by removing this line. When the function exits, the variables on the stack are destroyed in reverse order of construction. That means that hsv is destroyed before valueHSV. When hsv is destroyed, it flushes its buffer (see QTextStream::~QTextStream()), writing any unwritten bytes to the (still open) file. Then valueHSV is destroyed, and is closed and flushed to disk automatically in the process (see QFile::~QFile()).

  14. The following user says thank you to yeye_olive for this useful post:

    YDYD (2nd July 2014)

  15. #13
    Join Date
    Jun 2014
    Posts
    33
    Thanks
    15
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to store QSlider position Value into .txt file?

    Thank to anda_skoa and yeye_olive remind,
    in my text file i get the value once i move the slider!

    Qt Code:
    1. qDebug() << "Value=" << h_slide1;
    To copy to clipboard, switch view to plain text mode 

    in my text file only have value, what if i need "Value = XX" inside of my text file?
    Is this achievable ?


    Added after 12 minutes:


    I have solved this myself, thank you all,

    another question is, can i output as XML format?
    Last edited by YDYD; 2nd July 2014 at 02:12.

  16. #14
    Join Date
    Jun 2014
    Posts
    33
    Thanks
    15
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to store QSlider position Value into .txt file?

    my dialog.cpp end up like this..

    Qt Code:
    1. #include "dialog.h"
    2. #include "ui_dialog.h"
    3.  
    4. #include <QDebug>
    5. #include <QFile>
    6. #include <QTextStream>
    7.  
    8. Dialog::Dialog(QWidget *parent) :
    9. QDialog(parent),
    10. ui(new Ui::Dialog)
    11. {
    12. ui->setupUi(this);
    13.  
    14. timer = new QTimer(this);
    15.  
    16. cam = cvCaptureFromCAM(-1);
    17.  
    18. if(cam==NULL)
    19. qDebug()<<"error";
    20.  
    21. timer->start(30);
    22. connect(timer,SIGNAL(timeout()),this,SLOT(getFrame()));
    23.  
    24. connect(ui->hueSlide1,SIGNAL(valueChanged(int)),this,SLOT(saveValue()));
    25. connect(ui->hueSlide2,SIGNAL(valueChanged(int)),this,SLOT(saveValue()));
    26. connect(ui->satSlide1,SIGNAL(valueChanged(int)),this,SLOT(saveValue()));
    27. connect(ui->satSlide2,SIGNAL(valueChanged(int)),this,SLOT(saveValue()));
    28. connect(ui->lumSlide1,SIGNAL(valueChanged(int)),this,SLOT(saveValue()));
    29. connect(ui->lumSlide2,SIGNAL(valueChanged(int)),this,SLOT(saveValue()));
    30. }
    31.  
    32. void Dialog::getFrame()
    33. {
    34. frame = cvQueryFrame(cam);
    35. QImage image = QImage ((const uchar*)frame->imageData,frame->width,frame->height,QImage::Format_RGB888).rgbSwapped();//rgbSwapped() make color better
    36. ui->original->setPixmap(QPixmap::fromImage(image));
    37. }
    38.  
    39. void Dialog::prcFrame()
    40. {
    41. imgHSV= cvCreateImage(cvSize(frame->width,frame->height),IPL_DEPTH_8U,3);
    42. cvCvtColor(frame,imgHSV,CV_BGR2HSV);
    43.  
    44. //threshed , get value from slider value
    45. // QImage filterImg = QImage ((const uchar*)imgHSV->imageData,imgHSV->width,imgHSV->height,QImage::Format_RGB888);
    46. // ui->filter->setPixmap(QPixmap::fromImage(filterImg));
    47. }
    48.  
    49. void Dialog::saveValue()
    50. {
    51. int h_slide1 = ui->hueSlide1->value();
    52. int h_slide2 = ui->hueSlide2->value();
    53. int s_slide1 = ui->satSlide1->value();
    54. int s_slide2 = ui->satSlide2->value();
    55. int l_slide1 = ui->lumSlide1->value();
    56. int l_slide2 = ui->lumSlide2->value();
    57. QFile valueHSV("/home/pi/valueHSV/hsv.txt");
    58. if(!valueHSV.open(QIODevice::WriteOnly|QIODevice::Text))
    59. {
    60. qDebug() << "cannot open file for writing"<<endl;
    61. return;
    62. }
    63. QTextStream hsv(&valueHSV);
    64. hsv << "<HUE LOW>" <<h_slide1<<endl;
    65. hsv << "<HUE HIGH>" <<h_slide2<<endl;
    66. hsv << "<SAT LOW>" <<s_slide1<<endl;
    67. hsv << "<SAT HIGH>" <<s_slide2<<endl;
    68. hsv << "<LUM LOW>" <<l_slide1<<endl;
    69. hsv << "<LUM HIGH>" <<l_slide2<<endl;
    70. //qDebug() << "H_low" << h_slide1;
    71. //qDebug() << "H_high" << h_slide2;
    72. //valueHSV.close();
    73.  
    74. }
    75.  
    76. Dialog::~Dialog()
    77. {
    78. timer->stop();
    79. cvReleaseCapture(&cam);
    80.  
    81. delete ui;
    82. }
    To copy to clipboard, switch view to plain text mode 

    if i would like to read the value from the txt file and send back to the slider, is it possible?

  17. #15
    Join Date
    Jun 2014
    Posts
    33
    Thanks
    15
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to store QSlider position Value into .txt file?

    I have added in a new connect , which is when LOAD button is clicked, i can get my the value from txt file which is from loadValue() function, but I could not obtain it,
    I did not get debug message from this function too,
    please advise where i did wrong,

    By the way, i would like to get Integer value only, i don't want the words.

    Qt Code:
    1. #include "dialog.h"
    2. #include "ui_dialog.h"
    3.  
    4. #include <QDebug>
    5. #include <QFile>
    6. #include <QTextStream>
    7.  
    8. Dialog::Dialog(QWidget *parent) :
    9. QDialog(parent),
    10. ui(new Ui::Dialog)
    11. {
    12. ui->setupUi(this);
    13.  
    14.  
    15. timer = new QTimer(this);
    16.  
    17. cam = cvCaptureFromCAM(-1);
    18.  
    19. if(cam==NULL)
    20. qDebug()<<"error";
    21.  
    22. timer->start(30);
    23. connect(timer,SIGNAL(timeout()),this,SLOT(getFrame()));
    24.  
    25. connect(ui->hueSlide1,SIGNAL(valueChanged(int)),this,SLOT(saveValue()));
    26. connect(ui->hueSlide2,SIGNAL(valueChanged(int)),this,SLOT(saveValue()));
    27. connect(ui->satSlide1,SIGNAL(valueChanged(int)),this,SLOT(saveValue()));
    28. connect(ui->satSlide2,SIGNAL(valueChanged(int)),this,SLOT(saveValue()));
    29. connect(ui->lumSlide1,SIGNAL(valueChanged(int)),this,SLOT(saveValue()));
    30. connect(ui->lumSlide2,SIGNAL(valueChanged(int)),this,SLOT(saveValue()));
    31. connect(ui->load,SIGNAL(clicked()),this,SLOT(loadValue()));
    32. }
    33.  
    34. void Dialog::getFrame()
    35. {
    36. frame = cvQueryFrame(cam);
    37. QImage image = QImage ((const uchar*)frame->imageData,frame->width,frame->height,QImage::Format_RGB888).rgbSwapped();//rgbSwapped() make color better
    38. ui->original->setPixmap(QPixmap::fromImage(image));
    39. }
    40.  
    41. void Dialog::prcFrame()
    42. {
    43. imgHSV= cvCreateImage(cvSize(frame->width,frame->height),IPL_DEPTH_8U,3);
    44. cvCvtColor(frame,imgHSV,CV_BGR2HSV);
    45.  
    46. //threshed , get value from slider value
    47. // QImage filterImg = QImage ((const uchar*)imgHSV->imageData,imgHSV->width,imgHSV->height,QImage::Format_RGB888);
    48. // ui->filter->setPixmap(QPixmap::fromImage(filterImg));
    49. }
    50.  
    51. void Dialog::saveValue()
    52. {
    53. int h_slide1 = ui->hueSlide1->value();
    54. int h_slide2 = ui->hueSlide2->value();
    55. int s_slide1 = ui->satSlide1->value();
    56. int s_slide2 = ui->satSlide2->value();
    57. int l_slide1 = ui->lumSlide1->value();
    58. int l_slide2 = ui->lumSlide2->value();
    59. QFile valueHSV("/home/pi/valueHSV/hsv.txt");
    60. if(!valueHSV.open(QIODevice::WriteOnly|QIODevice::Text))
    61. {
    62. qDebug() << "cannot open file for writing"<<endl;
    63. return;
    64. }
    65. QTextStream hsv(&valueHSV);
    66. hsv << "<HUE LOW>" <<h_slide1<<endl;
    67. hsv << "<HUE HIGH>" <<h_slide2<<endl;
    68. hsv << "<SAT LOW>" <<s_slide1<<endl;
    69. hsv << "<SAT HIGH>" <<s_slide2<<endl;
    70. hsv << "<LUM LOW>" <<l_slide1<<endl;
    71. hsv << "<LUM HIGH>" <<l_slide2<<endl;
    72. //qDebug() << "H_low" << h_slide1;
    73. //qDebug() << "H_high" << h_slide2;
    74. //valueHSV.close();
    75.  
    76. }
    77.  
    78. void Dialog::loadValue()
    79. {
    80. QFile valueHSV("/home/pi/valueHSV/hsv.txt");
    81. if(!valueHSV.open(QIODevice::ReadOnly|QIODevice::Text))
    82. {
    83. qDebug() << "cannot open file for reading"<<endl;
    84. return;
    85. }
    86. QTextStream hsv(&valueHSV);
    87. while (!hsv.atEnd())
    88. {
    89. QString load=hsv.readLine();
    90. if(load.isNull())
    91. break;
    92. }
    93. foreach(QString str, list)
    94. {
    95. int h1 = str.toInt();
    96. qDebug() << "value"<<h1;
    97. }
    98. return;
    99. }
    100.  
    101. Dialog::~Dialog()
    102. {
    103. timer->stop();
    104. cvReleaseCapture(&cam);
    105.  
    106. delete ui;
    107. }
    To copy to clipboard, switch view to plain text mode 

  18. #16
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,391
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to store QSlider position Value into .txt file?

    Qt Code:
    1. ...
    2. TextStream hsv(&valueHSV);
    3. while (!hsv.atEnd())
    4. {
    5. QString load=hsv.readLine();
    6. if(load.isNull())
    7. break;
    8. }
    9. foreach(QString str, list)
    10. {
    11. ....
    To copy to clipboard, switch view to plain text mode 
    "list" is always empty, you never add anything to it. I guess you want to do that in the while loop:
    Qt Code:
    1. TextStream hsv(&valueHSV);
    2. while (!hsv.atEnd())
    3. {
    4. QString load=hsv.readLine();
    5. if(load.isNull())
    6. break;
    7. list << load;
    8. }
    To copy to clipboard, switch view to plain text mode 

  19. The following user says thank you to stampede for this useful post:

    YDYD (2nd July 2014)

  20. #17
    Join Date
    Jun 2014
    Posts
    33
    Thanks
    15
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to store QSlider position Value into .txt file?

    Quote Originally Posted by stampede View Post
    Qt Code:
    1. ...
    2. TextStream hsv(&valueHSV);
    3. while (!hsv.atEnd())
    4. {
    5. QString load=hsv.readLine();
    6. if(load.isNull())
    7. break;
    8. }
    9. foreach(QString str, list)
    10. {
    11. ....
    To copy to clipboard, switch view to plain text mode 
    "list" is always empty, you never add anything to it. I guess you want to do that in the while loop:
    Qt Code:
    1. TextStream hsv(&valueHSV);
    2. while (!hsv.atEnd())
    3. {
    4. QString load=hsv.readLine();
    5. if(load.isNull())
    6. break;
    7. list << load;
    8. }
    To copy to clipboard, switch view to plain text mode 
    Hi stampede,
    Thanks for your reply, after i deleted the foreach loop, and add
    Qt Code:
    1. list<<load;
    To copy to clipboard, switch view to plain text mode 

    i was able to get all the thing i written into txt file,
    but i would like to have the Integer inside txt file only, is it possible?

  21. #18
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to store QSlider position Value into .txt file?

    Quote Originally Posted by YDYD View Post
    another question is, can i output as XML format?
    Sure.

    Either create it manually, i.e. writing the XML markup as text, or using QXmlStreamWriter

    Cheers,
    _

  22. The following user says thank you to anda_skoa for this useful post:

    YDYD (2nd July 2014)

  23. #19
    Join Date
    Jun 2014
    Posts
    33
    Thanks
    15
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to store QSlider position Value into .txt file?

    Qt Code:
    1. void Dialog::saveValue()
    2. {
    3. int h_slide1 = ui->hueSlide1->value();
    4. int h_slide2 = ui->hueSlide2->value();
    5. int s_slide1 = ui->satSlide1->value();
    6. int s_slide2 = ui->satSlide2->value();
    7. int l_slide1 = ui->lumSlide1->value();
    8. int l_slide2 = ui->lumSlide2->value();
    9. QFile valueHSV("/home/pi/valueHSV/hsv.xml");
    10. if(!valueHSV.open(QIODevice::WriteOnly|QIODevice::Text))
    11. {
    12. qDebug() << "cannot open file for writing"<<endl;
    13. return;
    14. }
    15. QXmlStreamWriter hsv(&valueHSV);
    16. hsv.setAutoFormatting(true);
    17. hsv.writeStartDocument();
    18. hsv.writeStartElement("HSV");
    19. hsv.writeAttribute("value","hsv");
    20. hsv.writeTextElement("HUE LOW",h_slide1);
    21. hsv.writeTextElement("HUE HIGH",h_slide2);
    22. hsv.writeTextElement("SAT LOW",s_slide1);
    23. hsv.writeTextElement("SAT HIGH",s_slide2);
    24. hsv.writeTextElement("LUM LOW",l_slide1);
    25. hsv.writeTextElement("LUM HIGH",l_slide2);
    26. hsv.writeEndElement();
    27. hsv.writeEndDocument();
    28. valueHSV.close();
    29. /*QTextStream hsv(&valueHSV);
    30.   hsv << "<HUE LOW>" <<h_slide1<<endl;
    31.   hsv << "<HUE HIGH>" <<h_slide2<<endl;
    32.   hsv << "<SAT LOW>" <<s_slide1<<endl;
    33.   hsv << "<SAT HIGH>" <<s_slide2<<endl;
    34.   hsv << "<LUM LOW>" <<l_slide1<<endl;
    35.   hsv << "<LUM HIGH>" <<l_slide2<<endl;*/
    36. //qDebug() << "H_low" << h_slide1;
    37. //qDebug() << "H_high" << h_slide2;
    38. //valueHSV.close();
    39.  
    40. }
    To copy to clipboard, switch view to plain text mode 

    After i change to QXmlStreamWriter

    I get error
    Qt Code:
    1. ERROR MSG:
    2. invalid conversion from 'int' to 'const char*' [-fpermissive]
    3. error: initializing argument 1 of 'QString::QString(const char*)'
    To copy to clipboard, switch view to plain text mode 

    Please advise.
    Thanks in advance

  24. #20
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,391
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to store QSlider position Value into .txt file?

    Qt Code:
    1. hsv.writeTextElement("HUE LOW",h_slide1);
    To copy to clipboard, switch view to plain text mode 
    second argument to "writeTextElement" should be a string, not an integer:
    Qt Code:
    1. hsv.writeTextElement("HUE LOW",QString::number(h_slide1));
    To copy to clipboard, switch view to plain text mode 

  25. The following user says thank you to stampede for this useful post:

    YDYD (2nd July 2014)

Similar Threads

  1. Replies: 4
    Last Post: 1st December 2011, 19:53
  2. Secure way to store passwords in settings file
    By Alir3z4 in forum Qt Programming
    Replies: 2
    Last Post: 27th November 2011, 12:27
  3. QSlider : How to hint current position value ?
    By andre_teprom in forum Newbie
    Replies: 2
    Last Post: 10th August 2011, 01:19
  4. read a .txt file and store it in a double array
    By fatecasino in forum Newbie
    Replies: 5
    Last Post: 3rd December 2010, 20:13
  5. initial position of a QSlider
    By franco.amato in forum Qt Programming
    Replies: 9
    Last Post: 24th September 2008, 18:37

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.