I need an editor of mine to evaluate the JS code in whatever JS file I have open. However, it never does. Although the app output says
QIODevice::read: device not open
Here's my code (mFilename is the variable that holds the open file's filename)
QFile sFile(mFilename);
QTextStream in(&sFile);
text = in.readAll();
sFile.close();
ui->webView->page()->mainFrame()->evaluateJavaScript(text);
You forgot to open the file. To open it you have to use QFile::open method.
If you want to read it just call the file.open like that:
sFile.open(QIODevice::ReadOnly);
Indeed you didn't open the file by calling the QFile constructor.
So, your code would now be:
QFile sFile(mFilename);
QTextStream in(&sFile);
sFile.open(QIODevice::ReadOnly);
text = in.readAll();
sFile.close();
ui->webView->page()->mainFrame()->evaluateJavaScript(text);
Related
I am currently starting to develop a QT desktop application, to edit the scripting language "Lua". The implementation should be pretty basic, opening Lua extension files, saving and editing them. The problem that i have stumbled upon is that I want to be able to open/save/edit just Lua files. While reading a QT documentation i stumbled upon an explanation of how you can open files for a so called "notepad" editor. They have provided the following example code:
QString fileName = QFileDialog::getOpenFileName(this, "Open the file");
QFile file(fileName);
currentFile = fileName;
if (!file.open(QIODevice::ReadOnly | QFile::Text)) {
QMessageBox::warning(this, "Warning", "Cannot open file: " + file.errorString());
return;
}
setWindowTitle(fileName);
QTextStream in(&file);
QString text = in.readAll();
ui->textEdit->setText(text);
file.close();
So here they basically add a condition where the file was unable to open, (it was in this line of code if (!file.open(QIODevice::ReadOnly | QFile::Text))) but they don't specify what the condition should look like if I only want to be able to open certain types of files (in my case, lua files). The same goes for the "save" option that they have displayed.. So my question is, how should I extend this condition, to check if the files has the given extension type for Lua ? Thanks in advance.
getOpenFileName can take more arguments (has default values for some of the arguments), see the documentation here.
So your code will be something like:
QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"),
"", //default path here
tr("Lua files (*.lua)"));
You could try this:
void QFileDialog::setNameFilter(const QString &filter)
Sets the filter used in the file dialog to the given filter.
/* If filter contains a pair of parentheses containing one or
more filename-wildcard patterns, separated by spaces, then
only the text contained in the parentheses is used as the filter.
This means that these calls are all equivalent: */
dialog.setNameFilter("All Lua files (*.lua)");
Taken from the docs:
https://doc.qt.io/qt-5/qfiledialog.html#setNameFilter
This question already has an answer here:
Writting in text file error, QT
(1 answer)
Closed 4 years ago.
i have the following problem, i have an resource file called data.txt and i want to open it with write permission.
Im using QFile and QTextStream to work with it.
I can only open the File with ReadOnly Acces but not with ReadWrite or WriteOnly acces.
Export functions with similar code working fine its only not working on the resource file.
I already tried to change the front slashes to double backslashes, i runned hunderd times the qmake and rebuild, i restartet my computer and resotre the resource file.
I already checked alot of entries on Stack but wasnt able to find one that resolves my problem. (Most questions were spelling issues like only one backslash).
QFile file(":/savelocation/data.txt");
if (!file.exists())
{
qDebug()<<"File not exist";
}
file.open(QIODevice::ReadWrite | QIODevice::Text);
if (file.isOpen())
{
qDebug()<<"File is open";
QTextStream out(&file);
out<< "something" << endl;
}
else
{
qDebug()<<"File is not open";
}
file.close();
file.open(QIODevice::ReadOnly);
if (file.isOpen())
{
qDebug()<<"File is open as read only";
}
else
{
qDebug()<<"File is not open as read only";
}
file.close();
Actual result:
My Application output of the code:
File is not open
File is open as read only
Its only possible to open it as ReadOnly for me.
Before i implemented the if i got the following output:
QIODevice::write (QFile,":/savelocation/data.txt"): device not open
Expected result:
The file would be opened with write access.
Thanks in advance.
As per the documentation, resources are embedded in your binary file and are thus read-only, both conceptually and practically.
You will have to save your data to a writable location on the filesystem, for example under QDir::home() or the current working directory QDir::current() .
Using QByteArray QIODevice::readAll() from QT5, I was able to make a bytes array from a txt file or an image, used decode after and recreated the file correctly. But, when I tried with a .db file (SQLITE) it didn't work.
I noticed that when you open a .db with a text editor, you will see "SQLite format 3" followed by encoded characters. After making a QByteArray from a .db file, followed by decode() to recreate the file, when I opened it with a text editor, the file only contains the text "SQLite format 3".
Does QByteArray only work with txt file or Image file?
If it does, how can I make a Array of bytes from a .db (SQLITE) file.
Thanks
Update1 (The code belows works):
QFile file("C:/database.db");
if(!file.open(QIODevice::ReadOnly))
qDebug()<<"You are stupid!";
QByteArray byteArray = file.readAll();
QFile file2("C:/database2.db");
file2.open(QIODevice::WriteOnly);
file2.write(byteArray);
file2.close();
file.close();
Update2:
About the decode I mentioned in my initial question, I was using the following:
QString QFile::decodeName(const QByteArray & localFileName)
which make no sense when you read carefully the documentation and was just wrong. :)
You should not open that file with QIODevice::Text flag.
Check this http://doc.qt.io/qt-5/qiodevice.html#OpenModeFlag-enum
I'm writing a tool for private use. The problem is that Qt cannot read a text file containing all contents published here.
It only reads this
The three points were pasted by Qt.
My code for reading the file is following
QFile file;
file.setFileName(m_filename);
if (!file.open(QIODevice::ReadOnly))
return;
QTextStream in(&file);
while (!in.atEnd()) {
m_fileContents += in.readLine();
}
file.close();
Do you have any idea why it doesn't work?
QFile file;
file.setFileName(m_filename);
if (!file.open(QIODevice::ReadOnly))
return;
m_fileContents = file.readAll();
I just tested your code on my own computer with your data and it works well.
If you're using an IDE, maybe it does not display all the text of your final string and this is why you have three dot at the end of your sample.
Also as evilruff suggest you can use QFile::readAll method directly.
QLabel* codeLabel = new Qlabel;
QFile file("C:\index.txt");
file.open(stderr, QIODevice::WriteOnly);
QByteArray data;
data = file.readAll();
codeLabel->setText("test"+QString(data));
file.close();
Then there is only "test" in QLabel.
Help, Please
Aside from the fact you should escape backslashes within C-style strings (c:\\index.txt), you have a problem with the following sequence:
// vvvvvvvvv
file.open(stderr, QIODevice::WriteOnly);
:
data = file.readAll();
// ^^^^
What exactly did you think was going to happen when you opened the file write-only, then tried to read it? You need to open it for reading such as with QIODevice::ReadOnly or QIODevice::ReadWrite.
On top of that, you should check the return code of all functions that fail by giving you a return code. You currently have no idea whether the file.open() worked or not.
I'm also not convinced that you should be opening stderr (which is really an ouput "device") for input. You'll almost certainly never get any actual data coming in on that file descriptor, which is probably why your input is empty.
You need to step back and ask what you're trying to acheive. For example, are you trying to capture everything your process sends to standard error? If so, it's not going to work that way.
If you're just trying to read the index.txt file, you're using the wrong overload. Remove the stderr parameter altogether:
file.open (QIODevice::ReadOnly);
If it's something else you're trying to do, add that to the question.
file.open(stderr, QIODevice::WriteOnly);
this closes the file again and reopens with the stderr stream in write only mode
you'll want to change that to
file.open(QIODevice::ReadOnly);
QFile file("C:\index.txt");
Here you try to open a file called: C:index.txt because '\i' is converted to i. You want to double you backslash:
QFile file("C:\\index.txt");
Because you read from a file you opened write-only.