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.
mainwindow.cpp
#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)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
QString fileName
= QFileDialog::getSaveFileName(this, tr
("Save Logs"),
"/Tmp/",tr
("(*.csv)"));
if(!fileName.isEmpty())
{
f.close();
}
}
void MainWindow::on_lineEdit_editingFinished()
{
}
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QApplication>
#include <QFileDialog>
#include <QDebug>
#include <QFile>
#include <QIODevice>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
QString fileName = QFileDialog::getSaveFileName(this, tr("Save Logs"),"/Tmp/",tr("(*.csv)"));
if(!fileName.isEmpty())
{
QFile f(fileName);
f.open(QIODevice::WriteOnly);
f.close();
}
}
void MainWindow::on_lineEdit_editingFinished()
{
}
To copy to clipboard, switch view to plain text mode
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include"ui_mainwindow.h"
namespace Ui {
class MainWindow;
}
{
Q_OBJECT
public:
explicit MainWindow
(QWidget *parent
= 0);
~MainWindow();
private slots:
void on_pushButton_clicked();
void on_lineEdit_editingFinished();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include"ui_mainwindow.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void on_pushButton_clicked();
void on_lineEdit_editingFinished();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
To copy to clipboard, switch view to plain text mode
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.
Bookmarks