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




Reply With Quote
Bookmarks