L
L
l2p2014-05-09 21:59:05
Qt
l2p, 2014-05-09 21:59:05

How to write to a file (QT)?

QFile file2;
file2.setFileName(QString("r.txt"));
if(file2.open(QIODevice::ReadWrite)) qDebug("Op");
if(file2.exists()) qDebug("Ex");
if(file2.write("ya")) qDebug("ololo");
file2.close();

In another project, these lines work. What might they be in conflict with?
#include <QMainWindow>
#include <QtDeclarative/QDeclarativeView>
#include <QGraphicsObject>
#include <QtGui>
#include <QDeclarativeContext>
#include <QDeclarativeEngine>
#include <QSystemTrayIcon>
#include <QTime>
#include <QFile>
#include <QTextStream>
#include <QTimer>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
EXL, 2014-05-10
@l2p

void ScriptWidget::onSaveButtonClick()
{
    QFile updateScriptFile("update-script");
    if (!updateScriptFile.open(QFile::WriteOnly | QFile::Text)) {
        QMessageBox::critical(this, tr("I/O Error"), tr("Error opening file %1\n"
                                                        "Error: %2")
                              .arg(updateScriptFile.fileName())
                              .arg(updateScriptFile.errorString()));
        return;
    }

    QTextStream out(&updateScriptFile);
    out << ui->textEdit->toPlainText(); // Get text from the QTextEdit Widget
    updateScriptFile.close();
    emit signalSendMessageToStatusBar(QString(tr("File Saved.")));
}

This is how I write it, everything seems to be working.
Use the documentation and Assistant.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question