QFile as an argument of a function?
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
Code:
#ifndef DEF_HERITAGE
#define DEF_HERITAGE
#include <QtGui>
{
Q_OBJECT
public:
explicit Heritage
(QWidget *parent
= 0);
virtual ~Heritage();
public slots:
private:
};
First class.cpp
Code:
#include "Heritage.h"
#include "ChildWindow.h"
Heritage
::Heritage(QWidget *parent
){
setWindowTitle("Heritage");
setCentralWidget(centralWidget);
}
{
QFile file1
(m_openFile
->text
());
....
}
Second class.h
Code:
#ifndef DEF_CHILDWINDOW
#define DEF_CHILDWINDOW
#include<QtGui>
#include"Heritage"
{
Q_OBJECT
public:
explicit ChildWindow
(QWidget *parent
= 0);
virtual ~ChildWindow();
public slots:
private:
};
#endif // CHILDWINDOW_H
Second class.cpp
Code:
#include "ChildWindow.h"
#include "Heritage.h"
ChildWindow
::ChildWindow(QWidget *parent
) {
}
void ChildWindow
::operation2(Heritage
&aFile,
QStringList &list
) {
list = aFile.operation1(m_openFile->document());
}
ChildWindow::~ChildWindow()
{
;
}
Re: QFile as an argument of a function?
What is the error you are getting?
Re: QFile as an argument of a function?
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.
Re: QFile as an argument of a function?
Either convert that document to a reference by using *(m_openFile->document()) or (better) declare the function to accept a pointer: operation1(QTextDocument *doc).
Re: QFile as an argument of a function?
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
Re: QFile as an argument of a function?
m_openFile seems to be QLineEdit, and it cannot be converted to QTextDocuent.
You need to create a QTextDocument, which you are missing.
Re: QFile as an argument of a function?
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
Re: QFile as an argument of a function?
Maybe you should start by telling us what you are trying to achieve?
Re: QFile as an argument of a function?
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.
Re: QFile as an argument of a function?
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.
Re: QFile as an argument of a function?
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.
Re: QFile as an argument of a function?
Do this
Code:
{
//pupolate list
return list;
}
void ChildWindow
::operation2(Heritage
&aFile,
QStringList &list
) {
list = aFile.operation1(m_openFile->text());
}
Re: QFile as an argument of a function?
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
Code:
#ifndef DEF_HERITAGE
#define DEF_HERITAGE
#include <QtGui>
{
Q_OBJECT
public:
explicit Heritage
(QWidget *parent
= 0);
virtual ~Heritage();
public slots:
private:
};
First class.cpp
Code:
#include "Heritage.h"
#include "ChildWindow.h"
Heritage
::Heritage(QWidget *parent
) {
setWindowTitle("Heritage");
setCentralWidget(centralWidget);
}
{
QFile file1
(m_openFile
->text
());
{
QMessageBox::critical(m_openFile,
"Warning",
"Open a file please");
return;
}
....
return list;
}
Second class.h
Code:
#ifndef DEF_CHILDWINDOW
#define DEF_CHILDWINDOW
#include<QtGui>
#include"Heritage"
{
Q_OBJECT
public:
explicit ChildWindow
(QWidget *parent
= 0);
virtual ~ChildWindow();
public slots:
private:
};
#endif // CHILDWINDOW_H
Second class.cpp
Code:
#include "ChildWindow.h"
#include "Heritage.h"
ChildWindow
::ChildWindow(QWidget *parent
) {
}
void ChildWindow
::operation2(Heritage
&aFile,
QStringList &list
) {
list = aFile.operation1(m_openFile->text());
}
ChildWindow::~ChildWindow()
{
;
}
Re: QFile as an argument of a function?
just return empty list
Code:
{
QMessageBox::critical(m_openFile,
"Warning",
"Open a file please");
return list;
}
Re: QFile as an argument of a function?
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?
Re: QFile as an argument of a function?
If filename is not used, how does ChildWindow tell Heritage which file to open?
Re: QFile as an argument of a function?
Maybe I was not clear.
In my code, at the level of first class.cpp line 11, we have: On the line 14, we have:
Code:
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.
Re: QFile as an argument of a function?
I think you are missing some basic concepts.
you need to open "filename", not "m_openFile->text()",
Re: QFile as an argument of a function?
Many thanks Reddy!
It is working now.
One more time, many thanks for your advices and help.