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:
Code:
#include <QtGui>
{
Q_OBJECT
public:
explicit MyMainWindow();
virtual ~MyMainWindow();
public slots:
void openFile();
private:
};
{
m_menuFile = menuBar()->addMenu("&File");
m_open = m_menuFile->addAction("Open a file");
connect(m_open, SIGNAL(triggered()), this, SLOT(openFile()));
setCentralWidget(m_centralZone);
}
void MyMainWindow::openFile()
{
m_openAFile->setText(file);
}
One of the childwindows
Code:
#include<QtGui>
#include"myMainWindow"
{
Q_OBJECT
public:
explicit ChildWindow
(QWidget *parent
= 0);
virtual ~ChildWindow();
public slots:
void operation2
(myMainWindow
&aFile,
QString &doc
);
private:
};
#include "ChildWindow.h"
#include "myMainWindow.h"
ChildWindow
::ChildWindow(QWidget *parent
) {
}
void ChildWindow
::operation2(myMainWindow
&aFile,
QString &doc
) {
doc = aFile.m_myText.text();
}
ChildWindow::~ChildWindow()
{
;
}
Re: Using text document ALREADY uploaded on another window
Defining public method in MyMainWindow or signals and slots.
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.
Re: Using text document ALREADY uploaded on another window
Why do you think that calling this method will give the file read
Code:
void ChildWindow
::putText( QString const &text
);
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
Re: Using text document ALREADY uploaded on another window
Expose your document as QTextDocument and not as a string.
Re: Using text document ALREADY uploaded on another window
Hi Wysota!
Could you please help with a code?
Re: Using text document ALREADY uploaded on another window
Code:
QTextDocument* MyMainWindow
::document() const { return m_textEdit
->document
();
}
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.
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 :)
Code:
#include <QtGui>
{
Q_OBJECT
public:
explicit MyMainWindow
(QWidget * parent
= 0);
virtual ~MyMainWindow() {}
void setOperner(bool opener) { m_opener = opener; }
signals:
void sendDocument
(const QString &doc
);
void hereIsData
(const QString &data
);
public slots:
void takeDocumentText
(const QString &docText
);
void openDocument
(const QString &filename
);
void openFile();
private:
bool m_opener;
};
MyMainWindow
::MyMainWindow(QWidget *parent
) , m_menuFile(menuBar()->addMenu("&File"))
, m_open(m_menuFile->addAction("Open a file"))
, m_opener(true)
{
m_layout->addWidget(m_openAFile, 0, 0, 1, 1);
m_layout->addWidget(m_myText, 1, 0, 1, 1);
m_centralZone->setLayout(m_layout);
connect(m_open, SIGNAL(triggered()), this, SLOT(openFile()));
setCentralWidget(m_centralZone);
}
void MyMainWindow
::openDocument(const QString &filename
) {
{
const QString data
= File.
readAll();
File.close();
m_openAFile->setText(filename);
takeDocumentText(data);
emit hereIsData(data);
}
}
void MyMainWindow
::takeDocumentText(const QString &docText
) {
m_myText->setText(docText);
}
void MyMainWindow::openFile()
{
if(m_opener)
openDocument(file);
else
emit sendDocument(file);
}
int main(int argc, char** argv)
{
MyMainWindow window1;
MyMainWindow window2;
window1.setWindowTitle("Document Opener & Viewer");
window2.setWindowTitle("Document Viewer");
window1.setOperner(true);
window2.setOperner(false);
window1.show();
window2.show();
app.
connect(&window2,
SIGNAL(sendDocument
(QString)),
&window1,
SLOT(openDocument
(QString)));
app.
connect(&window1,
SIGNAL(hereIsData
(QString)),
&window2,
SLOT(takeDocumentText
(QString)));
return app.exec();
}
#include "main.moc"
Re: Using text document ALREADY uploaded on another window
Quote:
Originally Posted by
Stanfillirenfro
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.
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!
Re: Using text document ALREADY uploaded on another window
Quote:
Originally Posted by
Stanfillirenfro
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,
_
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.