i'm coding in qt. i'm using this to create a csv file.
QtCSV::StringData strData;
QStringList strList0;
int a=10; int b=20;
QStringList strList;
strList << QString::number(a) << QString::number(b);
strData.addRow(strList);
QFile my_file(fileName);
if(my_file.open(QFile::WriteOnly ))
QtCSV::Writer::write(my_file, strData);
the file created and data is writed. the problem is this method of writing create this line of data:
""10","20""
the inner "" is excess. how can i write so that it remove? the other standard csv file does not have this and my file cannot open in some programs.
Related
My goal is to display only files which user wants to see. For instance, "*.h; *.txt" as an input should shows the only *.h and *.txt files in selected folder. The program works for only one input(ie. *.h) The code:
QString mask = ";";
QStringList stringList(ui->lineEdit->text());
ui->lineEdit->setInputMask(mask);
QFileInfoList fileList = qdir.entryInfoList(QStringList() << stringList, QDir::Files, QDir::Size);
The program display when the users enter input only one type of file:
But it does not display when the users enter input as *.h; *.cpp
Best regards!
I solved it.
All I need to do was
QString str = ui->lineEdit->text();
QStringList stringList = str.split(QLatin1Char(';'));
QFileInfoList fileList = qdir.entryInfoList(QStringList() << stringList, QDir::Files, QDir::Size);
I am trying to extract .DAT file using Quazip library in Qt 5.14. I integrated this quazip library into my project and try to extract files using it. .txt file is get extracted but .DAT file is not get extracted. .DAT files are created but it does not contain any data.
Here is my source code
bool fileHelper::extractAll( QString folderPath, QString filePath ) {
QuaZip zip(filePath);
zip.open(QuaZip::mdUnzip);
bool isSuccess = false;
for(bool f=zip.goToFirstFile(); f; f=zip.goToNextFile())
{
// set source file in archive
QString filePath = zip.getCurrentFileName();
QuaZipFile zFile( zip.getZipName(), filePath );
// open the source file
zFile.open( QIODevice::ReadOnly );
// create a bytes array and write the file data into it
//QByteArray ba = zFile.read()
QByteArray ba = zFile.readAll();
// close the source file
zFile.close();
// set destination file
//QFile dstFile( getfileStoreRootDir()+filePath );
QFile dstFile( folderPath+filePath );
qDebug() << "dstFile :" << dstFile;
// open the destination file
dstFile.open( QIODevice::WriteOnly | QIODevice::Text );
// write the data from the bytes array into the destination file
dstFile.write( ba.data() );
//close the destination file
dstFile.close();
//mark extraction sucess
isSuccess = true;
}
zip.close();
return isSuccess; }
Please tell me am I doing something wrong or any other extra flag or something is required for it.
I'm trying to overwrite an existing file using Qt. Since QFileDialog::getSaveFileName will always replace the selected file if it already exists, I've tried creating a custom QFileDialog in over to get to my goal so I used what has been suggested in this link but it seems not to work...
What I've tried so far:
QFileDialog diag(this);
diag.setFileMode(QFileDialog::AnyFile);
diag.setNameFilter(tr("Test (*.tst)"));
diag.setAcceptMode(QFileDialog::AcceptSave);
diag.exec();
QStringList namefile = diag.selectedFiles();
QFile file(namefile[0]);
if(file.open(QIODevice::WriteOnly | QIODevice::Text)){
QTextStream stream(&file);
stream << ui->lineEdit->text() << endl;
}
file.close();
result:
I'm trying to create a simple "To Do list" app in QT Creator while coding the part that loads and saves the list from a file I get stuck on a problem.
If you enter a string like "Do my homework" the program threads the string as it should, but when you load the program again the save file got split in words. So it gets all the entries but each word separated ("Do", "my", "homework").
What is the solution? I tried working with 'char arrays' and 'getline' but they give me nothing but errors.
Here is my code for the save and load parts:
void MainWindow::LoadList(){
std::ifstream load_file("./data.bin");
char loader[255];
while (load_file >> loader){
QString Writer = QString::fromStdString(loader);
ui->lstTaskList->addItem(Writer);
}
}
void MainWindow::SaveList(){
std::ofstream save_file("./data.bin");
for (auto i = 0; i < ui->lstTaskList->count(); i++){
QString Saver = ui->lstTaskList->item(i)->text();
std::string saver = Saver.toStdString();
save_file << saver << std::endl;
}
}
Can anyone help me with this, please?
My thanks in advance...
The anwser was using QFile and QByteArray for me, I knew about QFile but it tries using basic "std" c++ till I learned more about QT.
I'm trying to get the last modified time of a text file I am saving in a notepad app I built.
I can get the file directory using this code:
QString file = QFileDialog::getOpenFileName(this, "Open a file");
ui->filePathLabel->setText(file);
How can I extract a last saved time from this?
Thanks.
Use QFileInfo::lastModified():
const QFileInfo info(file);
const QDateTime lastModified = info.lastModified();
...