Overwrite an existing file using Qt - qt

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:

Related

pleora sdk convert PvBuffer or PvRawData to QByteArray

I'm using the pleora sdk to capture images from an external camera and I am able to successfully write the data to tiff image files on disk. My next step is to change the data storage to SQLite instead of disk files.
I have PvBuffer *lBuffer pointer working fine. Now I need to convert that data to a format I can use to write to SQLite. I'm using Qt on linux so the QByteArray would be very convenient.
This is kind of a specific question for the pleora sdk and Qt. I'm hoping someone has experience with this.
PvRawData *rawData = lBuffer->GetRawData();
QByteArray ba;
//Need to copy the data from rawData to ba.
Thank you in advance.
I found an answer and wanted to post in case anybody else has something similar. I uses the reintepret_cast method.
data = lBuffer->GetDataPointer()
imgSize = lBuffer->GetPayloadSize();
const char *d = reinterpret_cast<char *>(data);
QByteArray ba(d, imgSize);
QSqlQuery q = QSqlQuery( db );
q.prepare("INSERT INTO imgData (image) values (:imageData)");
q.bindValue(":imageData", ba);
if ( !q.exec() )
qDebug() << "Error inserting image into table: " << q.lastError() << endl;
else
qDebug() << "Query executed properly" << endl;

How to use QTextStream with PythonQT?

I am writing a plugin for a Qt desktop app using PythonQT.
I wonder how to use << operator in python.
QTextStream stream(&file);
stream << doc.toString();
Any hints?
How may I ask Python to list all methods for a given class like QTextStream?
Or is there another way in Python to write a QDomDocument to a QFile?
Found a solution already...
doc = QDomDocument()
root = doc.createElement("Animation")
doc.appendChild(root)
stream = QTextStream(xmlfile)
doc.save(stream, 0)

Need to display in qtextEdit real time

I have an arm board with a touchscreen display, where I want to display the output from a certain function, vcm_test(). The output of this function is saved to a file called
test.txt . Now I am able to read the contents of the file test.txt and display it in my qtextEdit only if it is less than 50-60 lines. Whereas I have more than 7000 lines in the test.txt . When I try to display 7000 lines the arm board keeps reading and nothing is displayed until reading is complete. Is there any way to read and display after every line or say every 10 lines. I thought of using qProcess in readfile too, but I have no idea how I can do that.
connect(ui->readfil, SIGNAL(clicked()), SLOT(readfile()));
connect(ui->VCMon, SIGNAL(clicked()), SLOT(vcm_test()));
connect(ui->Offloaderon, SIGNAL(clicked()), SLOT(offloader_test()));
connect(ui->quitVCM, SIGNAL(clicked()),vcmprocess, SLOT(kill()));
connect(ui->quitoffloader, SIGNAL(clicked()),offloaderprocess, SLOT(kill()));}
MainWindow::~MainWindow(){
delete ui;}
void MainWindow::readfile(){
QString filename="/ftest/test.txt";
QFile file(filename);
if(!file.exists()){
qDebug() << "NO file exists "<<filename;}
else{
qDebug() << filename<<" found...";}
QString line;
ui->textEdit->clear();
if (file.open(QIODevice::ReadOnly | QIODevice::Text)){
QTextStream stream(&file);
while (!stream.atEnd()){
line = stream.readLine();
ui->textEdit->setText(ui->textEdit->toPlainText()+line+"\n");
qDebug() << "line: "<<line;}
}
file.close();}
void MainWindow::vcm_test(){
vcmprocess->start("/ftest/vcm_test_2");}
void MainWindow::offloader_test(){
offloaderprocess->start("/ftest/off_test_2");}
Any advice is really appreciated.Thanks.
You could use QApplication::processEvents() after reading every line and appending it to your text edit. But you should be really careful when using this, and I would not recommend doing so. You should also consider using QTextEdit::Append() instead of setText.
A better solution is to read the file in another thread and use signals and slots to send read data that you want to append to your QTextEdit.

Read exif metadata of images in Qt

In my Qt app I want to read exif data of images. QImage or QPixmap don't seem to provide such hooks.
Is there any API in Qt that allows reading exif without using external libraries like libexif?
EDIT: This is a duplicate of this
For me, the best choice was easyexif by Mayank Lahiri. You only need to add two files exif.cpp and exif.h to your project.
int main(int argc, char *argv[])
{
for (int i=1; i<argc; ++i){
QFile file(argv[i]);
if (file.open(QIODevice::ReadOnly)){
QByteArray data = file.readAll();
easyexif::EXIFInfo info;
if (int code = info.parseFrom((unsigned char *)data.data(), data.size())){
qDebug() << "Error parsing EXIF: code " << code;
continue;
}
qDebug() << "Camera model : " << info.Model.c_str();
qDebug() << "Original date/time : " << info.DateTimeOriginal.c_str();
} else
qDebug() << "Can't open file:" << argv[i];
}
return 0;
}
Try QExifImageHeader from qt extended framework. qtextended.org is not available for me? but you may search for other download mirrows.
QImageReader has a method named transformation() which is introduced in version 5.5, first you should try that.
You can also check the following link to see how it's done using Windows GDI in Qt, http://amin-ahmadi.com/2015/12/17/how-to-read-image-orientation-in-qt-using-stored-exif/

QDesktopServices::openUrl with selecting specified file in explorer

In most coding programs, you can right click on the item and click show in explorer and it shows the file in explorer with the item selected. How would you do that in Qt with QDesktopServices? (or any way to do it in QT)
you can use this method to select file on Windows or MacOS ,if you want select on linux you can find a way in QtCreator sources.
void select(const QString& path){
#if defined(Q_OS_WIN)
const QString explorer = "explorer";
QStringList param;
if (!QFileInfo(path).isDir())
param << QLatin1String("/select,");
param << QDir::toNativeSeparators(path);
QProcess::startDetached(explorer, param);
#elif defined(Q_OS_MAC)
QStringList scriptArgs;
scriptArgs << QLatin1String("-e")
<< QString::fromLatin1("tell application \"Finder\" to reveal POSIX file \"%1\"")
.arg(path);
QProcess::execute(QLatin1String("/usr/bin/osascript"), scriptArgs);
scriptArgs.clear();
scriptArgs << QLatin1String("-e")
<< QLatin1String("tell application \"Finder\" to activate");
QProcess::execute("/usr/bin/osascript", scriptArgs);
Have you tried using the file:/// syntax? The following is taken from a code base I'm working with:
PyQt4.QtGui.QDesktopServices.openUrl(PyQt4.QtCore.QUrl('file:///%s' % dirname))

Resources