Results 1 to 14 of 14

Thread: Using text document ALREADY uploaded on another window

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

    Default Using text document ALREADY uploaded on another window

    Hello!
    I have two windows in an application. The idea is to upload a text document on a window(main window) and to use it FROM other windows(childwindows), without having to upload the document again once the user is on one of the childwindows. To get the document on the main window, a button is required and a path to the file is shown. Having the document on the main window, any path shoul be required from the childwindows and also, any button for this purpose is not needed on the childwindows. That means, the document should be retrieved from the text widget of the main window.

    The bottle neck is at the line 29 of one of the childwindows. With this line, I want to get to the text document of the main window. Previously, I have created an instance of the main window. While compiling, I have the following error message: M_myText is private. In this code, I though I could use the friendship, but this would suppress the encapsulation, so that I do not thing it is the good idea. The heritage would also not be the good idea.

    My question is the following:

    How can I get to the (and the) document I've uploaded on the main window from one of my childwindows?

    Any help would be welcome.

    Many thanks in advance.

    Here is my code

    Main window:
    Qt Code:
    1. #include <QtGui>
    2.  
    3. class MyMainWindow: public QMainWindow
    4. {
    5. Q_OBJECT
    6.  
    7. public:
    8. explicit MyMainWindow();
    9. virtual ~MyMainWindow();
    10.  
    11.  
    12. public slots:
    13. void openFile();
    14.  
    15. private:
    16. QAction *m_open;
    17. QTextEdit *m_myText;
    18. QWidget *m_centralZone;
    19. QMenu *m_menuFile;
    20. QLineEdit *m_openAFile;
    21. };
    22.  
    23. MyMainWindow::MyMainWindow() : QMainWindow()
    24. {
    25. m_menuFile = menuBar()->addMenu("&File");
    26.  
    27. m_myText = new QTextEdit(m_centralZone);
    28.  
    29. m_open = m_menuFile->addAction("Open a file");
    30.  
    31. m_openAFile = new QLineEdit;
    32.  
    33. connect(m_open, SIGNAL(triggered()), this, SLOT(openFile()));
    34.  
    35. setCentralWidget(m_centralZone);
    36. }
    37.  
    38. void MyMainWindow::openFile()
    39. {
    40. QString file = QFileDialog::getOpenFileName(this, "Information", "Open", "*.text");
    41. m_openAFile->setText(file);
    42. }
    To copy to clipboard, switch view to plain text mode 

    One of the childwindows

    Qt Code:
    1. #include<QtGui>
    2. #include"myMainWindow"
    3.  
    4. class ChildWindow: public QWidget
    5. {
    6. Q_OBJECT
    7.  
    8. public:
    9. explicit ChildWindow(QWidget *parent = 0);
    10. virtual ~ChildWindow();
    11.  
    12. public slots:
    13. void operation2(myMainWindow &aFile, QString &doc);
    14.  
    15. private:
    16.  
    17. };
    18.  
    19.  
    20. #include "ChildWindow.h"
    21. #include "myMainWindow.h"
    22.  
    23. ChildWindow::ChildWindow(QWidget *parent)
    24. : QWidget(parent)
    25. {
    26. }
    27. void ChildWindow::operation2(myMainWindow &aFile, QString &doc)
    28. {
    29. doc = aFile.m_myText.text();
    30. }
    31.  
    32.  
    33. ChildWindow::~ChildWindow()
    34. {
    35. ;
    36. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,499
    Thanked 277 Times in 272 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Using text document ALREADY uploaded on another window

    Defining public method in MyMainWindow or signals and slots.

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

    Default Re: Using text document ALREADY uploaded on another window

    Hi Lesiok!
    By calling a public method of myMainWindow from a child window, the process of uploading the text will be repeated, what I do not want. I want to use the data I have already uploaded on my main window.
    Last edited by Stanfillirenfro; 5th February 2013 at 09:30.

  4. #4
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,499
    Thanked 277 Times in 272 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Using text document ALREADY uploaded on another window

    Why do you think that calling this method will give the file read
    Qt Code:
    1. void ChildWindow::putText( QString const &text );
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: Using text document ALREADY uploaded on another window

    This could be a method. The idea is to be able to use the text already uploaded on the main window. I do not want to display the text on the child window.
    my main problem is to have the path the text on the main window.
    Any help would be welcome

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

    Default Re: Using text document ALREADY uploaded on another window

    Expose your document as QTextDocument and not as a string.
    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.


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

    Default Re: Using text document ALREADY uploaded on another window

    Hi Wysota!
    Could you please help with a code?

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

    Default Re: Using text document ALREADY uploaded on another window

    Qt Code:
    1. QTextDocument* MyMainWindow::document() const { return m_textEdit->document(); }
    To copy to clipboard, switch view to plain text mode 
    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: Using text document ALREADY uploaded on another window

    Hi Wysota and thanks for the code.
    It fails to compile. The error message is that "QTextDocument* MyMainWindow::document() const" is private in this context, when I am calling from the childwindow.

  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: Using text document ALREADY uploaded on another window

    Hi here is a similar example, see if you can get help from this code.

    This code has MyMainWindow class which can open and view file, or just view the file opened by the another instance of the MyMainWindow.
    So I create two instance of MyMainWindow, one will act as opener and viewer, and other will be just viewer (the actual file is opened by the opener itself, and send using singal/slot).. Have Fun

    Qt Code:
    1. #include <QtGui>
    2.  
    3. class MyMainWindow: public QMainWindow
    4. {
    5. Q_OBJECT
    6.  
    7. public:
    8. explicit MyMainWindow(QWidget * parent = 0);
    9. virtual ~MyMainWindow() {}
    10.  
    11. void setOperner(bool opener) { m_opener = opener; }
    12.  
    13. signals:
    14. void sendDocument(const QString &doc);
    15. void hereIsData(const QString &data);
    16.  
    17. public slots:
    18. void takeDocumentText(const QString &docText);
    19. void openDocument(const QString &filename);
    20. void openFile();
    21.  
    22. private:
    23. QWidget *m_centralZone;
    24. QLineEdit *m_openAFile;
    25. QTextEdit *m_myText;
    26. QGridLayout * m_layout;
    27. QMenu *m_menuFile;
    28. QAction *m_open;
    29. bool m_opener;
    30. };
    31.  
    32. MyMainWindow::MyMainWindow(QWidget *parent)
    33. : QMainWindow(parent)
    34. , m_centralZone(new QWidget)
    35. , m_openAFile(new QLineEdit)
    36. , m_myText(new QTextEdit)
    37. , m_layout(new QGridLayout)
    38. , m_menuFile(menuBar()->addMenu("&File"))
    39. , m_open(m_menuFile->addAction("Open a file"))
    40. , m_opener(true)
    41. {
    42. m_layout->addWidget(m_openAFile, 0, 0, 1, 1);
    43. m_layout->addWidget(m_myText, 1, 0, 1, 1);
    44. m_centralZone->setLayout(m_layout);
    45.  
    46. connect(m_open, SIGNAL(triggered()), this, SLOT(openFile()));
    47.  
    48. setCentralWidget(m_centralZone);
    49. }
    50.  
    51. void MyMainWindow::openDocument(const QString &filename)
    52. {
    53. QFile File(filename);
    54. if(File.open(QIODevice::ReadOnly | QIODevice::Text))
    55. {
    56. const QString data = File.readAll();
    57. File.close();
    58.  
    59. m_openAFile->setText(filename);
    60. takeDocumentText(data);
    61.  
    62. emit hereIsData(data);
    63. }
    64. }
    65.  
    66. void MyMainWindow::takeDocumentText(const QString &docText)
    67. {
    68. m_myText->setText(docText);
    69. }
    70.  
    71. void MyMainWindow::openFile()
    72. {
    73. const QString file = QFileDialog::getOpenFileName(this, "Information", "Open", "*.text");
    74.  
    75. if(m_opener)
    76. openDocument(file);
    77. else
    78. emit sendDocument(file);
    79. }
    80.  
    81. int main(int argc, char** argv)
    82. {
    83. QApplication app(argc, argv);
    84.  
    85. MyMainWindow window1;
    86. MyMainWindow window2;
    87.  
    88. window1.setWindowTitle("Document Opener & Viewer");
    89. window2.setWindowTitle("Document Viewer");
    90.  
    91. window1.setOperner(true);
    92. window2.setOperner(false);
    93.  
    94. window1.show();
    95. window2.show();
    96.  
    97. app.connect(&window2, SIGNAL(sendDocument(QString)), &window1, SLOT(openDocument(QString)));
    98. app.connect(&window1, SIGNAL(hereIsData(QString)), &window2, SLOT(takeDocumentText(QString)));
    99.  
    100. return app.exec();
    101. }
    102.  
    103. #include "main.moc"
    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.

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

    Default Re: Using text document ALREADY uploaded on another window

    Quote Originally Posted by Stanfillirenfro View Post
    Hi Wysota and thanks for the code.
    It fails to compile. The error message is that "QTextDocument* MyMainWindow::document() const" is private in this context, when I am calling from the childwindow.
    So declare it as public and not private.
    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.


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

    Default Re: Using text document ALREADY uploaded on another window

    Many thanks Reddy for your code.
    Ii is very helpfull, although the problem is not overcome. The code you sent me deals only with the main window. In my case, and due to your help of yesterday, the problem with the main window was solved. My main problem now it to use the document on the TextEdit of the main window from childwindow withough having to upload it again.
    If I have a fucntion such as "functionxxx" for example of the childwindow, how can I get the path to the text on the main window and its text? When I am compiling my initial code, the error message is that "No file name specified" and that MyMainWindow ist private in this context.

    Any help? Please!

  13. #13
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Using text document ALREADY uploaded on another window

    Quote Originally Posted by Stanfillirenfro View Post
    My main problem now it to use the document on the TextEdit of the main window from childwindow withough having to upload it again.
    No, your main problem is that you are ignoring any information about the actual solution, i.e. what wysota is writing.

    Obviously other people not reading what you are trying to achieve and posting solutions to problems you are not having doesn't help either.

    So either share the QTextDocument created by your main text edit with the secondary one, or create a stand alone QTextDocument and use it on both text edits.

    Cheers,
    _

  14. #14
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,491
    Thanks
    252
    Thanked 717 Times in 707 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Using text document ALREADY uploaded on another window

    I think the real problem is that the OP has only a minimal understanding of C++ programming in general and has no idea how to implement the suggestions being posted. He is way over his head with this project and needs to stop asking us basic C++ questions and start by reading a good C++ textbook until he understands some basic concepts.

    There is no magic to using the Qt class library; it is just C++ classes in the end, and if you don't understand the basics of passing C++ instances around a program either by pointer or by reference, you won't be able to write any C++ program, much less a Qt program.

Similar Threads

  1. Open and display a document on a window
    By Stanfillirenfro in forum Qt Programming
    Replies: 2
    Last Post: 4th February 2013, 09:53
  2. Replies: 0
    Last Post: 2nd August 2010, 09:37
  3. Multi-window document structure
    By JariV in forum Newbie
    Replies: 2
    Last Post: 16th February 2009, 20:42
  4. listing the files that has been uploaded to postgres
    By aekilic in forum Qt Programming
    Replies: 2
    Last Post: 10th September 2007, 10:43

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.