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>
{
Q_OBJECT
public:
explicit MyWindow();
virtual ~MyWindow();
public slots:
void openFile();
private:
};
#endif // MYWINDOW_H
#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
To copy to clipboard, switch view to plain text mode
.cpp file
#include "myWindow.h"
myWindow::myWindow()
{
m_menuFile = menuBar()->addMenu("&File");
m_myText->setVisible(false);
m_open = m_menuFile->addAction("Open a file");
connect(m_open, SIGNAL(triggered()), this, SLOT(openFile()));
setCentralWidget(centralZone);
}
void myWindow::openFile()
{
m_openAFile->setPlainText(file);
myText->setVisible(true);
myText->setText(text);
}
#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);
}
To copy to clipboard, switch view to plain text mode
Bookmarks