Results 1 to 7 of 7

Thread: button to save line edit text to file

  1. #1
    Join Date
    May 2014
    Posts
    7
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: button to save line edit text to file

    hello

    I have set up QT widget application. at the moment im looking to get a push button to "once clicked" to read the data from a line edit box and save it to a file that i is created when the application is opened.

    This is the code on the main.cpp for the file creation.

    // creating directory upon run for user input
    QDir mDir;
    mDir.mkpath("C:/Monice/BW");

    The code i have so far for the button is where im struggling, i have been following Qt's documentation on help but haven't found anything suffcient that i can use to create what i need.
    his is the code in the mainwindow.cpp

    void MainWindow:n_save_clicked()
    {
    // read LineEdit, write to file in dirv and clear LineEdit
    lineEdit::copy("C:/Monice/BW/MBW");
    lineEdit::clear();
    }

    i know that its very wrong and if i try to run it the program sais lineedit is not delared.

    i dont need it all done for me im just looking for pointers in the right direction here




    Added after 49 minutes:


    I've made some changes, i think im getting close.

    My main.cpp looks like this
    Qt Code:
    1. #include "mainwindow.h"
    2. #include <QApplication>
    3. #include <cstdlib>
    4. #include <QNetworkConfiguration>
    5. #include <QDebug>
    6. #include <QDir>
    7. #include <QFileInfo>
    8. #include <QNetworkAccessManager>
    9. #include <QNetworkConfiguration>
    10. #include <mainwindow.h>
    11. #include <QFile>
    12. #include <QString>
    13.  
    14. void Write(QString Filename)
    15. {
    16. QFile mFile(Filename);
    17. }
    18.  
    19. int main(int argc, char *argv[])
    20. {
    21.  
    22. QApplication a(argc, argv);
    23. MainWindow w;
    24. w.show();
    25.  
    26. // creating directory upon run for user input
    27. QDir mDir;
    28. mDir.mkpath("C:/Monice/BW");
    29.  
    30. QString mFile = ("C:/Monice/BW/MBW.txt");
    31.  
    32.  
    33. return a.exec();
    34. }
    To copy to clipboard, switch view to plain text mode 
    my mainwindow.cpp looks like this
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include "QObject"
    4. #include "QtCore"
    5. #include "QNetworkConfigurationManager"
    6. #include "QNetworkInterface"
    7. #include "QHostAddress"
    8. #include "QString"
    9. #include "QFile"
    10. #include "QLineEdit"
    11. #include "QDir"
    12.  
    13.  
    14. MainWindow::MainWindow(QWidget *parent) :
    15. QMainWindow(parent),
    16. ui(new Ui::MainWindow)
    17.  
    18. {
    19. ui->setupUi(this);
    20.  
    21. //display network interfaces
    22. QList<QNetworkInterface> list = QNetworkInterface::allInterfaces();
    23.  
    24. // each interface is allocated one after the other in a list
    25. foreach (QNetworkInterface mon, list)
    26.  
    27. //adding the list to be shown in the interface combobox
    28. {
    29. ui->comboBox_Interface->addItem(mon.name());
    30. }
    31.  
    32. // a for loop that counts from 50-90 in multiples of 5
    33. for (int i = 50; i <= 90; i += 5)
    34. //selecting the combox alert to diplay the figures 50-90 with a % symbol
    35. {
    36. ui->comboBox_Alert->addItem(QString::number(i) + "%");
    37. }
    38.  
    39.  
    40. }
    41.  
    42. MainWindow::~MainWindow()
    43. {
    44. delete ui;
    45. }
    46.  
    47.  
    48. void MainWindow::on_save_clicked()
    49. {
    50. // read LineEdit, write to file in dirv and clear LineEdit
    51. QFile mFile(Filename);
    52. QTextStream out(&mFile);
    53. out << lineEdit::text;
    54.  
    55. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by anda_skoa; 6th May 2014 at 15:31. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: button to save line edit text to file

    You will need to open the file for writing or appending before streaming into it and you need to call the lineEdit's text() getter method.

    Qt Code:
    1. QFile mFile(Filename); // Assuming Filename is some member of MainWindow or a global variable
    2.  
    3. if (mFile.open(QIODevice::WriteOnly) {
    4. QTextStream out(&mFile);
    5. out << lineEdit->text();
    6. }
    To copy to clipboard, switch view to plain text mode 

    Seeing that you are using a designer created UI, your line edit is probably defined there as well, so access to it is similar to the combo boxes you access in the constructor.

    Cheers,
    _

  3. #3
    Join Date
    May 2014
    Posts
    7
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: button to save line edit text to file

    that makes sense ive implemented it as so
    Qt Code:
    1. void Write(QString Filename)
    2. {
    3. QFile mFile(Filename); // Assuming Filename is some member of MainWindow or a global variable
    4.  
    5. if (mFile.open(QIODevice::WriteOnly))
    6.  
    7. {
    8. QTextStream out(&mFile);
    9. out << lineEdit->text();
    10. }
    11. }
    12. int main(int argc, char *argv[])
    13. {
    14.  
    15. QApplication a(argc, argv);
    16. MainWindow w;
    17. w.show();
    18.  
    19. // creating directory upon run for user input
    20. QDir mDir;
    21. mDir.mkpath("C:/Monice/BW");
    22.  
    23.  
    24. QString mFile = ("C:/Monice/BW/MBW.txt");
    25.  
    26. return a.exec();
    27. }
    To copy to clipboard, switch view to plain text mode 
    however it still sais that lineEdit is not declared in the scope
    Last edited by anda_skoa; 7th May 2014 at 11:38. Reason: missing [code] tags

  4. #4
    Join Date
    Oct 2011
    Posts
    51
    Thanks
    5
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: button to save line edit text to file

    Qt Code:
    1. out << lineEdit->text();
    To copy to clipboard, switch view to plain text mode 
    You gat all your widgets in ui object so you need to add ui-> before your widget name
    Qt Code:
    1. out << ui->lineEdit->text();
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    May 2014
    Posts
    7
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: button to save line edit text to file

    haha i did that and now the ui is not within the scope?!

    I have meddled with the code this is it atm
    Qt Code:
    1. #include "mainwindow.h"
    2. #include <QApplication>
    3. #include <cstdlib>
    4. #include <QNetworkConfiguration>
    5. #include <QDebug>
    6. #include <QDir>
    7. #include <QFileInfo>
    8. #include <QNetworkAccessManager>
    9. #include <QNetworkConfiguration>
    10. #include <mainwindow.h>
    11. #include <QFile>
    12. #include <QString>
    13. #include <QIODevice>
    14.  
    15.  
    16. void Write(QString Filename){
    17.  
    18.  
    19. QFile mFile(filename); // Assuming Filename is some member of MainWindow or a global variable
    20.  
    21. if (mFile.open(QIODevice::WriteOnly))
    22.  
    23. {
    24. QTextStream out(&mFile);
    25. out << ui->lineEdit->text();
    26. }
    27. }
    28. int main(int argc, char *argv[])
    29. {
    30.  
    31. QApplication a(argc, argv);
    32. MainWindow w;
    33. w.show();
    34.  
    35. // creating directory upon run for user input
    36. QDir mDir;
    37. mDir.mkpath("C:/Monice/BW");
    38.  
    39.  
    40. QString mFile = ("C:/Monice/BW/MBW.txt");
    41.  
    42. return a.exec();
    43. }
    To copy to clipboard, switch view to plain text mode 
    the filename is not declared in scope and neither is the ui
    Last edited by anda_skoa; 7th May 2014 at 11:39. Reason: missing [code] tags

  6. #6
    Join Date
    Oct 2011
    Posts
    51
    Thanks
    5
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: button to save line edit text to file

    Put your definition of Write in MainWidnow class... and read about some basic's of C++ programming

  7. #7
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: button to save line edit text to file

    Quote Originally Posted by davidrhcp View Post
    haha i did that and now the ui is not within the scope?!
    Why do you expect it to be in scope?

    Quote Originally Posted by davidrhcp View Post
    the filename is not declared in scope
    C++ is case sensitive when it comes to identifiers. "Filename" and "filename" are two different identifiers.

    As cszawisza said, it would be benefitial to know at least the basics of C++ before attempting to write GUI programs with that language.

    Cheers,
    _

Similar Threads

  1. QTableView line edit clears the text on edit
    By PlasticJesus in forum Qt Programming
    Replies: 5
    Last Post: 14th March 2015, 20:06
  2. save line edit text to tabel widget row
    By akhilteja in forum Newbie
    Replies: 3
    Last Post: 24th August 2013, 10:06
  3. Clear button in line edit
    By MTK358 in forum Qt Programming
    Replies: 3
    Last Post: 1st August 2010, 14:27
  4. clear button for line edit
    By jayreddy in forum Qt Programming
    Replies: 6
    Last Post: 3rd December 2009, 05:36
  5. Line edit and push button
    By dela in forum Newbie
    Replies: 1
    Last Post: 10th December 2008, 17:10

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.