Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Problem with Global Variable Declarations (QT)

Reply
Thread Tools

Problem with Global Variable Declarations (QT)

 
 
Chris Portka
Guest
Posts: n/a
 
      12-05-2006
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:ebug()
{
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);
}

 
Reply With Quote
 
 
 
 
Steven T. Hatton
Guest
Posts: n/a
 
      12-05-2006
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 http://www.velocityreviews.com/forums/(E-Mail Removed) 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/
 
Reply With Quote
 
 
 
 
Michael DOUBEZ
Guest
Posts: n/a
 
      12-05-2006
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;
....

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
shortcut for large amount of global var declarations? Alex Hall Python 5 05-11-2010 05:46 AM
FWSM/PIX and Dynamic PAT using global IP range vs. global interface vs. global IP Hoffa Cisco 1 10-25-2006 06:50 PM
FWSM/PIX and Dynamic PAT using global IP range vs. global interface vs. global IP Hoffa Cisco 0 10-25-2006 01:04 PM
function declarations, global/within a function? Douglas C Programming 2 07-05-2004 08:54 PM
global struct declarations Elliot Marks C Programming 20 01-10-2004 02:31 PM



Advertisments