Results 1 to 7 of 7

Thread: Open File Dialog Help

  1. #1
    Join Date
    Jan 2011
    Posts
    128
    Thanks
    2

    Default Open File Dialog Help

    Hey people,
    I started a small application and this is what I have so far:
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5. #include <QPushButton>
    6. #include <QFileDialog>
    7. #include <QLineEdit>
    8.  
    9.  
    10. namespace Ui {
    11. class MainWindow;
    12. }
    13.  
    14. class MainWindow : public QMainWindow
    15. {
    16. Q_OBJECT
    17.  
    18. public:
    19. explicit MainWindow(QWidget *parent = 0);
    20. ~MainWindow();
    21. QPushButton * btn;
    22. QLineEdit * line;
    23.  
    24.  
    25. private slots:
    26. void open();
    27.  
    28. private:
    29. Ui::MainWindow *ui;
    30. };
    31.  
    32. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include <QFileDialog>
    4.  
    5.  
    6. MainWindow::MainWindow(QWidget *parent) :
    7. QMainWindow(parent),
    8. ui(new Ui::MainWindow)
    9. {
    10. ui->setupUi(this);
    11.  
    12. btn = new QPushButton("Open!");
    13. btn->show();
    14. line = new QLineEdit;
    15. line->show();
    16. QObject::connect(btn, SIGNAL(clicked()), this, SLOT(open()));
    17. }
    18.  
    19. MainWindow::~MainWindow()
    20. {
    21. delete ui;
    22.  
    23. }
    24.  
    25. void MainWindow::open()
    26. {
    27. fd = new QFileDialog;
    28. fd->show();
    29.  
    30. }
    To copy to clipboard, switch view to plain text mode 
    And main.cpp
    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include "mainwindow.h"
    3. #include <QPushButton>
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. QApplication a(argc, argv);
    8. MainWindow w;
    9. w.show();
    10. return a.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 

    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.

  2. #2
    Join Date
    Oct 2009
    Location
    Germany
    Posts
    120
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default 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()).

  3. #3
    Join Date
    Jan 2011
    Posts
    128
    Thanks
    2

    Default Re: Open File Dialog Help

    Thank you for the reply
    BTW:How to make the widgets have a parent?

  4. #4
    Join Date
    Oct 2009
    Location
    Germany
    Posts
    120
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Open File Dialog Help

    Pass a pointer to the parent in widget CTOR, e.g. in your main window:

    Qt Code:
    1. btn = new QPushButton("Open!",this);
    To copy to clipboard, switch view to plain text mode 

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

  5. #5
    Join Date
    Jan 2011
    Posts
    128
    Thanks
    2

    Default Re: Open File Dialog Help

    So,this is what I have now:
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5. #include <QDialog>
    6. #include <QPushButton>
    7. #include <QString>
    8. #include <QFileDialog>
    9. #include <QLineEdit>
    10. #include <QVBoxLayout>
    11. #include <QHBoxLayout>
    12. #include <QLabel>
    13. #include <QTextEdit>
    14.  
    15. namespace Ui {
    16. class MainWindow;
    17. }
    18.  
    19. class MainWindow : public QMainWindow
    20. {
    21. Q_OBJECT
    22.  
    23. public:
    24. explicit MainWindow(QWidget *parent = 0);
    25. ~MainWindow();
    26. QPushButton * btn;
    27. QLineEdit * line;
    28. QVBoxLayout * layout;
    29. QString * fname;
    30. QWidget * win;
    31. QLabel * label;
    32. QTextEdit * txt;
    33. private slots:
    34. void open();
    35.  
    36. private:
    37. Ui::MainWindow *ui;
    38. };
    39.  
    40. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include <QString>
    4. #include <QFileDialog>
    5. #include <QVBoxLayout>
    6.  
    7. MainWindow::MainWindow(QWidget *parent) :
    8. QMainWindow(parent),
    9. ui(new Ui::MainWindow)
    10. {
    11. ui->setupUi(this);
    12. win = new QWidget;
    13. btn = new QPushButton("Open!",this);
    14. fname = new QString;
    15. layout = new QVBoxLayout;
    16. cl = new QHBoxLayout;
    17. line = new QLineEdit(this);
    18. label = new QLabel("Directory",this);
    19. txt = new QTextEdit;
    20.  
    21. line->show();
    22. btn->show();
    23. txt->show();
    24.  
    25. layout->addWidget(btn);
    26. layout->addLayout(cl);
    27. cl->addWidget(label);
    28. cl->addWidget(line);
    29. layout->addWidget(txt);
    30. win->setLayout(layout);
    31. MainWindow::setCentralWidget(win);
    32.  
    33. setFixedHeight(sizeHint().height());
    34. MainWindow::setWindowTitle("Hello Qt!");
    35.  
    36. QObject::connect(btn, SIGNAL(clicked()), this, SLOT(open()));
    37. }
    38.  
    39. MainWindow::~MainWindow()
    40. {
    41. delete ui;
    42.  
    43. }
    44.  
    45. void MainWindow::open()
    46. {
    47. line->setText(fd->getOpenFileName(this));
    48. //QFile * file = new QFile; Here error starts...
    49. //txt->setPlainText(file->read(fd->FileName)); this doesn't work.
    50. }
    To copy to clipboard, switch view to plain text mode 

    All is ok except the last 2 lines...I don't know how to open and read content with QFile.

  6. #6
    Join Date
    Oct 2009
    Location
    Germany
    Posts
    120
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default 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:
    Qt Code:
    1. void MainWindow::open()
    2. {
    3. QString fname = QFileDialog::getOpenFileName(this, tr("Put your dialog caption here"));
    4. QFile openFile(fname); // you may check for fname.isEmpty before
    5. openFile.open(QIODevice::ReadOnly); // you should do some error checking here!
    6. QByteArray content = openFile.readAll();
    7. txt->setPlainText(QString(content));
    8. }
    To copy to clipboard, switch view to plain text mode 
    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.

  7. The following user says thank you to ars for this useful post:

    "BumbleBee" (20th February 2011)

  8. #7
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default 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.

Similar Threads

  1. Replies: 31
    Last Post: 11th January 2011, 10:36
  2. Replies: 1
    Last Post: 3rd August 2010, 03:41
  3. Open Dialog from MainWindow.
    By halvors in forum Qt Programming
    Replies: 8
    Last Post: 1st April 2010, 02:09
  4. File Open dialog with preview?
    By will49 in forum Qt Programming
    Replies: 2
    Last Post: 24th July 2007, 19:08
  5. Open/saveFile dialog bug?
    By macbeth in forum Qt Programming
    Replies: 1
    Last Post: 12th March 2007, 16:20

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.