View Full Version : how to associate a LineEdit with Save button?

4th November 2015, 20:19
Hello All,

I'm trying to build a window to provide a space to the user to enter a file name to save/create file with the entered name. I want to associate the LineEdit(entered filename) with the PushButton(Save Button) so that when the user types in a name (say log) and hits the save button a file needs to be created along with the extension (like this: log.csv). This is where I've got so far. Please help.


#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QApplication>
#include <QFileDialog>
#include <QDebug>
#include <QFile>
#include <QIODevice>

MainWindow::MainWindow(QWidget *parent) :
ui(new Ui::MainWindow)

delete ui;

void MainWindow::on_pushButton_clicked()
QString fileName = QFileDialog::getSaveFileName(this, tr("Save Logs"),"/Tmp/",tr("(*.csv)"));
QFile f(fileName);

void MainWindow::on_lineEdit_editingFinished()




#include <QMainWindow>

namespace Ui {
class MainWindow;

class MainWindow : public QMainWindow

explicit MainWindow(QWidget *parent = 0);

private slots:
void on_pushButton_clicked();

void on_lineEdit_editingFinished();

Ui::MainWindow *ui;

#endif // MAINWINDOW_H

Also, if someone could explain tr("Save Logs") part in the syntax of QString fileName = QFileDialog::getSaveFileName(this, tr("Save Logs"),"/Tmp/",tr("(*.csv)")); . That would be really helpful. Thanks in advance.

4th November 2015, 22:00
It isn't at all clear what you're trying to do with this code. Your push button click slot already invokes a QFileDialog to get the log file name, so what's the purpose of the line edit? In addition, what you're doing in the push button click slot once you get a file name doesn't make much sense either. You get the file name, open the file, close the file, so nothing really happens except the creation of an empty file.

tr("Save Logs") is the translated (the tr() bit) title for the QFileDialog that will be displayed. It's a title, that's all.

9th November 2015, 16:02
Hello d_stranz, I realized that what you said is right regarding LineEdit, I don't need it again. Instead of lineedit and the push button, can i choose to have a check box in my form which can prompt to enter a filename to log the data to the file and save it with .csv extension when the check box is checked. If that is possible, could you please guide me on how to do it. I'm still working on what needs to go within the push button, as of now its just opening and closing the file.