Problem : save text from text Browser
-
hello
i tried this code to save text from text browser but it only create a blank text document
@
void MainWindow::fileSave()
{
QFile sFile (mFilename); //mFilename is a QStringif( !sFile.open (QFile::WriteOnly | QFile::Text)) return fileSaveAs(); if(sFile.open(QFile::WriteOnly | QFile::Text)){ QTextStream out(&sFile); out<<textView->toPlainText(); ; sFile.flush(); sFile.close(); }}; void MainWindow::fileSaveAs() {
QString File = QFileDialog::getSaveFileName(this, tr("Save as..."),QString(), tr("txt (*.txt);")) ;
if (!File.isEmpty()){ mFilename = File ; fileSave();
}};@
i made a button that open a file in the text browser and it work but when i save it again it become blank .
-
Hi,
You don't do anything if open fails. You should at least use a QMessageBox to show a warning.
On a side note, this pretty much continues this "thread":http://qt-project.org/forums/viewthread/43815/ you should have rather kept it there.
-
hi
i have no problem with open file my problem is with saving it .
i can save a plaintext with the same code but here i have a textbrowser .
what should i do ?
thank you -
QTextBrowser is a QTextEdit so it should behave pretty much the same.
One quick way to verify the content of your QTextBrowser is to
@qDebug() << textView->toPlainText();@
Anyway, it's still not a clean technique to open a file twice.
-
thank you so much :)
the code became :
@
void MainWindow::fileSave()
{
QFile sFile(mFilename);if(sFile.open(QFile::WriteOnly | QFile::Text)){ QTextStream out(&sFile); out <<textView->toPlainText(); sFile.flush(); sFile.close(); }};
@
i wish to become an expert like you someday :) -
You should still have a at least something that tells you if open failed e.g. you user choose a read-only file.
Keep practicing young one ;) don't hesitate to ask questions to more experienced developers and read documentation (books, Qt's documentation examples etc.)