Save Input Text to File through Button Click
-
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]
@ -
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 (relative from executable) 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
-
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!
-
No problem!
Keep on Qt ;)