PDA

View Full Version : QFile as an argument of a function?



Stanfillirenfro
2nd February 2013, 12:33
I have a problem again and I would be greteful to everybody who could help me.

I have a fucntion in a class that I want to use in another without having to rewrite it again. Th eidea is to open a text document from and to work with it in different windows.

May I explain a bit?
1- In First class.cpp (line 13), I am geting the document I want to work with.
2- In second class.cpp (line 10), I retrieve the resulat of the operation and save in list.

My code is just not compiling, and I am suspecting the argument in second class.cpp (line 10) to be wrong, but I have reach my limit at this point.

Any help would be welcome.

Many thanks in advance!

Here is my code:

First class.h


#ifndef DEF_HERITAGE
#define DEF_HERITAGE

#include <QtGui>


class Heritage: public QMainWindow
{
Q_OBJECT

public:
explicit Heritage(QWidget *parent = 0);
virtual ~Heritage();

public slots:
void operation1(QTextDocument &doc);

private:
QLineEdit *m_openFile;



};

First class.cpp

#include "Heritage.h"
#include "ChildWindow.h"

Heritage::Heritage(QWidget *parent)
{
m_openFile = new QLineEdit;
setWindowTitle("Heritage");
setCentralWidget(centralWidget);
}

void Heritage::operation1(QTextDocument &doc)
{
QFile file1(m_openFile->text());
....
}

Second class.h

#ifndef DEF_CHILDWINDOW
#define DEF_CHILDWINDOW

#include<QtGui>
#include"Heritage"

class ChildWindow: public QWidget
{
Q_OBJECT

public:
explicit ChildWindow(QWidget *parent = 0);
virtual ~ChildWindow();

public slots:
void operation2(Heritage &aFile, QStringList &list);

private:

};

#endif // CHILDWINDOW_H

Second class.cpp

#include "ChildWindow.h"
#include "Heritage.h"

ChildWindow::ChildWindow(QWidget *parent)
: QWidget(parent)
{
}
void ChildWindow::operation2(Heritage &aFile, QStringList &list)
{
list = aFile.operation1(m_openFile->document());
}


ChildWindow::~ChildWindow()
{
;
}

wysota
2nd February 2013, 12:44
What is the error you are getting?

Stanfillirenfro
2nd February 2013, 12:50
Hi Wysota!
I have the following error message:
erreur : no matching function for call to 'Heritage::operation1(QTextDocument*)'
candidates are: void Heritage::operation1(QTextDocument&)

Of course, it is asking me a reference to a text instead of a pointer. The problem is that it is not clear for me to declare a fonction with an argument as pointer in this case.

wysota
2nd February 2013, 12:52
Either convert that document to a reference by using *(m_openFile->document()) or (better) declare the function to accept a pointer: operation1(QTextDocument *doc).

Stanfillirenfro
2nd February 2013, 12:58
After declaring the function to accept the pointer, I have the following message:
erreur : no match for 'operator=' in 'list = aFile->Heritage::operation1(((Heritage*)this)->Heritage::m_openFile->QTextEdit::document())'
candidates are: QStringList& QStringList::operator=(const QStringList&)

Your help would be welcome

Santosh Reddy
2nd February 2013, 13:00
m_openFile seems to be QLineEdit, and it cannot be converted to QTextDocuent.

You need to create a QTextDocument, which you are missing.

Stanfillirenfro
2nd February 2013, 13:18
I have created a QTextDocument, but I have the following message:

erreur : 'class QTextDocument' has no member named 'document'

The problem is that I do not know with what I can change document in the line 10 of second class.cpp

wysota
2nd February 2013, 13:24
Maybe you should start by telling us what you are trying to achieve?

Stanfillirenfro
2nd February 2013, 13:32
Here is the idea:
I want to modofy a text document in different ways. Coloring it, changing color, resizing it... For that, I want to carry out each operation in a single window. So I want to create a function which will be responsible of opening the text document and preparing it before each above mentioned operation starts. For example, the gap between lines should be removed... In order to avoid opening the same file and proceding the preperation every time, I have decided to perform this step in a seperate window and just to call the function everytime I have to perform an operation, without rewriting the code again. That is the idea.
The real problem is to find and argument for the same function in another window when this function is called. That is the problem I have at the line 10 of second class.cpp

Any help would be welcome.

wysota
2nd February 2013, 13:48
Ok but so far you only seem to have a QLineEdit and no text document. Assuming the line edit holds the path to a file containing the document, you should start by opening the file and reading the document from it.

