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
#include <QDate>
#include <QFile>
#include "qstd.h"
//start
int main() {
using namespace qstd;
int days;
cout << "The first date is: " << d1.toString()
<< "\nToday's date is: "
<< d2.toString("ddd MMMM d, yyyy")<< endl;
if (d1 < d2)
cout << d1.toString("MM/dd/yy") << " is earlier than "
<< d2.toString("yyyyMMdd") << endl;
cout << "There are " << d1.daysTo(d2)
<< " days between "
<< d1.toString("MMM dd, yyyy") << " and "
<< d2.toString(Qt::ISODate) << endl;
cout << "Enter number of days to add to the first date: "
<< flush;
days = promptInt();
cout << "The first date was " << d1.toString()
<< "\nThe computed date is "
<< d1.addDays(days).toString() << endl;
cout << "First date displayed in longer format: "
<< d1.toString("dddd, MMMM dd, yyyy") << endl;
//end
cout << "\nNow we save the following dates to a file: \n"
<< d1 .toString() << '\t' << d2.toString() << endl;
promptOutputFile(outfile);
ofs << d1.toString() << '\n';
ofs << d2.toString() << '\n';
outfile.flush();
outfile.close();
cout << "Now we read those dates from the file:\n";
promptInputFile(infile);
QDate dt1, dt2;
// we use new variables dateStr = ifstr.readLine();
dt1
= QDate::fromString(dateStr
);
dateStr = ifstr.readLine();
dt2
= QDate::fromString(dateStr
);
cout << "Here are the dates we read from the file:\n"
<< dt1.toString() << '\n'
<< dt2.toString() << endl;
infile.close();
return 0;
}
#include <QDate>
#include <QFile>
#include "qstd.h"
//start
int main() {
using namespace qstd;
QDate d1(2002, 4,1), d2(QDate::currentDate());
int days;
cout << "The first date is: " << d1.toString()
<< "\nToday's date is: "
<< d2.toString("ddd MMMM d, yyyy")<< endl;
if (d1 < d2)
cout << d1.toString("MM/dd/yy") << " is earlier than "
<< d2.toString("yyyyMMdd") << endl;
cout << "There are " << d1.daysTo(d2)
<< " days between "
<< d1.toString("MMM dd, yyyy") << " and "
<< d2.toString(Qt::ISODate) << endl;
cout << "Enter number of days to add to the first date: "
<< flush;
days = promptInt();
cout << "The first date was " << d1.toString()
<< "\nThe computed date is "
<< d1.addDays(days).toString() << endl;
cout << "First date displayed in longer format: "
<< d1.toString("dddd, MMMM dd, yyyy") << endl;
//end
cout << "\nNow we save the following dates to a file: \n"
<< d1 .toString() << '\t' << d2.toString() << endl;
QFile outfile;
promptOutputFile(outfile);
QTextStream ofs (&outfile);
ofs << d1.toString() << '\n';
ofs << d2.toString() << '\n';
outfile.flush();
outfile.close();
cout << "Now we read those dates from the file:\n";
QFile infile;
promptInputFile(infile);
QTextStream ifstr(&infile);
QString dateStr;
QDate dt1, dt2; // we use new variables
dateStr = ifstr.readLine();
dt1 = QDate::fromString(dateStr);
dateStr = ifstr.readLine();
dt2 = QDate::fromString(dateStr);
cout << "Here are the dates we read from the file:\n"
<< dt1.toString() << '\n'
<< dt2.toString() << endl;
infile.close();
return 0;
}
To copy to clipboard, switch view to plain text mode
qstd.h
#ifndef QSTD_H
#define QSTD_H
#include <QTextStream>
#include <QFile>
#include <QString>
/** @short helper objects and functions which help reduce the
need for char[] and the standard library.
defines three @ref QTextStream instances
which behave like the c++ standard iostreams, bound to the
standard in/out/error.
Also provided, some helper functions for writing
interactive stdin/stdout applications.
*/
//start
namespace qstd {
/** @short An alias for standard input
*/
extern QTextStream cin;
/* declared only, defined in the .cpp file */ /** @short An alias for standard output
*/
/** @short An alias for standard error
*/
/** yes/no prompt
interactive stdin UI - prompts user with
a yes/no question. Repeatedly-asks
until user supplies a valid answer.
@param yesNoQuestion the yes/no question
@return true/false depending on what the
user responded.
*/
/** Convenience function that feeds a specific question
to the yes() function.
@usage do {.....} while(more ("foobar"));
so that user sees the question: "Another foobar (y/n)? "
@param name of the item being handled by the loop.
*/
/** A function for safely taking an int from the keyboard.
Takes data into a QString and tests to make sure it
can be converted to int before returning.
@param base allows choice of number base.
@return returns validated int.
*/
int promptInt(int base = 10);
/** A function for safely taking a double from the keyboard.
Takes data into a QString and tests to make sure it
can be converted to double before returning.
@return returns validated int.
*/
double promptDouble();
/** Complete dialog for opening a file for output.
Asks user for file name, checks to see if
file already exists and, if so, asks the user if
it is ok to overwrite.
@param Reference QFile parameter is set to point
to the (eventually) opened file.
*/
/** @short Dialog for a output file prompt
*/
void promptOutputFile(QFile& outfile);
/** @short Dialog for input file prompt */
void promptInputFile(QFile& infile);
//end
}
#endif
#ifndef QSTD_H
#define QSTD_H
#include <QTextStream>
#include <QFile>
#include <QString>
/** @short helper objects and functions which help reduce the
need for char[] and the standard library.
defines three @ref QTextStream instances
which behave like the c++ standard iostreams, bound to the
standard in/out/error.
Also provided, some helper functions for writing
interactive stdin/stdout applications.
*/
//start
namespace qstd {
/** @short An alias for standard input
*/
extern QTextStream cin; /* declared only, defined in the .cpp file */
/** @short An alias for standard output
*/
extern QTextStream cout;
/** @short An alias for standard error
*/
extern QTextStream cerr;
/** yes/no prompt
interactive stdin UI - prompts user with
a yes/no question. Repeatedly-asks
until user supplies a valid answer.
@param yesNoQuestion the yes/no question
@return true/false depending on what the
user responded.
*/
bool yes(QString yesNoQuestion);
/** Convenience function that feeds a specific question
to the yes() function.
@usage do {.....} while(more ("foobar"));
so that user sees the question: "Another foobar (y/n)? "
@param name of the item being handled by the loop.
*/
bool more(QString prompt);
/** A function for safely taking an int from the keyboard.
Takes data into a QString and tests to make sure it
can be converted to int before returning.
@param base allows choice of number base.
@return returns validated int.
*/
int promptInt(int base = 10);
/** A function for safely taking a double from the keyboard.
Takes data into a QString and tests to make sure it
can be converted to double before returning.
@return returns validated int.
*/
double promptDouble();
/** Complete dialog for opening a file for output.
Asks user for file name, checks to see if
file already exists and, if so, asks the user if
it is ok to overwrite.
@param Reference QFile parameter is set to point
to the (eventually) opened file.
*/
/** @short Dialog for a output file prompt
*/
void promptOutputFile(QFile& outfile);
/** @short Dialog for input file prompt */
void promptInputFile(QFile& infile);
//end
}
#endif
To copy to clipboard, switch view to plain text mode
qstd.cpp
//start id=namespace
#include "qstd.h"
/* QTextStreams look a lot like iostreams,
we just have to point them to the right place. */
//start id=streamdefs
//end
/* Namespace members are like static class members */
cout <<
QString(" %1 [y/n]? ").
arg(question
);
cout.flush();
ans = cin.readLine();
return (ans.toUpper().startsWith("Y", Qt::CaseInsensitive));
}
//end
return yes
(QString("Another %1").
arg(s
));
}
int qstd::promptInt(int base /* =10 */) { /* Usage: int n = promptInt(); */
int result;
bool ok;
cout << ": " << flush;
while (1) {
numstr = cin.readLine();
result = numstr.toInt(&ok, base);
if (!ok) {
cout << "Invalid number. Try again: ";
cout.flush();
}
else
return result;
}
}
double qstd::promptDouble() { /* Usage: double d = promptDouble(); */
double result;
bool ok;
while (1) {
numstr = cin.readLine();
result = numstr.toDouble(&ok);
if (!ok) {
cout << "Invalid number. Try again: ";
cout.flush();
}
else
return result;
}
}
void qstd::promptOutputFile(QFile& outfile) {
while (1) {
cout << "Please enter the file name for saving this data: ";
cout.flush();
filename = cin.readLine();
outfile.setFileName(filename);
bool fileExists
= outfile.
open(QIODevice::ReadOnly);
if (!fileExists)
break;
if (yes("File already exists ... Ok to overwrite"))
break;
outfile.close();
outfile.reset();
}
outfile.close();
outfile.reset();
cout << filename << " open for writing ...\n";
cout.flush();
}
void qstd::promptInputFile(QFile& infile) {
while (1) {
cout << "Name of the file to be read: ";
cout.flush();
filename = cin.readLine();
infile.setFileName(filename);
bool fileExists
= infile.
open(QIODevice::ReadOnly);
if (fileExists)
break;
cout << "File does not exist ... Please try again. \n";
cout.flush();
infile.reset();
}
cout << filename << " open for reading ...\n";
cout.flush();
}
//start id=namespace
#include "qstd.h"
/* QTextStreams look a lot like iostreams,
we just have to point them to the right place. */
//start id=streamdefs
QTextStream qstd::cin(stdin, QIODevice::ReadOnly);
QTextStream qstd::cout(stdout, QIODevice::WriteOnly);
QTextStream qstd::cerr(stderr, QIODevice::WriteOnly);
//end
/* Namespace members are like static class members */
bool qstd::yes(QString question) {
QString ans;
cout << QString(" %1 [y/n]? ").arg(question);
cout.flush();
ans = cin.readLine();
return (ans.toUpper().startsWith("Y", Qt::CaseInsensitive));
}
//end
bool qstd::more(QString s) {
return yes(QString("Another %1").arg(s));
}
int qstd::promptInt(int base /* =10 */) { /* Usage: int n = promptInt(); */
QString numstr;
int result;
bool ok;
cout << ": " << flush;
while (1) {
numstr = cin.readLine();
result = numstr.toInt(&ok, base);
if (!ok) {
cout << "Invalid number. Try again: ";
cout.flush();
}
else
return result;
}
}
double qstd::promptDouble() { /* Usage: double d = promptDouble(); */
QString numstr;
double result;
bool ok;
while (1) {
numstr = cin.readLine();
result = numstr.toDouble(&ok);
if (!ok) {
cout << "Invalid number. Try again: ";
cout.flush();
}
else
return result;
}
}
void qstd::promptOutputFile(QFile& outfile) {
QString filename;
while (1) {
cout << "Please enter the file name for saving this data: ";
cout.flush();
filename = cin.readLine();
outfile.setFileName(filename);
bool fileExists = outfile.open(QIODevice::ReadOnly);
if (!fileExists)
break;
if (yes("File already exists ... Ok to overwrite"))
break;
outfile.close();
outfile.reset();
}
outfile.close();
outfile.reset();
outfile.open(QIODevice::WriteOnly);
cout << filename << " open for writing ...\n";
cout.flush();
}
void qstd::promptInputFile(QFile& infile) {
QString filename;
while (1) {
cout << "Name of the file to be read: ";
cout.flush();
filename = cin.readLine();
infile.setFileName(filename);
bool fileExists = infile.open(QIODevice::ReadOnly);
if (fileExists)
break;
cout << "File does not exist ... Please try again. \n";
cout.flush();
infile.reset();
}
cout << filename << " open for reading ...\n";
cout.flush();
}
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
Bookmarks