Re: Open File Dialog Help
Your button and line edit object have no parent. Make your main window the parent of the objects. Moreover, you should add a layout to your main window and add button and line edit to this layout. Look through some of the Qt examples to see how they do it.
Alternatively (maybe simpler for starting with Qt), you may use the designer to place your button, line edit and text edit into the main window. In this case, you can access the objects through the ui object.
For your application, the static method QFileDialog::getOpenFileName() could be a better choice than using a local QFileDialog object. Even if you want to use a local QFileDialog object, there is no need to make it a member of your class. Simply create it as a local object inside your open slot. And then you do not need to create it using new() operator. Anyway, I recommend using QFileDialog::getOpenFileName(). This method returns the name of a file to be opened as QString (see the Qt documentation). Using this string, you can use class QFileInfo to obtain the name of the containing directory (see QFileInfo documentation for selecting a method according to your needs). Set the directory name using QLineEdit::setText() into the line edit.
For displaying the file content, use QFile for opening the file and reading the content. See QTextEdit documentation for setting the text. If you just want to display ascii text, use QTextEdit::setPlainText() for displaying the text.
Note: You find QLineEdit::setText() when looking for property QLineEdit::text() (similar for QTextEdit::setPlainText()).
Re: Open File Dialog Help
Thank you for the reply
BTW:How to make the widgets have a parent?
Re: Open File Dialog Help
Pass a pointer to the parent in widget CTOR, e.g. in your main window:
When placing the objects with designer, all of this stuff is implicitly done inside the ui object.
Re: Open File Dialog Help
So,this is what I have now:
Code:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QDialog>
#include <QPushButton>
#include <QString>
#include <QFileDialog>
#include <QLineEdit>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QLabel>
#include <QTextEdit>
namespace Ui {
class MainWindow;
}
{
Q_OBJECT
public:
explicit MainWindow
(QWidget *parent
= 0);
~MainWindow();
private slots:
void open();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
Code:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QString>
#include <QFileDialog>
#include <QVBoxLayout>
MainWindow
::MainWindow(QWidget *parent
) : ui(new Ui::MainWindow)
{
ui->setupUi(this);
label
= new QLabel("Directory",
this);
line->show();
btn->show();
txt->show();
layout->addWidget(btn);
layout->addLayout(cl);
cl->addWidget(label);
cl->addWidget(line);
layout->addWidget(txt);
win->setLayout(layout);
MainWindow::setCentralWidget(win);
setFixedHeight(sizeHint().height());
MainWindow::setWindowTitle("Hello Qt!");
QObject::connect(btn,
SIGNAL(clicked
()),
this,
SLOT(open
()));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::open()
{
line->setText(fd->getOpenFileName(this));
//QFile * file = new QFile; Here error starts...
//txt->setPlainText(file->read(fd->FileName)); this doesn't work.
}
All is ok except the last 2 lines...I don't know how to open and read content with QFile.
Re: Open File Dialog Help
What about reading QFile documentation and your compiler error messages?
Here´s some more or less pseudo code for your open() slot:
Code:
void MainWindow::open()
{
QFile openFile
(fname
);
// you may check for fname.isEmpty before openFile.
open(QIODevice::ReadOnly);
// you should do some error checking here! txt
->setPlainText
(QString(content
));
}
If you don't find the methods you're looking for in the documentation of a class, you may also look into the documentation of its parent class. In the code snippet above, readAll() e.g. is documented in class QIODevice, the parent class of QFile.
Re: Open File Dialog Help
Is there a reason you are both initialising the UI (ui.setupUI) and creating your own widgets? As stated previously, you would normally let the setupUI create all your widgets for you, and if you don't want that, you would remove the references to the UI namespace.