Solved QT FILE OPERATION
-
sir
I am working on Nano pc t2 board
pc os ubuntu 16.04
Nano pc t2 OS 16.04 ubuntu
i am able to do all the file operation (open read close and write )in pc,but if i cross compile and run it on my kit(nano pc t2) file operation are not working#include "mainwindow.h" #include "ui_mainwindow.h" #include<qfile.h> #include <QTextStream> #include <QtCore> QFile mainsort(QDir::current().path()+"/ms.csv"); QByteArray temp1; MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); } MainWindow::~MainWindow() { delete ui; } void MainWindow::on_pushButton_pressed() { if ( mainsort.open(QIODevice::ReadWrite | QIODevice::Truncate | QIODevice::Text) ) { QTextStream out(&mainsort); out << "something"; ui->label->setText("WRitten"); } mainsort.close(); } void MainWindow::on_pushButton_2_pressed() { QCoreApplication::quit(); } void MainWindow::on_pushButton_3_pressed() { mainsort.open(QIODevice::ReadOnly); temp1=mainsort.readAll(); mainsort.close(); ui->label->setText( QString::fromUtf8(temp1)); }
-
@swansorter
Slightly strange that you just get a number, and it seems to mean I/O Error, which is also a touch strange.Nevertheless, @jsulm has told you above what you need to use. Pick a
QStandardPaths
suitable for the user to have write permission, for exampleQStandardPaths::HomeLocation
.3.i wanted place file where my application file is located
This is a bad idea. The application file/executable is likely to be placed in a directory to which the user does not have write access, and anyway it's not a good place for data files.
-
From your code we don't even know what mainsort is. Probably a file, but where it is and how you try to open it, remain mysteries. Perhaps there is relative path issue.
-
@mvuori mainsort is file name,it is in poject directory path
-
@swansorter but the executable is in the build directory, not the project directory.
Regards
-
@aha_1980 In pc it working fine .In buid directory executable is there and my mainsort file also there only.
but it is not happening in my board
-
@swansorter
What is not happening on board? Where is the file located, and how are you expecting it to be picked up with the path you specify? What are you expectingQDir::current()
to be in each case? Why don't you at least output it in each case so they you/we know where it is looking? And why doesn't your code give any indication to anybody if it fails to open the file? And no call toQFile::error
/errorString()
.Also, personally, I would not try to access
QDir::current()
at global scope. It will presumably be evaluated prior to whateverQApplication
you create. Wouldn't it be safer to have it execute that after creating the application? There is no need to have those two variables as globals. -
@JonB
1.file creation not happening
2.located in opt/QtE_demo /
3.i wanted place file where my application file is located -
@swansorter said in QT FILE OPERATION:
opt/QtE_demo /
Do you mean /opt/QtE_demo/? If so /opt is not writeable by normal users.
-
@jsulm then where i can place ?
-
@swansorter User data is usually stored in user home directory.
Check https://doc.qt.io/qt-5/qstandardpaths.html to see how to get various standard locations. -
@swansorter said in QT FILE OPERATION:
1.file creation not happening
What did you do about printing
QFile::error
/errorString()
if file open fails? -
@JonB getting error 5
-
@jsulm i will check
-
@swansorter said in QT FILE OPERATION:
@JonB getting error 5
That is what
QFile::errorString()
reports? -
@JonB yea
-
@swansorter said in QT FILE OPERATION:
QFile mainsort(QDir::current().path()+"/ms.csv");
I don't think this is a good idea to do this, at least with a global variable!
Have you checked, on the Nano pc on which path you tried to write?
Have you checked the directory is writable / the application is allowed to write there? -
@swansorter
Slightly strange that you just get a number, and it seems to mean I/O Error, which is also a touch strange.Nevertheless, @jsulm has told you above what you need to use. Pick a
QStandardPaths
suitable for the user to have write permission, for exampleQStandardPaths::HomeLocation
.3.i wanted place file where my application file is located
This is a bad idea. The application file/executable is likely to be placed in a directory to which the user does not have write access, and anyway it's not a good place for data files.
-
@JonB thank very much.
i created separate folder for file operation in home location -
@KroMignon thank you sir for your information