Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Save Input Text to File through Button Click

Save Input Text to File through Button Click

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 5.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    bill725
    wrote on last edited by
    #1

    This is the only file my program has called main.cpp

    When I build and run it, it displays my two text input fields. I want to type into them, click the button and have the text save to a file. How can I go about doing this?

    @//! [main program]
    #include <QtWidgets>
    #include <QPushButton>
    #include <QObject>

    void main::example(){

    }

    int main(int argc, char *argv[])
    {
    QApplication app(argc, argv);
    QWidget window;
    //! [create, lay out widgets and show]
    //This line created a label that says Longitude
    QLabel *pointOneLongLabel = new QLabel(QApplication::translate("windowlayout", "Longitude:"));
    //This line creates a text input where the user can type into. It will be used to type in the longitude.
    QLineEdit *pointOneLongInput = new QLineEdit();

    //The following pairs of lines are the same as above.  It is creating a label and textinput for
    //each of the points (longitude and latitude we will navigate to.
    
    QLabel *pointOneLatLabel = new QLabel(QApplication::translate("windowlayout", "Latitude:"));
    QLineEdit *pointOneLatInput = new QLineEdit();
    
    
    
    //This is going to create a new clickable button that will read "Apply"
    QPushButton *button = new QPushButton();
    button->setText("Apply");
    
    //This line will make the button do something.
    QObject::connect(button, SIGNAL(clicked()),window ,SLOT(fun()));
    
    
    //QString textinlineedit;
    //textinlineedit = lineEdit ->text();
    
    //This line creates a new layout in the window
    QHBoxLayout *layout = new QHBoxLayout();
    //These lines add our objects (labels, input boxes, buttons) to the layout.
    layout->addWidget(pointOneLongLabel);
    layout->addWidget(pointOneLongInput);
    layout->addWidget(pointOneLatLabel);
    layout->addWidget(pointOneLatInput);
    
    layout->addWidget(button);
    
    window.setLayout(layout);
    

    //! [create, lay out widgets and show]
    window.setWindowTitle(
    QApplication::translate("windowlayout", "Enter Waypoints"));
    window.show();
    return app.exec();
    }
    //! [main program]
    @

    1 Reply Last reply
    0
    • S Offline
      S Offline
      stevenceuppens
      wrote on last edited by
      #2

      Hi Bill,

      If you really want to keep everything in the main function, Lambda (Qt5 & C++11) can be used here...

      Add this to your .pro file to enable c++11:
      @
      CONFIG += c++11
      @

      Example code:
      @
      #include <QtWidgets>
      #include <QPushButton>
      #include <QObject>
      #include <QDebug>

      int main(int argc, char *argv[])
      {
      QApplication app(argc, argv);

      // setup QFile, argument is path to file &#40;relative from executable&#41;
      QFile data("output.txt");
      // open file
      if (!data.open(QFile::WriteOnly | QFile::Truncate)) {
      
          qDebug() << "Can't open file";
      
          return 1;
      }
      // attach file to QTextStream
      QTextStream out(&data);
      
      
      QWidget window;
      

      //! [create, lay out widgets and show]
      //This line created a label that says Longitude
      QLabel *pointOneLongLabel = new QLabel(QApplication::translate("windowlayout", "Longitude:"));
      //This line creates a text input where the user can type into. It will be used to type in the longitude.
      QLineEdit *pointOneLongInput = new QLineEdit();

      //The following pairs of lines are the same as above.  It is creating a label and textinput for
      //each of the points (longitude and latitude we will navigate to.
      
      QLabel *pointOneLatLabel = new QLabel(QApplication::translate("windowlayout", "Latitude:"));
      QLineEdit *pointOneLatInput = new QLineEdit();
      
      
      
      //This is going to create a new clickable button that will read "Apply"
      QPushButton *button = new QPushButton();
      button->setText("Apply");
      
      //This line will make the button do something.
      // using Lambda (Qt5 && c++11 only)
      QObject::connect(button, &QPushButton::clicked, [&out, pointOneLongInput, pointOneLatInput]() {
      
          // stream data to QTextStream
          out << "Result: " << pointOneLongInput->text() << "," << pointOneLatInput->text() << endl;
      });
      
      
      //QString textinlineedit;
      //textinlineedit = lineEdit ->text();
      
      //This line creates a new layout in the window
      QHBoxLayout *layout = new QHBoxLayout();
      //These lines add our objects (labels, input boxes, buttons) to the layout.
      layout->addWidget(pointOneLongLabel);
      layout->addWidget(pointOneLongInput);
      layout->addWidget(pointOneLatLabel);
      layout->addWidget(pointOneLatInput);
      
      layout->addWidget(button);
      
      window.setLayout(layout);
      //! [create, lay out widgets and show]
      window.setWindowTitle(QApplication::translate("windowlayout", "Enter Waypoints"));
      window.show();
      return app.exec();
      

      }
      @

      But in would advice to move QWidget window, to its own class / file where you easily can make signal - slot connections

      Steven CEUPPENS
      Developer &#x2F; Architect
      Mobile: +32 479 65 93 10

      1 Reply Last reply
      0
      • B Offline
        B Offline
        bill725
        wrote on last edited by
        #3

        Thank you so much Steven! That worked like a charm, I know that it is poor programming practice to have a file that does multiple things and that I should have a class for each feature I need to implement but I was using an example template that was almost completely functional and just needed to add that small piece. Thanks again!

        1 Reply Last reply
        0
        • S Offline
          S Offline
          stevenceuppens
          wrote on last edited by
          #4

          No problem!

          Keep on Qt ;)

          Steven CEUPPENS
          Developer &#x2F; Architect
          Mobile: +32 479 65 93 10

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved