Results 1 to 19 of 19

Thread: QFile as an argument of a function?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 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.


  3. #3
    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.

  4. #4
    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.

  5. #5
    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.

  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?

    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.

  7. #7
    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?

  8. #8
    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.

  9. #9
    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.

  10. #10
    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.

  11. #11
    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
  •  
Qt is a trademark of The Qt Company.