PDA

View Full Version : Open and display a document on a window



Stanfillirenfro
4th February 2013, 10:22
Hello!
I want to open a text document and to display it on my window. The process has two actions: Opening the file and displaying it on the window. Theoritically, two slots are expected, and two bottons too to be clicked on. But practically, it seems not feasible, since a click on the open button should display the data on the window.
If I click on m_open (see below), a dialog window for the selection of the file will open. I have to selcet this file, click on "open" button of the Dialog window and then the window dissapears.

Line 8 of .cpp: The text erea shoul dremain hide until the data a ready to be display, and will appear with the line 25 of .cpp.
On the line 27 of .cpp file, I display the data on the window. If is not here, I must create another slot, that requires another button, because when I am connecting the button "m_open" to this slot (to open the file) and to another(to display the data), the compilation fails.

My questions:
1- Is there a way to connect the button open of the dialog window (when opening a file) to a slot in order to display my data on my window? If not, how to proceed?

Many thanks in advance.

Here is my code:

.h flie


#ifndef DEF_MYWINDOW
#define DEF_MYWINDOW

#include <QtGui>


class MyWindow: public QMainWindow
{
Q_OBJECT

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

public slots:
void openFile();

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

};

#endif // MYWINDOW_H

.cpp file


#include "myWindow.h"

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

m_myText = new QTextEdit(m_centralZone);
m_myText->setVisible(false);

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

m_openAFile = new QLineEdit;

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


setCentralWidget(centralZone);
}

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

myText->setVisible(true);

myText->setText(text);

}

Santosh Reddy
4th February 2013, 10:36
I guess this is what you wanted


#include <QtGui>

class MyWindow: public QMainWindow
{
Q_OBJECT

public:
explicit MyWindow();
virtual ~MyWindow() {};

signals:
void fileSelected(const QString & filename);

public slots:
void openFile();
void loadFile(const QString & filename);

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

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

m_myText = new QTextEdit(m_centralZone);
m_myText->setVisible(false);

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

m_openAFile = new QLineEdit;

connect(m_open, SIGNAL(triggered()), this, SLOT(openFile()));
connect(this, SIGNAL(fileSelected(QString)), this, SLOT(loadFile(QString)));

setCentralWidget(m_centralZone);
}

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

void MyWindow::loadFile(const QString & filename)
{
QFile file(filename);
if(file.open(file.Text | file.ReadOnly))
{
QString text = file.readAll();
m_myText->setText(text);
}
else
m_myText->setText("Error Opening File");

m_myText->setVisible(true);
}

Stanfillirenfro
4th February 2013, 10:53
Incredible!!!!!!
Reddy, you a really an engel!!!

Many thanks!! It works PERFECTLY!!!

Many thanks one more time Reddy!