PDA

View Full Version : button to save line edit text to file



davidrhcp
6th May 2014, 14:06
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::on_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


#include "mainwindow.h"
#include <QApplication>
#include <cstdlib>
#include <QNetworkConfiguration>
#include <QDebug>
#include <QDir>
#include <QFileInfo>
#include <QNetworkAccessManager>
#include <QNetworkConfiguration>
#include <mainwindow.h>
#include <QFile>
#include <QString>

void Write(QString Filename)
{
QFile mFile(Filename);
}

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

QApplication a(argc, argv);
MainWindow w;
w.show();

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

QString mFile = ("C:/Monice/BW/MBW.txt");


return a.exec();
}

my mainwindow.cpp looks like this


#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "QObject"
#include "QtCore"
#include "QNetworkConfigurationManager"
#include "QNetworkInterface"
#include "QHostAddress"
#include "QString"
#include "QFile"
#include "QLineEdit"
#include "QDir"


MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)

{
ui->setupUi(this);

//display network interfaces
QList<QNetworkInterface> list = QNetworkInterface::allInterfaces();

// each interface is allocated one after the other in a list
foreach (QNetworkInterface mon, list)

//adding the list to be shown in the interface combobox
{
ui->comboBox_Interface->addItem(mon.name());
}

// a for loop that counts from 50-90 in multiples of 5
for (int i = 50; i <= 90; i += 5)
//selecting the combox alert to diplay the figures 50-90 with a % symbol
{
ui->comboBox_Alert->addItem(QString::number(i) + "%");
}


}

MainWindow::~MainWindow()
{
delete ui;
}


void MainWindow::on_save_clicked()
{
// read LineEdit, write to file in dirv and clear LineEdit
QFile mFile(Filename);
QTextStream out(&mFile);
out << lineEdit::text;

}

anda_skoa
6th May 2014, 14:35
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.



QFile mFile(Filename); // Assuming Filename is some member of MainWindow or a global variable

if (mFile.open(QIODevice::WriteOnly) {
QTextStream out(&mFile);
out << lineEdit->text();
}


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,
_

davidrhcp
6th May 2014, 15:04
that makes sense ive implemented it as so


void Write(QString Filename)
{
QFile mFile(Filename); // Assuming Filename is some member of MainWindow or a global variable

if (mFile.open(QIODevice::WriteOnly))

{
QTextStream out(&mFile);
out << lineEdit->text();
}
}
int main(int argc, char *argv[])
{

QApplication a(argc, argv);
MainWindow w;
w.show();

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


QString mFile = ("C:/Monice/BW/MBW.txt");

return a.exec();
}

however it still sais that lineEdit is not declared in the scope

cszawisza
6th May 2014, 15:46
out << lineEdit->text();

You gat all your widgets in ui object so you need to add ui-> before your widget name


out << ui->lineEdit->text();

davidrhcp
6th May 2014, 15:57
haha i did that and now the ui is not within the scope?!

I have meddled with the code this is it atm


#include "mainwindow.h"
#include <QApplication>
#include <cstdlib>
#include <QNetworkConfiguration>
#include <QDebug>
#include <QDir>
#include <QFileInfo>
#include <QNetworkAccessManager>
#include <QNetworkConfiguration>
#include <mainwindow.h>
#include <QFile>
#include <QString>
#include <QIODevice>


void Write(QString Filename){


QFile mFile(filename); // Assuming Filename is some member of MainWindow or a global variable

if (mFile.open(QIODevice::WriteOnly))

{
QTextStream out(&mFile);
out << ui->lineEdit->text();
}
}
int main(int argc, char *argv[])
{

QApplication a(argc, argv);
MainWindow w;
w.show();

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


QString mFile = ("C:/Monice/BW/MBW.txt");

return a.exec();
}

the filename is not declared in scope and neither is the ui

cszawisza
6th May 2014, 21:52
Put your definition of Write in MainWidnow class... and read about some basic's of C++ programming :)

anda_skoa
7th May 2014, 10:41
haha i did that and now the ui is not within the scope?!

Why do you expect it to be in scope?



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,
_