PDA

View Full Version : Using text document ALREADY uploaded on another window



Stanfillirenfro
5th February 2013, 09:29
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:

#include <QtGui>

class MyMainWindow: public QMainWindow
{
Q_OBJECT

public:
explicit MyMainWindow();
virtual ~MyMainWindow();


public slots:
void openFile();

private:
QAction *m_open;
QTextEdit *m_myText;
QWidget *m_centralZone;
QMenu *m_menuFile;
QLineEdit *m_openAFile;
};

MyMainWindow::MyMainWindow() : QMainWindow()
{
m_menuFile = menuBar()->addMenu("&File");

m_myText = new QTextEdit(m_centralZone);

m_open = m_menuFile->addAction("Open a file");

m_openAFile = new QLineEdit;

connect(m_open, SIGNAL(triggered()), this, SLOT(openFile()));

setCentralWidget(m_centralZone);
}

void MyMainWindow::openFile()
{
QString file = QFileDialog::getOpenFileName(this, "Information", "Open", "*.text");
m_openAFile->setText(file);
}


One of the childwindows


#include<QtGui>
#include"myMainWindow"

class ChildWindow: public QWidget
{
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)
: QWidget(parent)
{
}
void ChildWindow::operation2(myMainWindow &aFile, QString &doc)
{
doc = aFile.m_myText.text();
}


ChildWindow::~ChildWindow()
{
;
}

Lesiok
5th February 2013, 09:53
Defining public method in MyMainWindow or signals and slots.

Stanfillirenfro
5th February 2013, 10:10
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.

Lesiok
5th February 2013, 11:03
Why do you think that calling this method will give the file read
void ChildWindow::putText( QString const &text );

Stanfillirenfro
5th February 2013, 11:19
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

wysota
5th February 2013, 12:12
Expose your document as QTextDocument and not as a string.

Stanfillirenfro
5th February 2013, 12:35
Hi Wysota!
Could you please help with a code?

wysota
5th February 2013, 13:20
QTextDocument* MyMainWindow::document() const { return m_textEdit->document(); }

Stanfillirenfro
5th February 2013, 13:44
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.

Santosh Reddy
5th February 2013, 13:46
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 :)



#include <QtGui>

class MyMainWindow: public QMainWindow
{
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:
QWidget *m_centralZone;
QLineEdit *m_openAFile;
QTextEdit *m_myText;
QGridLayout * m_layout;
QMenu *m_menuFile;
QAction *m_open;
bool m_opener;
};

MyMainWindow::MyMainWindow(QWidget *parent)
: QMainWindow(parent)
, m_centralZone(new QWidget)
, m_openAFile(new QLineEdit)
, m_myText(new QTextEdit)
, m_layout(new QGridLayout)
, 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)
{
QFile File(filename);
if(File.open(QIODevice::ReadOnly | QIODevice::Text))
{
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()
{
const QString file = QFileDialog::getOpenFileName(this, "Information", "Open", "*.text");

if(m_opener)
openDocument(file);
else
emit sendDocument(file);
}

int main(int argc, char** argv)
{
QApplication app(argc, 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"

wysota
5th February 2013, 13:47
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.

Stanfillirenfro
5th February 2013, 14:41
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!

anda_skoa
6th February 2013, 11:30
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,
_

d_stranz
6th February 2013, 17:57
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.