LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-16-2008, 04:30 PM   #1
iwasapenguin
Member
 
Registered: Jul 2007
Posts: 110

Rep: Reputation: 15
Using a pointer to a QTextStream & QFile


I've tried the QT forums for this but the KDE development section
looks like a ghost town.

The problem is that I'm attempting to have a class that holds a
QFile, QTextStream and KTextEdit and will save the file when
a button is pressed. These are in a QPtrList and displayed on
a QTabWidget.

The trouble is though that I can't compile the code without the QFile
being declared in the class constructor but it also needs to be accessed
by a slot to save the file and the same runs for the QTextStream.

I attempted to use pointers to fix this but the line:
Code:
streamPtr->operator<<( filePtr )
caused a seg fault (though I didn't expect much of it).

Would anybody know how to either get the pointer method to work
or better approach?
 
Old 04-16-2008, 06:37 PM   #2
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
This may not be a ghost town but do we have the right sign posts?

I'm not certain if I understand the problem but are you struggling with compile errors about the redeclaration of QFile?

If so you will need to first check your header guards, next if all of those are in place correctly do a forward declaration of your QFile object.

Maybe posting the relevant code with the error will help.
 
Old 04-16-2008, 10:52 PM   #3
iwasapenguin
Member
 
Registered: Jul 2007
Posts: 110

Original Poster
Rep: Reputation: 15
This is why newbies (i.e. me) shouldn't try to code semi-ide's...

From bridgetextedit.h
Code:
class bridgeTextEdit : public QObject {
Q_OBJECT
public:
	bridgeTextEdit( QTabWidget *parent, QDomElement fileElement, bridgeMainWindow *window  );
	virtual ~bridgeTextEdit();

	QVBox *controll;
	QLabel *label;
	KTextEdit *editField;

public slots:
	void slotSaveFile();
	void slotSaveFileAs();

private:
	QTextStream *streamPtr;
};
from bridgetextedit.cpp
Code:
bridgeTextEdit::bridgeTextEdit( QTabWidget *parent, QDomElement fileElement, bridgeMainWindow *window )
	: QObject()
{

	//initialise the gui
	controll = new QVBox( parent );

	label = new QLabel( controll );
	label->setText( fileElement.attribute("descrip") );

	editField = new KTextEdit( controll );
	editField->setMinimumHeight( 600 );
	editField->setMinimumWidth( 800 );

	parent->addTab( controll, fileElement.attribute("url"));

	//all file realted isuses below
	QFile file( fileElement.attribute("url") );
	if ( !file.open( IO_ReadWrite ) )
		goto error;
	else{
		 QTextStream stream( &file );
		 editField->setText( stream.read() );

		 streamPtr = &stream;
	}

...

void bridgeTextEdit::slotSaveFile()
{
        streamPtr->operator<<( file );
	editField->setModified( false );
}
Or something like that. I ripped out the offending code so that
I could work on other parts of it in the mean time.
 
Old 04-18-2008, 03:55 PM   #4
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
I see two possible things: first one is the set of includes you have. You don't show them, so it's hard to say that's the reason. Also, I would be careful with declaring QT-based classes as local variables:
Code:
QFile file( fileElement.attribute("url") );
Instead, I'd rather write
Code:
QFile *file = new QFile( fileElement.attribute("url") );
If it doesn't help, show us the exact compilation error you get.
 
Old 04-20-2008, 12:21 AM   #5
iwasapenguin
Member
 
Registered: Jul 2007
Posts: 110

Original Poster
Rep: Reputation: 15
here's the output from make when I try to have a function that uses pointers to streams and
files...

g++ -c bridgetextedit.cpp -o bridgetextedit.o -I/usr/lib/qt-3.3/include -I/usr/include/kde -g -Wall
...
bridgetextedit.cpp: In member function ‘void bridgeTextEdit::slotSaveFile()’:
bridgetextedit.cpp:63: error: no match for ‘operator<<’ in ‘((bridgeTextEdit*)this)->bridgeTextEdit::streamPtr << QTextEdit::text() const()’

... And the matching code...

(From the class declaration)
Code:
class bridgeTextEdit : public QObject {
Q_OBJECT
public:
	bridgeTextEdit( QTabWidget *parent, QDomElement fileElement, bridgeMainWindow *window  );
	virtual ~bridgeTextEdit();

	QVBox *controll;
	QLabel *label;
	KTextEdit *editField;

public slots:
	void slotSaveFile();
	void slotSaveFileAs();

private:
	QFile *filePtr;
	QTextStream *streamPtr;
};
(from the constructor)
Code:
filePtr = new QFile( fileElement.attribute("url") );
	if ( !file->open( IO_ReadWrite ) )
		goto error;
	else{
		 streamPtr  = new QFile( file );
		 editField->setText( stream->read() );
	}
(and from the save function)
Code:
void bridgeTextEdit::slotSaveFile()
{

	streamPtr << editField->text();
	editField->setModified( false );
}
 
Old 04-21-2008, 02:09 PM   #6
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
This doesn't compile:
Code:
    QTextStream *ts = new QTextStream();
    ts << "blah";
But this one does:
Code:
    QTextStream *ts = new QTextStream();
    *ts << "blah";
It's exactly the same in your case.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Kded and Qfile::Open Murdock1979 Linux - Software 2 05-01-2006 03:40 PM
Slackware 10.2 & Thinkpad Pointer. TheGreatGonzo Slackware - Installation 19 03-22-2006 09:51 PM
X11 complaining about QFile no file name Riddick Linux - Software 2 09-07-2005 12:00 PM
returning data to main() via a pointer to a pointer. slzckboy Programming 3 05-30-2005 01:20 PM
hot to set value of pointer to pointer to a structure in C alix123 Programming 2 11-17-2004 06:40 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:27 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration