Results 1 to 5 of 5

Thread: How to read a textfile and display result in gui [Qt programming problem]

  1. #1
    Join Date
    Sep 2010
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default How to read a textfile and display result in gui [Qt programming problem]

    could anyone give a helping hand?
    my code was able to read the textfile and display using gui.
    the textfile "data.txt" is in this form

    123-GCF 2 1 2
    124-BOG 4 1 2 5 7
    456-DES 6 6 7 8 9 3 1
    234-REE 2 9 8

    wherein :
    123-GCF = code
    2 = size of array
    1 2 = array of numbers

    i need to count the frequency of numbers in the third column that is the array of numbers
    and display the result in the gui.

    in this example once i clicked the display button it should show
    1 = 3;
    2 = 2;
    3 = 1;
    ...
    ...
    ...

    so far I have this code................... your help is very much appreciated..

    thanks in advance ....

    Qt Code:
    1. #include<QtCore>
    2. #include<QLabel>
    3. #include<QFile>
    4. #include<QtGui>
    5. #include<QString>
    6. #include <iostream>
    7. class DblVec : public QVector<int>{
    8. public:
    9. DblVec(int n):QVector<int>(n){}
    10. };
    11. using namespace std;
    12.  
    13. int main(int argc, char *argv[])
    14. {
    15. QApplication a(argc, argv);
    16. QWidget *window = new QWidget;
    17. QGridLayout *layout = new QGridLayout;
    18. QPushButton *readFile = new QPushButton;
    19. QString date;
    20. quint32 size;
    21. DblVec line(size);
    22. QTextBrowser *browse = new QTextBrowser;
    23. QScrollBar *scrollbar = new QScrollBar;
    24. QFile infile("data.txt");
    25.  
    26. window->setWindowTitle("Data");
    27. readFile->setText("Display Relative Frequency");
    28. layout->addWidget(readFile, 0, 1);
    29.  
    30. if ( infile.open(QIODevice::ReadOnly |QIODevice::Text) )
    31. {QString line2;
    32. QTextStream stream( &infile );
    33. while(!stream.atEnd())
    34. {
    35. line2 = stream.readAll();
    36. }
    37.  
    38. browse->setText(line2);
    39. browse->setVerticalScrollBar(scrollbar);
    40. browse->show();
    41. infile.close();
    42. }
    43.  
    44. layout->addWidget(browse, 0, 0);
    45. window->setLayout(layout);
    46. window->resize(500, 400);
    47. window->show();
    48. return a.exec();
    49. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: How to read a textfile and display result in gui [Qt programming problem]

    Counting numbers is pretty easy.

    Instead of giving you the answer to your homework, try to think about the problem yourself.

    What would you do to count the numbers?

    Edit: lets make this simple.
    I have: 1 1 2 3 5 2 8 1
    How many times does 2 appear in this list? How do you count it?

  3. #3
    Join Date
    Sep 2010
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to read a textfile and display result in gui [Qt programming problem]

    sorry, my englsih is very bad. i think i am not understood very well here.

    my code shows that i am using QString for the entire line, that is how i read the textfile.

    i am new to Qt so i am not even familiar with all the libraries and how to use it.

    at first i tried to assign Qstring for the code, quint32 for the size, QVector for the array
    but its still not working.

    my problem is accessing the third column. obviously, i can't acces third column if i assign QString for the entire line when reading the textfile. I need to find ways of reading wherein I can access the third column. And from there, i can start doing my counting.

    thanks for the comment... i will try to figure it out myself. it's hard to explain my english is very bad. a lesson is learn today. don't ever ask or you shall be bad mouthed.

  4. #4
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: How to read a textfile and display result in gui [Qt programming problem]

    Ohh, from your first question I thought you had a problem with counting.

    For the parsing, use a QTextstream but do not read the entire line into a string.
    Try something like

    Qt Code:
    1. QString string;
    2. int integer1;
    3. ...
    4.  
    5. stream >> string >> integer1 >> ...;
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to read a textfile and display result in gui [Qt programming problem]

    You can also use QString::split() to get a list of field values but then you'll have to convert them to the types you need.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. How to display result
    By sksingh73 in forum Newbie
    Replies: 1
    Last Post: 7th June 2010, 08:39
  2. How to read rtf file and display it?
    By sunnysun520 in forum Qt Programming
    Replies: 2
    Last Post: 16th May 2009, 14:48
  3. Load textfile content into a textEdit
    By gt.beta2 in forum Newbie
    Replies: 1
    Last Post: 3rd March 2009, 22:57
  4. loading a textfile in a qlabel
    By harakiri in forum Newbie
    Replies: 5
    Last Post: 12th June 2007, 09:08
  5. How to Read and display BMP image using QT
    By agsrinivasan in forum Qt Programming
    Replies: 3
    Last Post: 29th January 2007, 07:14

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.