Thunfische
Thunfische

Reputation: 1157

Accesing the stored text file in the "Text Edit", Qt C++

I create a Qt Gui application with a "pushbutton" and a "text edit". I wanna assign the text in "text edit" to a QString variable after click on the "pushbutton". How can i store this text in a QString variable ?

Upvotes: 1

Views: 3374

Answers (2)

Mohamed A M-Hassan
Mohamed A M-Hassan

Reputation: 443

if you have a file and want to store it in a QString when cklicking a button, you can do that using QFileDialog for example:

//includes
#include <QFileDialog>
#include <QFile>
#include <QTextStream>
#include <QMessageBox>

// then in the Button,you can use this
 QString fileName = QFileDialog::getOpenFileName(this,
    tr("Open File"), "/home", tr("code file (*.txt)"));// string has the file link
// if you using Windows OS replace "/home" with "c://"
QFile file(fileName);
if (!file.open(QIODevice::ReadOnly))
    QMessageBox::information(0,"info",file.errorString());
QTextStream in(&file); 
QString ex=in.readAll();

now you have all things in the file stored in QString. note that you can replace .txt with any extension you need and also you can add extensions

Upvotes: 0

user1590232
user1590232

Reputation:

And why do you need a file? Just this:

QString foo = ui->textEdit->toPlainText();

Also you need to connect signal "clicked" of QPuhsButton and created slot to get text.

Upvotes: 4

Related Questions