By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,650 Members | 1,778 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,650 IT Pros & Developers. It's quick & easy.

How to avoid mixing processing and interfaces - How to replace cout?

P: 2
I bet that others came across this problem and that there is a very good general solution, however I have not been able to get this done.

In short: How to replace std::cout by something that writes in a dialog box without mixing my code with some monstrous class library such as in Qt4, or for Windows Microsoft's class libraries.

The details of this implementation should not matter for this question. The only thing that matters is that I've got a bunch of C++ classes and one function that uses all the methods in there to parse an input string according to a particular grammar, and spit out a new string (or several). Let's call it the pattern-machine, and it works just fine, so far. Before I proceed developing it, I want to make it part of a system that makes use of dialog windows. In particular, I want to use Qt4 (under Linux/Fedora but convertible to windows).

Reading around in the examples and tutorials, I find that Qt4 basically has it's own standard library, but now everything is called a Q-something, QString, QList, Qwhatever. The claim is that std-lib and Qt4 can all be mixed but how to convert objects between the two isn't clear at all: What if I don't even want to change any of my already developed include files, where all is based on the standard C++ library?

I implemented everything so far simply and exclusively using standard output via std::cout, and also file output. Currently I would like to just replace std::cout by something from a different system. I thought I could just include a reference of type ostream& in my class as a member and write everything to that object, but the compiler doesn't allow this, and I think I'm on the wrong path.

Eagerly awaiting pearls of wisdom......
Jun 15 '07 #1
Share this Question
Share on Google+
3 Replies


weaknessforcats
Expert Mod 5K+
P: 9,197
You can replace std::cout with anything you want provided:

1) your class derives from ostream.
2) implements a char data stream

Sounds like you re re-inventing Windows. Maybe not such a good idea.
Jun 15 '07 #2

P: 2
Sounds like you re re-inventing Windows. Maybe not such a good idea.
That wasn't really my plan :-)

I read a little bit more in the meantime on how to integrate code with Qt4 (Troll tech). I guess this is how I will proceed: In my class I'll replace all occurrences of "cout << something" by a similar operation to a string stream class. So I have to find some object in the Troll's forest of some 5000 Qobjects that can accept these strings and display them in a window (looks like QtextEditor could be abused for that), alternatively I can also use directly one of their Qtextstream classes which looks just like cout, and hook that up with output to a window. Looks like a problem is then to be able to convert std::strings into Qt strings and back (seems trivial but may not be, I saw some discussions about this elsewhere ).

So I'll end up having to write a piece of code that includes my headers and the Qt headers to make an object which inherits both my main class and a Qt class. This beast then should be able to receive and emit signals, as well as fetch and send strings (and convert from one version of strings to the other back and forth). An objective is to not have a single line of extra code that has to do with Qt in the code I ready wrote. Let's see how much headache this is going to bee.....
Jun 15 '07 #3

weaknessforcats
Expert Mod 5K+
P: 9,197
Quote:
Originally Posted by weaknessforcats
Sounds like you re re-inventing Windows. Maybe not such a good idea.


That wasn't really my plan :-)
There is a book: "Programming the STL" that might help you.
Jun 16 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.