PDA

View Full Version : QFileDialog how to save???



unix7777
3rd February 2010, 21:06
Hi,

i'm trying to implement QFileDialog saving function.i use Mac OS QT 4.6

What i fo is:

void Nopad::savefile()
{
QFileDialog::getSaveFileName(this,
tr("Save file"), "/VOLUMES/", tr("Image Files (*.txt )"));

}

The question is: how to set the text from textEdit to be saved in .txt file?textEdit is generated in ui_nopad.h

boudie
3rd February 2010, 23:31
You need a QTextStream. See the sample code in the docs.

vishwajeet.dusane
4th February 2010, 05:17
Here try this
Step 1. Let user save .txt file using QFileDialog::getSaveFileName
Step 2. above function will return file name of saved file
Step 3. Open it in using QTextStream or QFile and dump all your data in it.

unix7777
4th February 2010, 08:41
Thanks a lot

unix7777
9th February 2010, 10:35
Here try this
Step 1. Let user save .txt file using QFileDialog::getSaveFileName
Step 2. above function will return file name of saved file
Step 3. Open it in using QTextStream or QFile and dump all your data in it.

Could you give me an example with source....

boudie
9th February 2010, 15:16
First let the user select a filename.



/************************************************** ****************************
saveImage
Opens a dialog where the user can select a path and filename where to save
the screen image.
On entry, we stop the timer to hold the actual screen.
On exit, we start the timer again.
************************************************** ****************************/
void MainWindow::saveImage()
{
refreshTimer->stop();

QString fileName =
QFileDialog::getSaveFileName(this, tr("Save Image"),
"untitled.png", tr("Images (*.png)"));

// you may forget this:
if (fileName != "")
ui.scopeWindow->saveImage(fileName);
// and insert the following code her


refreshTimer->start();
}


In this piece of code I'm saving an image, so skip that part and the timer-parts. Only use the getting the filename.


Then save something to a textfile:


QFile data(fileName);
if (data.open(QFile::WriteOnly | QFile::Truncate)) {
QTextStream out(&data);
out << "some_text";
}

unix7777
9th February 2010, 15:30
Yes it creates file but i try to insert the text from text Edit doing the following:

void Nopad::savefile()
{

QString fileName = QFileDialog::getSaveFileName(this, tr("Save File"),
"/VOLUMES/",
tr("Text (*.txt)"));

QFile data(fileName);
if (data.open(QFile::WriteOnly | QFile::Truncate)) {
QTextStream out(&data);
out << ui->textEdit->setText();
}


}

It gives me an error

boudie
10th February 2010, 15:52
You made a little mistake. :)

Change:
out << ui->textEdit->setText();

into:
out << ui->textEdit->text();

unix7777
10th February 2010, 18:00
You made a little mistake. :)

Change:
out << ui->textEdit->setText();

into:
out << ui->textEdit->text();


The following error appears: class QtextEdit has no member function text() .....

unix7777
11th February 2010, 15:24
any suggestions about what function to use???

boudie
11th February 2010, 22:52
out << ui->textEdit->toPlainText();

unix7777
12th February 2010, 11:23
SUPERB finally it works , now the problem is why the saved file has no extension .txt?How to make it with .txt extension?

hollowhead
12th February 2010, 17:15
I'm a novice too but does this help its from the textedit demo example code?


bool TextEdit::fileSaveAs()
{
QString fn = QFileDialog::getSaveFileName(this, tr("Save as..."),
QString(), tr("ODF files (*.odt);;HTML-Files (*.htm *.html);;All Files (*)"));
if (fn.isEmpty())
return false;
if (! (fn.endsWith(".odt", Qt::CaseInsensitive) || fn.endsWith(".htm", Qt::CaseInsensitive) || fn.endsWith(".html", Qt::CaseInsensitive)) )
fn += ".odt"; // default
setCurrentFileName(fn);
return fileSave();
}