PDA

View Full Version : Use Push Button to change a variable



Qem1st
16th January 2013, 04:07
I am trying to make a program that pulls text from a text file indicated by the user (typing the file path in a lineEdit) and displays it on a text edit widget. Here's what I have so far:

#include <QApplication>
#include <QtGui>
#include <QtCore>

int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QWidget *window = new QWidget;
window->setWindowTitle("Text Reader");

QGridLayout *layout = new QGridLayout;

QTextEdit *textbox = new QTextEdit();
QLineEdit *enterFile = new QLineEdit();
QPushButton *goButton = new QPushButton("Ok");
QPushButton *quitButton = new QPushButton("Quit");

layout->addWidget(textbox, 0,0,1,1);
layout->addWidget(enterFile, 1,0,1,1);
layout->addWidget(goButton, 2,0,1,1);
layout->addWidget(quitButton, 3,0,1,1);

window->setLayout(layout);

QObject::connect(quitButton, SIGNAL(clicked()), qApp, SLOT(quit()));

//
//STUFF TO BE ADDED
//

window->show();
return app.exec(); //Starts main event loop
}

This is just the shell of things, and I'm learning as I add things in. I'll add in all the stuff for opening the file and storing it's contents into a QString later, but how do I take the text from 'enterFile' (a QLineEdit), and store that into a QString? I'll end up using that string to open up a file and load its text onto the textEdit widget. I've tried using a SIGNAL/SLOT mechanism thing, but it doesn't seem to do anything.

Also, I'm totally new to qt and object oriented programming. The only experience I have is working with console applications in VC++ (though I know the basics there well enough)

Santosh Reddy
16th January 2013, 08:53
Have a look at this, this is one way to do it.



#include <QApplication>
#include <QtGui>
#include <QtCore>

class QTextEditLoader : public QObject // Added <<<<<<<<<<<<<<<<<<<<
{
Q_OBJECT

public:
explicit QTextEditLoader(QTextEdit *textEdit, QLineEdit * filename)
: QObject(textEdit)
, parent(textEdit)
, fileName(filename)
{
;
}

public slots:
void loadTextFromFile(void)
{
QString text("<Error, Unable to open file>");
QFile file(fileName->text());

if(file.open(file.ReadOnly | file.Text))
{
text = file.readAll();
if(text.isEmpty())
text = "<Error, Empty/Non-Text File>";
file.close();
}

parent->setText(text);
}

private:
QTextEdit *parent;
QLineEdit *fileName;
};

int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QWidget *window = new QWidget;
window->setWindowTitle("Text Reader");

QGridLayout *layout = new QGridLayout;

QTextEdit *textbox = new QTextEdit();
QLineEdit *enterFile = new QLineEdit();
QTextEditLoader *loader = new QTextEditLoader(textbox, enterFile); // Added <<<<<<<<<<<<<<<<<<<<
QPushButton *goButton = new QPushButton("Ok");
QPushButton *quitButton = new QPushButton("Quit");

layout->addWidget(textbox, 0,0,1,1);
layout->addWidget(enterFile, 1,0,1,1);
layout->addWidget(goButton, 2,0,1,1);
layout->addWidget(quitButton, 3,0,1,1);

window->setLayout(layout);

QObject::connect(quitButton, SIGNAL(clicked()), qApp, SLOT(quit()));
QObject::connect(goButton, SIGNAL(clicked()), loader, SLOT(loadTextFromFile())); // Added <<<<<<<<<<<<<<<<<<<<

//
//STUFF TO BE ADDED
//

window->show();
return app.exec(); //Starts main event loop
}

#include "Main.moc"

anda_skoa
16th January 2013, 13:26
but how do I take the text from 'enterFile' (a QLineEdit), and store that into a QString

Already very thorougly answered but just to emphasis that the most reduced answer to your question is


QString fileName = enterFile->text();


Cheers,
_

Qem1st
16th January 2013, 18:56
I've got everything fixed up, and have everything set up to get the file name and to open the file, but when I try to use this line of code,

QString line = Stream.readAll();

or this,

QString line = Stream.readLine(0);

'line' is empty. I've verified that it opened the file up without a problem, but somehow it thinks it's empty. The file is a text file with some random crap that I generated in notepad.

Why can't I get anything?

ChrisW67
17th January 2013, 22:03
Why can't I get anything?

How can we possibly know? How have you opened the file? Have you already read anything form it? Is it the file you think you have opened or another? What is the relationship between the file you have opened and "Stream"?