Results 1 to 5 of 5

Thread: Output problem

  1. #1
    Join Date
    Feb 2007
    Posts
    42
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question Output problem

    hi, i am reading this book on qt4::: An Introduction to Design Patterns in C++ with Qt4

    one of the examples has the following code:

    qtio-demo.cpp
    Qt Code:
    1. #include <QDate>
    2. #include <QFile>
    3. #include "qstd.h"
    4. //start
    5. int main() {
    6. using namespace qstd;
    7. QDate d1(2002, 4,1), d2(QDate::currentDate());
    8. int days;
    9. cout << "The first date is: " << d1.toString()
    10. << "\nToday's date is: "
    11. << d2.toString("ddd MMMM d, yyyy")<< endl;
    12.  
    13. if (d1 < d2)
    14. cout << d1.toString("MM/dd/yy") << " is earlier than "
    15. << d2.toString("yyyyMMdd") << endl;
    16.  
    17. cout << "There are " << d1.daysTo(d2)
    18. << " days between "
    19. << d1.toString("MMM dd, yyyy") << " and "
    20. << d2.toString(Qt::ISODate) << endl;
    21.  
    22. cout << "Enter number of days to add to the first date: "
    23. << flush;
    24. days = promptInt();
    25. cout << "The first date was " << d1.toString()
    26. << "\nThe computed date is "
    27. << d1.addDays(days).toString() << endl;
    28. cout << "First date displayed in longer format: "
    29. << d1.toString("dddd, MMMM dd, yyyy") << endl;
    30. //end
    31.  
    32. cout << "\nNow we save the following dates to a file: \n"
    33. << d1 .toString() << '\t' << d2.toString() << endl;
    34. QFile outfile;
    35. promptOutputFile(outfile);
    36. QTextStream ofs (&outfile);
    37. ofs << d1.toString() << '\n';
    38. ofs << d2.toString() << '\n';
    39. outfile.flush();
    40. outfile.close();
    41. cout << "Now we read those dates from the file:\n";
    42. QFile infile;
    43. promptInputFile(infile);
    44. QTextStream ifstr(&infile);
    45. QString dateStr;
    46. QDate dt1, dt2; // we use new variables
    47. dateStr = ifstr.readLine();
    48. dt1 = QDate::fromString(dateStr);
    49. dateStr = ifstr.readLine();
    50. dt2 = QDate::fromString(dateStr);
    51. cout << "Here are the dates we read from the file:\n"
    52. << dt1.toString() << '\n'
    53. << dt2.toString() << endl;
    54. infile.close();
    55. return 0;
    56. }
    To copy to clipboard, switch view to plain text mode 

    qstd.h
    Qt Code:
    1. #ifndef QSTD_H
    2. #define QSTD_H
    3.  
    4. #include <QTextStream>
    5. #include <QFile>
    6. #include <QString>
    7.  
    8. /** @short helper objects and functions which help reduce the
    9.   need for char[] and the standard library.
    10.  
    11.   defines three @ref QTextStream instances
    12.   which behave like the c++ standard iostreams, bound to the
    13.   standard in/out/error.
    14.  
    15.   Also provided, some helper functions for writing
    16.   interactive stdin/stdout applications.
    17. */
    18. //start
    19. namespace qstd {
    20.  
    21. /** @short An alias for standard input
    22.   */
    23. extern QTextStream cin; /* declared only, defined in the .cpp file */
    24. /** @short An alias for standard output
    25.   */
    26. extern QTextStream cout;
    27. /** @short An alias for standard error
    28.   */
    29. extern QTextStream cerr;
    30. /** yes/no prompt
    31.   interactive stdin UI - prompts user with
    32.   a yes/no question. Repeatedly-asks
    33.   until user supplies a valid answer.
    34.  
    35.   @param yesNoQuestion the yes/no question
    36.   @return true/false depending on what the
    37.   user responded.
    38.   */
    39. bool yes(QString yesNoQuestion);
    40. /** Convenience function that feeds a specific question
    41.   to the yes() function.
    42.   @usage do {.....} while(more ("foobar"));
    43.   so that user sees the question: "Another foobar (y/n)? "
    44.   @param name of the item being handled by the loop.
    45.   */
    46. bool more(QString prompt);
    47. /** A function for safely taking an int from the keyboard.
    48.   Takes data into a QString and tests to make sure it
    49.   can be converted to int before returning.
    50.   @param base allows choice of number base.
    51.   @return returns validated int.
    52.   */
    53. int promptInt(int base = 10);
    54. /** A function for safely taking a double from the keyboard.
    55.   Takes data into a QString and tests to make sure it
    56.   can be converted to double before returning.
    57.   @return returns validated int.
    58.   */
    59. double promptDouble();
    60. /** Complete dialog for opening a file for output.
    61.   Asks user for file name, checks to see if
    62.   file already exists and, if so, asks the user if
    63.   it is ok to overwrite.
    64.   @param Reference QFile parameter is set to point
    65.   to the (eventually) opened file.
    66.   */
    67. /** @short Dialog for a output file prompt
    68.   */
    69. void promptOutputFile(QFile& outfile);
    70.  
    71. /** @short Dialog for input file prompt */
    72. void promptInputFile(QFile& infile);
    73.  
    74.  
    75. //end
    76. }
    77.  
    78. #endif
    To copy to clipboard, switch view to plain text mode 

    qstd.cpp
    Qt Code:
    1. //start id=namespace
    2. #include "qstd.h"
    3.  
    4. /* QTextStreams look a lot like iostreams,
    5. we just have to point them to the right place. */
    6.  
    7. //start id=streamdefs
    8. QTextStream qstd::cin(stdin, QIODevice::ReadOnly);
    9. QTextStream qstd::cout(stdout, QIODevice::WriteOnly);
    10. QTextStream qstd::cerr(stderr, QIODevice::WriteOnly);
    11. //end
    12.  
    13.  
    14. /* Namespace members are like static class members */
    15. bool qstd::yes(QString question) {
    16. QString ans;
    17. cout << QString(" %1 [y/n]? ").arg(question);
    18. cout.flush();
    19. ans = cin.readLine();
    20. return (ans.toUpper().startsWith("Y", Qt::CaseInsensitive));
    21. }
    22. //end
    23.  
    24. bool qstd::more(QString s) {
    25. return yes(QString("Another %1").arg(s));
    26. }
    27.  
    28.  
    29. int qstd::promptInt(int base /* =10 */) { /* Usage: int n = promptInt(); */
    30. QString numstr;
    31. int result;
    32. bool ok;
    33. cout << ": " << flush;
    34. while (1) {
    35. numstr = cin.readLine();
    36. result = numstr.toInt(&ok, base);
    37. if (!ok) {
    38. cout << "Invalid number. Try again: ";
    39. cout.flush();
    40. }
    41. else
    42. return result;
    43. }
    44. }
    45.  
    46.  
    47. double qstd::promptDouble() { /* Usage: double d = promptDouble(); */
    48. QString numstr;
    49. double result;
    50. bool ok;
    51. while (1) {
    52. numstr = cin.readLine();
    53. result = numstr.toDouble(&ok);
    54. if (!ok) {
    55. cout << "Invalid number. Try again: ";
    56. cout.flush();
    57. }
    58. else
    59. return result;
    60. }
    61. }
    62.  
    63.  
    64. void qstd::promptOutputFile(QFile& outfile) {
    65. QString filename;
    66. while (1) {
    67. cout << "Please enter the file name for saving this data: ";
    68. cout.flush();
    69. filename = cin.readLine();
    70. outfile.setFileName(filename);
    71. bool fileExists = outfile.open(QIODevice::ReadOnly);
    72. if (!fileExists)
    73. break;
    74. if (yes("File already exists ... Ok to overwrite"))
    75. break;
    76. outfile.close();
    77. outfile.reset();
    78. }
    79. outfile.close();
    80. outfile.reset();
    81. outfile.open(QIODevice::WriteOnly);
    82. cout << filename << " open for writing ...\n";
    83. cout.flush();
    84. }
    85.  
    86.  
    87. void qstd::promptInputFile(QFile& infile) {
    88. QString filename;
    89. while (1) {
    90. cout << "Name of the file to be read: ";
    91. cout.flush();
    92. filename = cin.readLine();
    93. infile.setFileName(filename);
    94. bool fileExists = infile.open(QIODevice::ReadOnly);
    95. if (fileExists)
    96. break;
    97. cout << "File does not exist ... Please try again. \n";
    98. cout.flush();
    99. infile.reset();
    100. }
    101. cout << filename << " open for reading ...\n";
    102. cout.flush();
    103. }
    To copy to clipboard, switch view to plain text mode 

    the code compiles without any problems or errors.The thing is that the application created does nothing!!!!
    it doesn't take input and it doesn't give output...

    How can i fix this??
    I just want to use some of qt's classes and output to console...

    PS: i am developing on Windows environment{if it matters..}

    thanks for your time,
    Nicolas

  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: Output problem

    Add CONFIG+=console to your project file and rerun qmake.

  3. The following user says thank you to wysota for this useful post:

    tpf80 (2nd April 2007)

  4. #3
    Join Date
    Jan 2006
    Location
    Innsbruck, Austria
    Posts
    62
    Thanks
    10
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Output problem

    I don't have much experience with Qt on Windows but perhaps the program is being detached from the terminal. Have you tried a simple printf() to check if it's being detached?

  5. #4
    Join Date
    Feb 2007
    Posts
    42
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Output problem

    Quote Originally Posted by wysota View Post
    Add CONFIG+=console to your project file and rerun qmake.
    i did so, and it worked!!! BUT it doesn't display the date correctly{unknown letters appear....maybe because the local is greek??}and it doesn't accept input correctly.....
    what can be done??

    secondly to create the project file i run qmake -project....shouldn't this add by itself the CONFIG+=console ????

    thanks for your time

  6. #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: Output problem

    Quote Originally Posted by aegis View Post
    BUT it doesn't display the date correctly{unknown letters appear....maybe because the local is greek??}and it doesn't accept input correctly.....
    what can be done??
    You can set a QTextCodec.

    shouldn't this add by itself the CONFIG+=console ????
    No, why should it? Not every application needs access to the console.

Similar Threads

  1. No output to console
    By Morea in forum Newbie
    Replies: 6
    Last Post: 1st November 2007, 22:51
  2. problem with QProcess in Qt4
    By kennyxing in forum Qt Programming
    Replies: 4
    Last Post: 20th October 2006, 09:07
  3. QTimer problem ... it runs but never triggs
    By yellowmat in forum Newbie
    Replies: 4
    Last Post: 4th July 2006, 12:54
  4. Problem with bitBlt
    By yellowmat in forum Newbie
    Replies: 1
    Last Post: 5th April 2006, 14:08
  5. Replies: 16
    Last Post: 7th March 2006, 15:57

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.