Creating text file with Qt Creator on embedded Linux - qt

I'm trying to create a text file by clicking on a button as following code, but I'm not getting.
QString local = "/local/flash/root";
QString name = " ProductionOrder.txt";
void page1000::on_pushButton_3_clicked()
{
QFile file(local+name);
if(!file.open(QFile::WriteOnly|QFile::Text)){
QMessageBox::warning(this,"ERROR","Error open file");
}
QTextStream output(&file);
QString text=ui->plainTextEdit->toPlainText();
output << text;
file.flush();
file.close();
}
what could be wrong with the code?
I am working with Qt 4.8
I don't know what to do

just put "/" at the end of the QString local
Like this: QString local = "/local/flash/root/";

Related

C++ integration Qt

So I wrote a Qt quick application that takes user inputs and stores them in a json file. I now want to add a feature that lets me recall the data in my file and display it in a text field within my application. I can get the text in the C++ portion of my application, Im just not sure how to display it in my user interface. Here is the code to get the text from my json file.
void Jsonfile:: display(){
//1. Open the QFile and write it to a byteArray and close the file
QFile file;
file.setFileName("data.json");
if(!file.open(QIODevice::ReadOnly)){
qDebug() << "file couldn't be opened/found";
return;
}
QByteArray byteArray;
byteArray = file.readAll();
file.close();
//2. Format the content of the byteArray as QJsonDocument
//and check on parse Errors
QJsonParseError parseError;
QJsonDocument jsonDoc;
jsonDoc = QJsonDocument::fromJson(byteArray, &parseError);
if(parseError.error != QJsonParseError::NoError){
qWarning() << "Parse error at " << parseError.offset << ":" << parseError.errorString();
return;
}
QTextStream textStream(stdout);
textStream << jsonDoc.toJson(QJsonDocument::Indented);
}

Can't load/save JPG image in Qt

I can't load/save jpg image in Qt. I checked the imageformat folder in plugins folder and I found the dll/lib files related to this extension.
Here's the code I'm using:
void loadImages(){
QImage image;
QString folderName = "C:\\img-src\\";
bool isLoaded;
for(int i=1;i<3;i++){
QString fileName = folderName + QString::number(i) + ".jpg";
isLoaded = image.load(fileName,"JPG");
if(isLoaded){
qDebug() << "loaded";
}else{
qDebug() << "not loaded";
}
//Rest of the code
}
}
I found what the problem was.
I have to copy the imageformats folder next to the exe file of the program to enable loading/saving jpg files.

QFtp get not working

Greeting
Im trying to download file with QT 4.8 and VS 2008 but i cant, It fail all the time.
I use following code to download file from FTP server.
void FTP::download(QString strFileName, QString strFTPFileName)
{
QFile * file = new QFile(strFileName);
if (m_fFile->open(QIODevice::ReadWrite))
{
QString strFtpServerIPAddress("192.168.7.10");
QString strFtpServerPortNumber("21");
QString strFtpUserName("admin");
QString strFtpPassword("admin");
QString strFtpFolderPath("\outgoing\");
qint16 iFtpPort = 21;
QFtp * ftp = new QFtp();
connect(ftp, SIGNAL(stateChanged(int)),SLOT(onStateChanged(int)));
connect(ftp, SIGNAL(done(bool)),SLOT(onDone(bool)));
connect(m_objFtp, SIGNAL(commandFinished(int, bool)),SLOT(onCommandFinished(int, bool)));
int iConnectToHostID = ftp->connectToHost(strFtpServerIPAddress, iFtpPort);
int iLogInID = ftp->login(strFtpUserName, strFtpPassword);
int iChangeDirectoryID = ftp->cd(strFtpFolderPath);
QFileInfo fileInfo(strFTPFileName);
QString strFileNameOnly(fileInfo.fileName());
int iOperationID = ftp->get(strFileNameOnly, file);
QEventLoop loop;
connect(this, SIGNAL(finished()), &loop, SLOT(quit()));
loop.exec();
ftp->close();
file->close();
delete ftp;
}
}
void FTP::onDone(bool bError)
{
...
//If "id" (saved from onStateChanged) is equal to "iOperationID", I emit "finished" Signal
...
}
I was checking onCommandFinished slot and all steps (HostLookup, Connecting, Connected, LoggedIn) finish without error but exactly in get operation, error is true and when i get detail about error with following codes ,
string strReason = QFtp::errorString().toStdString();
int iError = QFtp::error();
strReason is Unknown error and iError is 0.
Any idea what is going wrong here ?
Thanks in advanced
EDIT 1
I found the problem. Some how i cant download file from root directory, If i try to download file inside another directory, It work.
In my code , I check if strFtpFolderPath is empty or equal to ".", In this case i change directory with ftp->cd("/") other wise i set it to given path.
Any idea why i cant download file from root directory?

Opening text file by using push button in Qt

what should be the contents in the area of signal and slots of the push button in the Qt, so that after clicking the push button only the text file will open.
void MainWindow::on_pushButton_clicked()
{
....
}
You can use whatever you want in order to open file, like FILE, fstream, QFile ecc. You simply call a class method, but inside that function you can put everything.
You're using Qt, so you can check QFile class of QT.
It can be done like:
void MainWindow::on_pushButton_clicked()
{
QFile file( filename );
if( !file.open( QIODevice::ReadOnly | QIODevice::Text ) )
QMessageBox::warning(this, tr("Error opening!"), tr("Could not open the file"));
QTextStream stream( &file );
while( !stream.atEnd() )
{
QString lineText;
lineText = stream.readLine(); //Read a line of text
QStringList tokens= lineText.split(" ",QString::SkipEmptyParts); //Take tokens from the line
}
file.close();
}

How to write decimal values to csv file in qt

I have written a piece of code that should save doubles in an csv file. Here it is:
QString fileName = QFileDialog::getSaveFileName(this,tr("Save Logger Data"), "",tr("LoggerData(*.csv);;All Files (*)"));
if (fileName.isEmpty())
{
return;
}
else
{
QFile file(fileName);
if (!file.open(QIODevice::WriteOnly))
{
QMessageBox::information(this, tr("Unable to open file"), file.errorString());
return;
}
QDataStream out(&file);
out << data1 << "/t" << data2 << "/n";
}
Here, data1 and data2 are doubles. When I open the savefile I only see weird characters (I asume they are hexadecimal values??). How can I change my code so it saves doubles instead of hex?
QDataStream is not the right class for this. For text output use QTextStream instead.

Resources