Results 1 to 12 of 12

Thread: How to display text from a file to a text browser

  1. #1
    Join Date
    Jun 2012
    Posts
    7
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default How to display text from a file to a text browser

    Hi all!
    I am attempting to read text from a .csv file and then count the umber of words and display the line and then word count. I can get it to work in a console application in c++ (n linux) but when I put that same code into Qt -- it won't display in the object browser.

    Here is my code below:

    Qt Code:
    1. #include<iostream>
    2. #include<cstdio>
    3. #include<fstream>
    4. #include<string>
    5. #include "bible.h"
    6. #include "ui_bible.h"
    7.  
    8. unsigned int numWords = 0;
    9.  
    10. bible::bible(QWidget *parent) :
    11. QMainWindow(parent),
    12. ui(new Ui::bible)
    13. {
    14. ui->setupUi(this);
    15. connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(on_textBrowser_historyChanged()));
    16. }
    17.  
    18. bible::~bible()
    19. {
    20. delete ui;
    21. }
    22.  
    23. void bible::on_comboBox_activated(const QString &arg1)
    24. {
    25.  
    26. }
    27.  
    28. void bible::on_pushButton_clicked()
    29. {
    30. std::ifstream in("example.csv");
    31. if(not in)
    32. {
    33. perror("example.csv");
    34. }
    35. else
    36. {
    37. std::string text;
    38. //unsigned int numWords = 0;
    39. //unsigned int revnumWords = text.size();
    40. getline(in, text);
    41. std::cout << "Text: \"" << text << "\"" << std::endl;
    42. for(int i=0; i < text.size(); i++)
    43. {
    44. if(text[i] == ' ' || text[i] == '.' || text[i] == ',' ||
    45. text[i] == '!' || text[i] == '?' || text[i] == ';')
    46. {
    47. ++numWords;
    48. //--revnumWords;
    49. //cout << revnumWords << " ";
    50.  
    51. if (numWords > 8)
    52.  
    53. {
    54. std::cout << numWords + 12 << " ";
    55. //cout << revnumWords << " ";
    56.  
    57. }
    58.  
    59. else
    60. {
    61. std::cout << numWords + 10 << " ";
    62. //cout << revnumWords << " ";
    63. }
    64.  
    65. }
    66.  
    67. }
    68.  
    69. }
    70.  
    71. }
    72.  
    73.  
    74. void bible::on_textBrowser_historyChanged()
    75. {
    76. if (numWords > 8)
    77.  
    78. {
    79. std::cout << numWords + 12 << " ";
    80. //cout << revnumWords << " ";
    81.  
    82. }
    83.  
    84. else
    85. {
    86. std::cout << numWords + 10 << " ";
    87. //cout << revnumWords << " ";
    88. }
    89.  
    90. }
    To copy to clipboard, switch view to plain text mode 

    Any help would be greatly appreciated.

    ironmantis7x
    Last edited by wysota; 11th June 2012 at 13:01. Reason: missing [code] tags

  2. #2
    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 display text from a file to a text browser

    You don't have any code related to displaying anything anywhere in your GUI.
    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.


  3. #3
    Join Date
    Jun 2012
    Posts
    7
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to display text from a file to a text browser

    Quote Originally Posted by wysota View Post
    You don't have any code related to displaying anything anywhere in your GUI.
    Can you help me understand where that code goes and what it looks like? I am really lost here...

    ironmantis7x

  4. #4
    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 display text from a file to a text browser

    You want me to teach you C++?
    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.


  5. #5
    Join Date
    Jun 2012
    Posts
    7
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to display text from a file to a text browser

    Quote Originally Posted by wysota View Post
    You want me to teach you C++?
    I please ask that you do not be rude or condescending to me. I am asking because I only know basic c++ and the GUI stuff I learned was on Visual Studio 2010 and I really want to learn Qt way of doing things so I can stay on Linux.

  6. #6
    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 display text from a file to a text browser

    I am not rude, I am merely asking, what you want me (or rather us all) to do? You didn't say what you tried, what didn't work. You didn't say what doubts you had, what you didn't understand from the documentation you had read. If you ask specific questions, you'll get specific answers. If you ask general questions then either you will get general answers or no answers at all.
    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.


  7. #7
    Join Date
    Jun 2012
    Posts
    7
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to display text from a file to a text browser

    I am not asking anyone to write code for me... that is not my aim.
    I read the docs and I am just confused on how to link the output of reading the test file to the correct slot for the QTextBrowser. That is my question. I learn by example best of all. SO what I am asking for is an example of how to link out put (any output) to a QTextBrowser widget.

    ironmantis7x

  8. #8
    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 display text from a file to a text browser

    Qt is C++. In Qt you can use anything C++ allows, thus you can call any public method of any public class you want. Including QTextEdit::setPlainText() or QTextEdit::setHtml().
    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.


  9. #9
    Join Date
    Jun 2012
    Posts
    7
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to display text from a file to a text browser

    thanks for setting me straight on the documentation. I will dive into this and if I have any further questions, I will ask.

    ironmantis7x

  10. #10
    Join Date
    Jun 2012
    Posts
    7
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to display text from a file to a text browser

    Okay -- after some reading and coding, I cam up with the following:


    Qt Code:
    1. #include<iostream>
    2. #include<cstdio>
    3. #include<fstream>
    4. #include<string>
    5. #include "bible2.h"
    6. #include "ui_bible2.h"
    7.  
    8. using namespace std;
    9.  
    10. bible2::bible2(QWidget *parent) :
    11. QMainWindow(parent),
    12. ui(new Ui::bible2)
    13. {
    14. ui->setupUi(this);
    15. connect (ui->pushButton, SIGNAL(clicked()), this, SLOT(on_textEdit_textChanged()));
    16. }
    17.  
    18. bible2::~bible2()
    19. {
    20. delete ui;
    21. }
    22.  
    23. void bible2::on_pushButton_clicked()
    24. {
    25. ifstream in("example.csv");
    26. if(not in)
    27. {
    28. perror("example.csv");
    29. }
    30. else
    31. {
    32. string text;
    33. unsigned int numWords = 0;
    34. unsigned int revnumWords = 0;
    35. getline(in, text);
    36. cout << "Text: \"" << text << "\"" << endl;
    37. for(int i=0; i < text.size(); i++)
    38. {
    39. if(text[i] == ' ' || text[i] == '.' || text[i] == ',' ||
    40. text[i] == '!' || text[i] == '?' || text[i] == ';')
    41. {
    42. ++numWords;
    43. //cout << numWords + 10 << " ";
    44.  
    45. if (numWords > 8)
    46.  
    47. {
    48. cout << numWords + 12 << " ";
    49.  
    50. }
    51.  
    52. else
    53. {
    54. cout << numWords + 10 << " ";
    55. }
    56.  
    57. }
    58.  
    59. }
    60. in.close();
    61. cout << "\n";
    62. cout << "In this textual file \"example.csv\" there exists " << numWords;
    63. cout << " words!"<< endl;
    64.  
    65. }
    66. //return 0;
    67. }
    68.  
    69. void bible2::on_textBrowser_historyChanged()
    70. {
    71.  
    72. }
    73.  
    74. void bible2::on_textEdit_textChanged()
    75. {
    76. void QTextEdit::append(const QString & text);
    77. }
    To copy to clipboard, switch view to plain text mode 
    I am STILL not getting the text to show up in the GUI window.

    What is wrong with my append statement?

    ironmantis7x
    Last edited by wysota; 14th June 2012 at 09:42. Reason: missing [code] tags

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

    Default Re: How to display text from a file to a text browser

    Please use [code] and [/code] tags around code.

    I am STILL not getting the text to show up in the GUI window.

    What is wrong with my append statement?
    Qt Code:
    1. void bible2:on_textEdit_textChanged()
    2. {
    3. void QTextEdit::append(const QString & text);
    4. }
    To copy to clipboard, switch view to plain text mode 
    This is basic C++ knowledge and nothing to do with Qt.

    Line 3 is not a call to QTextEdit::append(), it is a declaration that such a function exists and takes a QString as a parameter. My compiler refuses to compile that and yours probably does too. You need to call the function append() on an instance of the QTextEdit class, i.e. an object. There's a QTextEdit object representing the on-screen text editor inside the ui object, so you can use the ui pointer to access it:
    Qt Code:
    1. ui->textEdit->append("some text");
    To copy to clipboard, switch view to plain text mode 
    You are still going to have to put that call in the correct place; I suggest that where you have it is completely nonsensical. Think about where the output from your console program was emitted...

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

    ironmantis7x (14th June 2012)

  13. #12
    Join Date
    Jun 2012
    Posts
    7
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to display text from a file to a text browser

    This was awesome!! Thanks for pointing out to me where I went wrong. I kept thinking it was a QT thing when clearly I have a lot more to learn about c++! I am a newbie to c++ and Qt.

    Thanks for helping me out. my program is starting to work correctly now.

    ironmantis7x

Similar Threads

  1. Replies: 1
    Last Post: 18th October 2011, 12:02
  2. display in text browser
    By mecrazycoder in forum Newbie
    Replies: 7
    Last Post: 13th December 2010, 17:49
  3. Replies: 0
    Last Post: 2nd August 2010, 10:37
  4. Replies: 3
    Last Post: 3rd May 2009, 09:58
  5. Replies: 1
    Last Post: 3rd September 2008, 15:16

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.