PDA

View Full Version : Open File Dialog Help



"BumbleBee"
19th February 2011, 07:08
Hey people,
I started a small application and this is what I have so far:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QPushButton>
#include <QFileDialog>
#include <QLineEdit>


namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
QPushButton * btn;
QFileDialog * fd;
QLineEdit * line;


private slots:
void open();

private:
Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H


#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFileDialog>


MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);

btn = new QPushButton("Open!");
btn->show();
line = new QLineEdit;
line->show();
QObject::connect(btn, SIGNAL(clicked()), this, SLOT(open()));
}

MainWindow::~MainWindow()
{
delete ui;

}

void MainWindow::open()
{
fd = new QFileDialog;
fd->show();

}

And main.cpp

#include <QtGui/QApplication>
#include "mainwindow.h"
#include <QPushButton>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}


So,the idea was to press the button,open the file dialog,select a text file and:
1)Its directory should appear in the line edit(I don't know how to take the directory and put it in the lineedit)
2)Its content should appear in a text edit.

And one more,all the widgets(btn and line)appear in different boxes...how to make them be in the mainwindow?

Thank you.

ars
19th February 2011, 18:25
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()).

"BumbleBee"
19th February 2011, 19:42
Thank you for the reply
BTW:How to make the widgets have a parent?

ars
19th February 2011, 21:18
Pass a pointer to the parent in widget CTOR, e.g. in your main window:


btn = new QPushButton("Open!",this);

When placing the objects with designer, all of this stuff is implicitly done inside the ui object.

"BumbleBee"
20th February 2011, 08:34
So,this is what I have now:

#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;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
QPushButton * btn;
QLineEdit * line;
QVBoxLayout * layout;
QHBoxLayout * cl;
QString * fname;
QWidget * win;
QLabel * label;
QTextEdit * txt;
private slots:
void open();

private:
Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H


#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QString>
#include <QFileDialog>
#include <QVBoxLayout>

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
win = new QWidget;
btn = new QPushButton("Open!",this);
fname = new QString;
layout = new QVBoxLayout;
cl = new QHBoxLayout;
line = new QLineEdit(this);
label = new QLabel("Directory",this);
txt = new QTextEdit;

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()
{
QFileDialog * fd = new QFileDialog;
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.

ars
20th February 2011, 09:30
What about reading QFile documentation and your compiler error messages?

Here´s some more or less pseudo code for your open() slot:


void MainWindow::open()
{
QString fname = QFileDialog::getOpenFileName(this, tr("Put your dialog caption here"));
QFile openFile(fname); // you may check for fname.isEmpty before
openFile.open(QIODevice::ReadOnly); // you should do some error checking here!
QByteArray content = openFile.readAll();
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.

squidge
20th February 2011, 10:46
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.