473,399 Members | 3,832 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Problem with Global Variable Declarations (QT)

I have a problem I've been working on for days now with global variable
use. I'm currently using QT for development but this same problem
should apply to anytime a C++ object is trying to be used globally.
Just assume the QT stuff is fine and that they are predefined library
objects. Anyway, I want to have a global QTextStream object that I use
to print debug information to. This program compiles fine, but if I
ever try to use my global QTextStream object it gives me the error:
"binary '<<' : no operator found which takes a left-hand operand of
type 'QTextStream'"
Any suggestions would be much appreciated. Thanks,
Chris

Here's all the relevant code:

DEBUG.H:

#ifndef DEBUG_H
#define DEBUG_H

#include <QMainWindow>
#include <QLabel>
#include <QTextStream>
#include <QSTring>
#include <QBuffer>

static QTextStream debugout;

class Debug : public QMainWindow
{
Q_OBJECT

public:
QLabel debugLabel;
QByteArray debugArray;
QBuffer debugBuffer;
Debug();

private:

private slots:
void textWritten(int numBytes);
};

#endif

DEBUG.CPP:

#include "Debug.h"

Debug::Debug()
{
setCentralWidget(&debugLabel);
debugout.setDevice(&debugBuffer);

connect(&debugBuffer, SIGNAL(bytesWritten(int)), this,
SLOT(textWritten(int)));
}

void Debug::textWritten(int numBytes)
{
QByteArray newText = debugBuffer.readAll();
debugArray.append(newText);
debugLabel.setText(debugArray);
}

Dec 5 '06 #1
2 10987
Chris Portka wrote:
I have a problem I've been working on for days now with global variable
use. I'm currently using QT for development but this same problem
should apply to anytime a C++ object is trying to be used globally.
Just assume the QT stuff is fine and that they are predefined library
objects. Anyway, I want to have a global QTextStream object that I use
to print debug information to. This program compiles fine, but if I
ever try to use my global QTextStream object it gives me the error:
"binary '<<' : no operator found which takes a left-hand operand of
type 'QTextStream'"
Any suggestions would be much appreciated. Thanks,
Chris

Here's all the relevant code:

DEBUG.H:

#ifndef DEBUG_H
#define DEBUG_H

#include <QMainWindow>
#include <QLabel>
#include <QTextStream>
#include <QSTring>
#include <QBuffer>

static QTextStream debugout;
Suggestion #1) Don't do that. Look at the docs for QDebug.
Suggestion #2) Post to qt*********@trolltech.com Your question really is
Qt-specific.
Suggestion #3) Don't do that. Globals are ugly.
Suggestion #4) http://doc.trolltech.com/4.2/qtextst...tml#setPadChar

QString s;
QTextStream out(&s);
out.setFieldWidth(10);
out.setPadChar('-');
out << "Qt" << endl << "rocks!" << endl;
--
NOUN:1. Money or property bequeathed to another by will. 2. Something handed
down from an ancestor or a predecessor or from the past: a legacy of
religious freedom. ETYMOLOGY: MidE legacie, office of a deputy, from OF,
from ML legatia, from L legare, to depute, bequeath. www.bartleby.com/61/
Dec 5 '06 #2
Steven T. Hatton a écrit :
Chris Portka wrote:
>I have a problem I've been working on for days now with global variable
use. I'm currently using QT for development but this same problem
should apply to anytime a C++ object is trying to be used globally.
Just assume the QT stuff is fine and that they are predefined library
objects. Anyway, I want to have a global QTextStream object that I use
to print debug information to. This program compiles fine, but if I
ever try to use my global QTextStream object it gives me the error:
"binary '<<' : no operator found which takes a left-hand operand of
type 'QTextStream'"
Any suggestions would be much appreciated. Thanks,
Chris

Here's all the relevant code:

DEBUG.H:

#ifndef DEBUG_H
#define DEBUG_H

#include <QMainWindow>
#include <QLabel>
#include <QTextStream>
#include <QSTring>
#include <QBuffer>

static QTextStream debugout;

[...]
Suggestion #3) Don't do that. Globals are ugly.
[...]

Yes, ugly. Especially declared static in a header file.

Use static within a class to make it global to the class but otherwise,
use extern.

in debug.hpp:
....
extern QTextStream debugout;
....

in debug.cpp
....
QTextStream debugout;
....

Dec 5 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Clifford W. Racz | last post by:
Has anyone solved the issue of translating lists in Word 2003 (WordML) into xHTML? I have been trying to get the nested table code for my XSLT to work for a while now, with no way to get the...
10
by: Kleenex | last post by:
Reason: I am working on an embedded project which has very limited memory (under 512 bytes, 60 or so of which is stack space), which translates into limited stack space. In order to save on stack...
2
by: ajikoe | last post by:
Hi, I tried to follow the example in swig homepage. I found error which I don't understand. I use bcc32, I already include directory where my python.h exist in bcc32.cfg. /* File : example.c...
6
by: shaun | last post by:
If I put (define) const variables at the top of a .cpp file but do not declare them in the .h file, are they only visible within that .cpp file? e.g. const int a={1,2,3,4}; in a cpp file...
7
by: zeecanvas | last post by:
Hi, First of all: Yes, I know global variables are bad, but I've a huge amount of legacy code, and I've to maintain it _as_is_. I'm maintaining a big program. I moved all (program-wide scope)...
16
by: Roman Ziak | last post by:
Hello, there were times when I used to be looking for a way to access JavaScript Global object similar to those found in VBScript or PHP ($GLOBALS). At present this has only academic value for...
9
by: Ed Jensen | last post by:
I'm having a vexing problem with global variables in Python. Please consider the following Python code: #! /usr/bin/env python def tiny(): bar = for tmp in foo: bar.append(tmp) foo = bar
112
by: istillshine | last post by:
When I control if I print messages, I usually use a global variable "int silent". When I set "-silent" flag in my command line parameters, I set silent = 1 in my main.c. I have many functions...
1
by: Jaco Naude | last post by:
Hi, I'm using a static library in my application which links fine except for a few global variables. The static library only contains a bunch of .cpp and .h files and the global variables are...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.