Results 1 to 3 of 3

Thread: how to associate a LineEdit with Save button?

  1. #1
    Join Date
    Oct 2015
    Posts
    35
    Thanks
    11
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11

    Default how to associate a LineEdit with Save button?

    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
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include <QApplication>
    4. #include <QFileDialog>
    5. #include <QDebug>
    6. #include <QFile>
    7. #include <QIODevice>
    8.  
    9.  
    10. MainWindow::MainWindow(QWidget *parent) :
    11. QMainWindow(parent),
    12. ui(new Ui::MainWindow)
    13. {
    14. ui->setupUi(this);
    15. }
    16.  
    17. MainWindow::~MainWindow()
    18. {
    19. delete ui;
    20. }
    21.  
    22. void MainWindow::on_pushButton_clicked()
    23. {
    24. QString fileName = QFileDialog::getSaveFileName(this, tr("Save Logs"),"/Tmp/",tr("(*.csv)"));
    25. if(!fileName.isEmpty())
    26. {
    27. QFile f(fileName);
    28. f.open(QIODevice::WriteOnly);
    29. f.close();
    30. }
    31. }
    32.  
    33. void MainWindow::on_lineEdit_editingFinished()
    34. {
    35.  
    36. }
    To copy to clipboard, switch view to plain text mode 

    mainwindow.h
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5. #include"ui_mainwindow.h"
    6.  
    7. namespace Ui {
    8. class MainWindow;
    9. }
    10.  
    11. class MainWindow : public QMainWindow
    12. {
    13. Q_OBJECT
    14.  
    15. public:
    16. explicit MainWindow(QWidget *parent = 0);
    17. ~MainWindow();
    18.  
    19. private slots:
    20. void on_pushButton_clicked();
    21.  
    22. void on_lineEdit_editingFinished();
    23.  
    24. private:
    25. Ui::MainWindow *ui;
    26. };
    27.  
    28. #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.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,316
    Thanks
    315
    Thanked 870 Times in 857 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: how to associate a LineEdit with Save button?

    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.

  3. #3
    Join Date
    Oct 2015
    Posts
    35
    Thanks
    11
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: how to associate a LineEdit with Save button?

    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.
    Last edited by rookee; 9th November 2015 at 17:41.

Similar Threads

  1. Replies: 6
    Last Post: 7th May 2014, 11:09
  2. Replies: 3
    Last Post: 23rd June 2013, 16:01
  3. Replies: 2
    Last Post: 6th June 2011, 09:40
  4. QFileDialog::getSaveFileName Save Button
    By penny in forum Qt Programming
    Replies: 1
    Last Post: 8th April 2011, 08:55
  5. How to enabled button, when i changed lineedit`s text ?
    By newermind in forum Qt Programming
    Replies: 8
    Last Post: 13th August 2009, 08:57

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
  •  
Qt is a trademark of The Qt Company.