Results 1 to 19 of 19

Thread: QFile as an argument of a function?

  1. #1
    Join Date
    Jan 2012
    Posts
    83
    Qt products
    Qt4
    Platforms
    Windows

    Default 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

    Qt Code:
    1. #ifndef DEF_HERITAGE
    2. #define DEF_HERITAGE
    3.  
    4. #include <QtGui>
    5.  
    6.  
    7. class Heritage: public QMainWindow
    8. {
    9. Q_OBJECT
    10.  
    11. public:
    12. explicit Heritage(QWidget *parent = 0);
    13. virtual ~Heritage();
    14.  
    15. public slots:
    16. void operation1(QTextDocument &doc);
    17.  
    18. private:
    19. QLineEdit *m_openFile;
    20.  
    21.  
    22.  
    23. };
    To copy to clipboard, switch view to plain text mode 

    First class.cpp
    Qt Code:
    1. #include "Heritage.h"
    2. #include "ChildWindow.h"
    3.  
    4. Heritage::Heritage(QWidget *parent)
    5. {
    6. m_openFile = new QLineEdit;
    7. setWindowTitle("Heritage");
    8. setCentralWidget(centralWidget);
    9. }
    10.  
    11. void Heritage::operation1(QTextDocument &doc)
    12. {
    13. QFile file1(m_openFile->text());
    14. ....
    15. }
    To copy to clipboard, switch view to plain text mode 

    Second class.h
    Qt Code:
    1. #ifndef DEF_CHILDWINDOW
    2. #define DEF_CHILDWINDOW
    3.  
    4. #include<QtGui>
    5. #include"Heritage"
    6.  
    7. class ChildWindow: public QWidget
    8. {
    9. Q_OBJECT
    10.  
    11. public:
    12. explicit ChildWindow(QWidget *parent = 0);
    13. virtual ~ChildWindow();
    14.  
    15. public slots:
    16. void operation2(Heritage &aFile, QStringList &list);
    17.  
    18. private:
    19.  
    20. };
    21.  
    22. #endif // CHILDWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    Second class.cpp
    Qt Code:
    1. #include "ChildWindow.h"
    2. #include "Heritage.h"
    3.  
    4. ChildWindow::ChildWindow(QWidget *parent)
    5. : QWidget(parent)
    6. {
    7. }
    8. void ChildWindow::operation2(Heritage &aFile, QStringList &list)
    9. {
    10. list = aFile.operation1(m_openFile->document());
    11. }
    12.  
    13.  
    14. ChildWindow::~ChildWindow()
    15. {
    16. ;
    17. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QFile as an argument of a function?

    What is the error you are getting?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jan 2012
    Posts
    83
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QFile as an argument of a function?

    Hi Wysota!
    I have the following error message:
    erreur : no matching function for call to 'Heritage:peration1(QTextDocument*)'
    candidates are: void Heritage:peration1(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.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default 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).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Jan 2012
    Posts
    83
    Qt products
    Qt4
    Platforms
    Windows

    Default 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:peration1(((Heritage*)this)->Heritage::m_openFile->QTextEdit::document())'
    candidates are: QStringList& QStringList:perator=(const QStringList&)

    Your help would be welcome

  6. #6
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default 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.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  7. #7
    Join Date
    Jan 2012
    Posts
    83
    Qt products
    Qt4
    Platforms
    Windows

    Default 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

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QFile as an argument of a function?

    Maybe you should start by telling us what you are trying to achieve?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Jan 2012
    Posts
    83
    Qt products
    Qt4
    Platforms
    Windows

    Default 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.

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default 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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. #11
    Join Date
    Jan 2012
    Posts
    83
    Qt products
    Qt4
    Platforms
    Windows

    Default 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.

  12. #12
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: QFile as an argument of a function?

    Do this
    Qt Code:
    1. QStringList Heritage::operation1(QString fileName)
    2. {
    3. QFile file1(fileName);
    4. //pupolate list
    5. return list;
    6. }
    7.  
    8. void ChildWindow::operation2(Heritage &aFile, QStringList &list)
    9. {
    10. list = aFile.operation1(m_openFile->text());
    11. }
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  13. #13
    Join Date
    Jan 2012
    Posts
    83
    Qt products
    Qt4
    Platforms
    Windows

    Default 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
    Qt Code:
    1. #ifndef DEF_HERITAGE
    2. #define DEF_HERITAGE
    3.  
    4. #include <QtGui>
    5.  
    6.  
    7. class Heritage: public QMainWindow
    8. {
    9. Q_OBJECT
    10.  
    11. public:
    12. explicit Heritage(QWidget *parent = 0);
    13. virtual ~Heritage();
    14.  
    15. public slots:
    16. QStringList operation1(QString fileName);
    17.  
    18. private:
    19. QLineEdit *m_openFile;
    20.  
    21.  
    22. };
    To copy to clipboard, switch view to plain text mode 

    First class.cpp
    Qt Code:
    1. #include "Heritage.h"
    2. #include "ChildWindow.h"
    3.  
    4. Heritage::Heritage(QWidget *parent)
    5. {
    6. m_openFile = new QLineEdit;
    7. setWindowTitle("Heritage");
    8. setCentralWidget(centralWidget);
    9. }
    10.  
    11. QStringList Heritage::operation1(QString fileName)
    12. {
    13.  
    14. QFile file1(m_openFile->text());
    15. if(!file1.open(QIODevice::ReadOnly))
    16. {
    17. QMessageBox::critical(m_openFile, "Warning", "Open a file please");
    18. return;
    19. }
    20. ....
    21. return list;
    22. }
    To copy to clipboard, switch view to plain text mode 

    Second class.h
    Qt Code:
    1. #ifndef DEF_CHILDWINDOW
    2. #define DEF_CHILDWINDOW
    3.  
    4. #include<QtGui>
    5. #include"Heritage"
    6.  
    7. class ChildWindow: public QWidget
    8. {
    9. Q_OBJECT
    10.  
    11. public:
    12. explicit ChildWindow(QWidget *parent = 0);
    13. virtual ~ChildWindow();
    14.  
    15. public slots:
    16. void operation2(Heritage &aFile, QStringList &list);
    17.  
    18. private:
    19.  
    20. };
    21.  
    22. #endif // CHILDWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    Second class.cpp
    Qt Code:
    1. #include "ChildWindow.h"
    2. #include "Heritage.h"
    3.  
    4. ChildWindow::ChildWindow(QWidget *parent)
    5. : QWidget(parent)
    6. {
    7. }
    8. void ChildWindow::operation2(Heritage &aFile, QStringList &list)
    9. {
    10. list = aFile.operation1(m_openFile->text());
    11. }
    12.  
    13.  
    14. ChildWindow::~ChildWindow()
    15. {
    16. ;
    17. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by Stanfillirenfro; 2nd February 2013 at 15:52.

  14. #14
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: QFile as an argument of a function?

    just return empty list
    Qt Code:
    1. if(!file1.open(QIODevice::ReadOnly))
    2. {
    3. QMessageBox::critical(m_openFile, "Warning", "Open a file please");
    4. return list;
    5. }
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  15. #15
    Join Date
    Jan 2012
    Posts
    83
    Qt products
    Qt4
    Platforms
    Windows

    Default 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?

  16. #16
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: QFile as an argument of a function?

    If filename is not used, how does ChildWindow tell Heritage which file to open?
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  17. #17
    Join Date
    Jan 2012
    Posts
    83
    Qt products
    Qt4
    Platforms
    Windows

    Default 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:
    Qt Code:
    1. QStringList Heritage::operation1(QString fileName)
    To copy to clipboard, switch view to plain text mode 
    On the line 14, we have:
    Qt Code:
    1. QFile file1(m_openFile->text());
    To copy to clipboard, switch view to plain text mode 
    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.

  18. #18
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default 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()",
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  19. #19
    Join Date
    Jan 2012
    Posts
    83
    Qt products
    Qt4
    Platforms
    Windows

    Default 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.
    Last edited by Stanfillirenfro; 2nd February 2013 at 17:24.

Similar Threads

  1. How to pass an argument to evaluateJavaScript function?
    By TheIndependentAquarius in forum Qt Programming
    Replies: 5
    Last Post: 5th January 2013, 09:45
  2. Function: ... Argument
    By sonulohani in forum General Programming
    Replies: 3
    Last Post: 15th December 2012, 15:55
  3. Invoking dll function with argument crashed
    By moiit in forum Qt Programming
    Replies: 13
    Last Post: 7th March 2012, 14:34
  4. QVector as function argument
    By stefan in forum Qt Programming
    Replies: 4
    Last Post: 12th May 2011, 12:40
  5. QMap as function argument...
    By cydside in forum Qt Programming
    Replies: 5
    Last Post: 18th April 2009, 17:59

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.