Results 1 to 6 of 6

Thread: Read values from text file after clicking a button

  1. #1
    Join Date
    Dec 2011
    Location
    Bangalore,India
    Posts
    38
    Thanks
    14
    Qt products
    Qt4
    Platforms
    Windows

    Smile Read values from text file after clicking a button

    Hi frns,
    The below code is for reading values from text files and display it in the text box and i get some errors in the below code . kindly please help me out.. thanks in advance..

    void MainWindow::slotTimer()
    {
    QFile file("Heartrate.txt");
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
    return;

    QTextStream in(&file);
    while (!in.atEnd())
    {
    QString line = in.readLine();
    TEhr(line);// displays the value of heartrate in Textbox
    }

    }

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,733
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Read values from text file after clicking a button

    You will have to tell us what the error messages are or we will be guessing all night.

  3. #3
    Join Date
    Dec 2011
    Location
    Bangalore,India
    Posts
    38
    Thanks
    14
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Read values from text file after clicking a button

    Hi,
    Thanks,the error is that the variable TEhr is a textedit box and it show that it accepts only one parameter...i the last line..

  4. #4
    Join Date
    May 2012
    Location
    Bangalore, India
    Posts
    271
    Thanks
    29
    Thanked 50 Times in 47 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Cool Re: Read values from text file after clicking a button

    Have you done that through designer? Could you please share your application code??? So that we would able to figure out the problem. Use signal slot for the button click, what is this slotTimer???


    Added after 5 minutes:


    See this code---->


    Qt Code:
    1. #ifndef NOTEPAD_H
    2. #define NOTEPAD_H
    3.  
    4. #include <QWidget>
    5. #include<QFile>
    6. #include<QTextStream>
    7.  
    8. namespace Ui {
    9. class Notepad;
    10. }
    11.  
    12. class Notepad : public QWidget
    13. {
    14. Q_OBJECT
    15.  
    16. public:
    17. explicit Notepad(QWidget *parent = 0);
    18. ~Notepad();
    19.  
    20. public slots:
    21. void showContent();
    22.  
    23. private:
    24. Ui::Notepad *ui;
    25. };
    To copy to clipboard, switch view to plain text mode 


    Qt Code:
    1. #include "notepad.h"
    2. #include "ui_notepad.h"
    3. Notepad::Notepad(QWidget *parent) :
    4. QWidget(parent),
    5. ui(new Ui::Notepad)
    6. {
    7. ui->setupUi(this);
    8. connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(showContent()));
    9. }
    10. void Notepad::showContent(){
    11. QString str;
    12. QFile file("yourfile.txt");
    13. if(file.open(QIODevice::ReadOnly)){
    14. QTextStream stream(&file);
    15. stream>>str;
    16. file.close();
    17. }
    18. ui->textEdit->setText(str);
    19. }
    20.  
    21. Notepad::~Notepad()
    22. {
    23. delete ui;
    24. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by sonulohani; 8th June 2012 at 14:05.

  5. #5
    Join Date
    Dec 2011
    Location
    Bangalore,India
    Posts
    38
    Thanks
    14
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Read values from text file after clicking a button

    I have pasted mycode below ...

    #include "user.h"

    #include <QWidget>
    #include <QtCore/QDebug>
    #include <QtCore/QStringList>
    #include <QTextStream>
    #include <QtCore>
    #include <QLabel>
    #include <QFile>
    #include <QtGui>
    #include <iostream>

    User::User(QWidget *parent)
    : QWidget(parent)
    {

    QGridLayout *layout = new QGridLayout;

    lblhr = new QLabel("Heartrate");
    lblsp = new QLabel("SPO2");
    lblpi = new QLabel("Perfusion Index");
    QFont labelFont( "TimesNewRoman", 10);

    TEhr = new QTextEdit();
    TEsp = new QTextEdit();
    TEpi = new QTextEdit();

    Bstart = new QPushButton("Start Read");
    connect(Bstart, SIGNAL(clicked()),this,SLOT(slotstart()));
    Bstart->setStyleSheet("* { background-color: rgb(255,125,100) }");

    Bstop = new QPushButton("Stop Read");
    connect(Bstop, SIGNAL(clicked()),this,SLOT(slotstop()));
    Bstop->setStyleSheet("* { background-color: rgb(255,125,100) }");

    Bout = new QPushButton();
    connect(Bout,SIGNAL(clicked()),this,SLOT(slotout() ));


    TEha = new QTextBrowser();


    layout->addWidget(lblhr,0,0);
    layout->addWidget(lblsp,1,0);
    layout->addWidget(lblpi,2,0);
    layout->addWidget(TEhr,0,2);
    layout->addWidget(TEsp,1,2);
    layout->addWidget(TEpi,2,2);
    layout->addWidget(Bstart,0,20);
    layout->addWidget(Bstop,1,20);
    layout->addWidget(Bout,3,20);
    layout->addWidget(TEha,5,0);



    lblhr->setFont(labelFont);
    lblsp->setFont(labelFont);
    lblpi->setFont(labelFont);
    TEhr->setFont(labelFont);
    TEsp->setFont(labelFont);
    TEpi->setFont(labelFont);
    TEha->setFont(labelFont);



    TEhr->setFixedSize(80,33);
    TEsp->setFixedSize(80,33);
    TEpi->setFixedSize(80,33);


    // TEhr->setReadonly(true);
    // TEsp->setReadOnly(true);
    // TEpi->setReadOnly(true);

    setLayout(layout);
    }

    User::~User()
    {

    }

    void User::slotstart()
    {
    QString str;
    QFile file("heartrate.txt");
    if(file.open(QIODevice::ReadOnly))
    {
    QTextStream stream(&file);
    stream>>str;
    file.close();
    }
    TEhr->setText(str); ------------> To be displayed in the text box
    }

    void User::slotstop()
    {
    this->showMaximized();
    }

    void User::slotout()
    {
    this->showNormal();
    }

    the text files contains a random one column with some random numbers...
    plz help me...

  6. #6
    Join Date
    May 2012
    Location
    Bangalore, India
    Posts
    271
    Thanks
    29
    Thanked 50 Times in 47 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Cool Re: Read values from text file after clicking a button

    write code in between the CODE tags

Similar Threads

  1. How to read the text of a pdf file?
    By Momergil in forum Newbie
    Replies: 4
    Last Post: 22nd January 2016, 11:45
  2. Read Text file using structure..
    By umulingu in forum Qt Programming
    Replies: 7
    Last Post: 14th September 2009, 12:22
  3. How to read line number in a text file
    By grsandeep85 in forum Qt Programming
    Replies: 7
    Last Post: 31st July 2009, 10:09
  4. Continuous read of text from an input file
    By ttvo in forum Qt Programming
    Replies: 1
    Last Post: 2nd June 2009, 01:09
  5. How to read text only as it is from file
    By thomasjoy in forum Qt Programming
    Replies: 3
    Last Post: 9th August 2007, 09:47

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.