Results 1 to 20 of 27

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

Hybrid View

Previous Post Previous Post   Next Post Next Post
  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,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 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
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 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 ?

  5. #5
    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

  6. #6
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 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.

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

  8. #8
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 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,
    _

  9. #9
    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,

Similar Threads

  1. Replies: 4
    Last Post: 1st December 2011, 20:53
  2. Secure way to store passwords in settings file
    By Alir3z4 in forum Qt Programming
    Replies: 2
    Last Post: 27th November 2011, 13:27
  3. QSlider : How to hint current position value ?
    By andre_teprom in forum Newbie
    Replies: 2
    Last Post: 10th August 2011, 02: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, 21:13
  5. initial position of a QSlider
    By franco.amato in forum Qt Programming
    Replies: 9
    Last Post: 24th September 2008, 19: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.