Stanfillirenfro
2nd February 2013, 13:55
You are wright Wysota!
Also, that is what I've done in the first class (first class.cpp, line 13). I mean, I've opened the document here. Should I also do that in the second class? If yes, it is what I wanted to avoid.

Santosh Reddy
2nd February 2013, 14:47
Do this


QStringList Heritage::operation1(QString fileName)
{
QStringList list;
QFile file1(fileName);
//pupolate list
return list;
}

void ChildWindow::operation2(Heritage &aFile, QStringList &list)
{
list = aFile.operation1(m_openFile->text());
}

Stanfillirenfro
2nd February 2013, 15:27
Hi Reddy!

It is difficult and complicate, since in QFile file1(m_openFile->text()), I am showing the path to the file to be used. In the decalration of the function, we have QString fileName, which will not be used in this case. Moreover, when I am testing if the file can be opened (if(!file1.open(QIODevice::ReadOnly)), with the return after the line of QMessageBox, I have the following error: "error : return-statement with no value, in function returning 'QStringList'"

Questions:
1- How to indicated the path to the file to be used in the declaration of the function in the heather file?
2- How to overcome the problem leading to the error message?
3- How to sole the entire problem?

Many thanks!

I have the following modified code:

first class.h

#ifndef DEF_HERITAGE
#define DEF_HERITAGE

#include <QtGui>


class Heritage: public QMainWindow
{
Q_OBJECT

public:
explicit Heritage(QWidget *parent = 0);
virtual ~Heritage();

public slots:
QStringList operation1(QString fileName);

private:
QLineEdit *m_openFile;


};

First class.cpp

#include "Heritage.h"
#include "ChildWindow.h"

Heritage::Heritage(QWidget *parent)
{
m_openFile = new QLineEdit;
setWindowTitle("Heritage");
setCentralWidget(centralWidget);
}

QStringList Heritage::operation1(QString fileName)
{

QFile file1(m_openFile->text());
QStringList list;
if(!file1.open(QIODevice::ReadOnly))
{
QMessageBox::critical(m_openFile, "Warning", "Open a file please");
return;
}
....
return list;
}

Second class.h

#ifndef DEF_CHILDWINDOW
#define DEF_CHILDWINDOW

#include<QtGui>
#include"Heritage"

class ChildWindow: public QWidget
{
Q_OBJECT

public:
explicit ChildWindow(QWidget *parent = 0);
virtual ~ChildWindow();

public slots:
void operation2(Heritage &aFile, QStringList &list);

private:

};

#endif // CHILDWINDOW_H

Second class.cpp

#include "ChildWindow.h"
#include "Heritage.h"

ChildWindow::ChildWindow(QWidget *parent)
: QWidget(parent)
{
}
void ChildWindow::operation2(Heritage &aFile, QStringList &list)
{
list = aFile.operation1(m_openFile->text());
}


ChildWindow::~ChildWindow()
{
;
}

Santosh Reddy
2nd February 2013, 16:05
just return empty list


if(!file1.open(QIODevice::ReadOnly))
{
QMessageBox::critical(m_openFile, "Warning", "Open a file please");
return list;
}

Stanfillirenfro
2nd February 2013, 16:14
Many thanks Reddy for yor help!
The error message has dissapear now, but another problem remains. Please have a look on first class.cpp, line 11 in my previous post. fileName is not used at all. If I delete it, it will not compile.
Do you have an idea how to solve the problem?

Santosh Reddy
2nd February 2013, 16:25
If filename is not used, how does ChildWindow tell Heritage which file to open?

Stanfillirenfro
2nd February 2013, 16:39
Maybe I was not clear.
In my code, at the level of first class.cpp line 11, we have:
QStringList Heritage::operation1(QString fileName) On the line 14, we have:

QFile file1(m_openFile->text());
m_openFile->text() replaces what you called in your code fileName, and what I've added here too. But the variable "fileName" is not used. It was my problem.
To answer to your qestion, according to me, ChildWindow shows the path to the file to be opned to Heritage with the expression: m_openFile->text().
Could you please clarify this situation? I would be grateful to you.

Santosh Reddy
2nd February 2013, 16:53
I think you are missing some basic concepts.

you need to open "filename", not "m_openFile->text()",

Stanfillirenfro
2nd February 2013, 17:15
Many thanks Reddy!
It is working now.
One more time, many thanks for your advices and help.