QFileDialog created without file extension in linux - qt

I am creating a file in both windows and linux using QFileDialog
fileName = QFileDialog::getSaveFileName(this, tr("Create project"), applicationPath,tr("Files (*.MSC)"));
In windows the file is created as path/to/file.MSC
but in linux file is created as path/to/file
why .MSC is not appending in Linux, whether we need to use other function for this

The following example works fine on Linux. You get the file myfile.MSC with the text "test" written on it.
#include <iostream>
#include <QApplication>
#include <QFileDialog>
#include <QString>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QString applicationPath = QDir::currentPath() + "/myfile.MSC";
QString fileName = QFileDialog::getSaveFileName(0,
QApplication::tr("Create project"),
applicationPath,
QApplication::tr("Files (*.MSC)"));
if (fileName.isEmpty())
return -1;
else {
QFile file(fileName);
if (!file.open(QIODevice::WriteOnly)) {
std::cout << "error\n";
return -1;
}
QDataStream out(&file);
out << "test";
}
return a.exec();
}

Related

Qt: Handle 'Save Print Output As' dialog with QPainter::begin()

here is a minimal reproducable example of my problem:
#include <QCoreApplication>
#include <QtPrintSupport/QPrinter>
#include <QPainter>
#include <QDebug>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QPrinter printer;
QPainter painter;
// this will open a file dialog to save a pdf file
if (!painter.begin(&printer)) {
qDebug() << "how to get the difference between 'Dialog Canceled' and 'File in use' here?";
}
else {
//do the painting stuf...
}
return a.exec();
}
My question is - as already mentioned in the code - how do I know if the dialog is canceled by the user or the pdf file is just in use (for example opened in Adobe Reader)?
I already tried to check the printer.outputFormat() and the printer.docName() but they are both empty.

How to show every qDebug with new window?

I make a QT console program.
I want to show every command by new window.
How could I do that ??
main.cpp
I use system is win10 with QT.
#include <QCoreApplication>
#include <QDebug> //在文字視窗輸出文字功能函式
#include <QDir> //搜尋資料夾功能函式
#include <QFileInfo> //顯示檔案資訊
#include <QString> //字串函式
#include <QStringList> //字串陣列函式
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QDir mDir; //設定資料夾位置
qDebug() << mDir.exists(); //輸出確定是否有mDir資料夾
QString command = "frtomem -p WnaYooAQ ";
foreach(QFileInfo mitm, mDir.entryInfoList(Qfilter)) //列出所有資料夾中篩選的檔案內容
{
qDebug() << command << mitm.fileName();
}
return a.exec();
}
I just can show it by one console window.
can't separete it.

Setting LocalStorage Location via setOfflineStoragePath

I'm trying to set the LocalStorage location (sqlite db) for my QML application but once i rebuild and run the application i still can't see the subfolder, INI file and the sqlite DB created on the desired location (in a subfolder within the resources folder). Here is what's in my main file.
Appreciate any one could pint what's I'm doing wrong here?
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include <QString>
#include <QDebug>
#include <QDir>
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
//engine.setOfflineStoragePath("qrc:/");
auto offlineStoragePath = QUrl::fromLocalFile(engine.offlineStoragePath());
engine.rootContext()->setContextProperty("offlineStoragePath", offlineStoragePath);
QString customPath = "qrc:/OffLineStorage";
QDir dir;
if(dir.mkpath(QString(customPath))){
qDebug() << "Default path >> "+engine.offlineStoragePath();
engine.setOfflineStoragePath(QString(customPath));
qDebug() << "New path >> "+engine.offlineStoragePath();
}
return app.exec();
}
By looking at the code snippet in your question, everything looks fine.
Anyway, you should verify if the following line actually returns true as you expect:
dir.mkpath(QString(customPath)
If no, the body of the if statement isn't executed in any case, thus setOfflineStoragePath is never invoked.
As a hint, using qrc:/OffLineStorage as a path for your storage doesn't seem to be a good idea. I'm not sure it will work once in the production environment (to be checked, it sounds strange, but it could work).
Try use engine.setOfflineStoragePath before engine.load.
Using qrc:/OffLineStorage as a path for your storage doesn't seem to
be a good idea. I'm not sure it will work once in the production
environment
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include <QString>
#include <QDebug>
#include <QDir>
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
QString customPath = "qrc:/OffLineStorage";
QDir dir;
if(dir.mkpath(QString(customPath))){
qDebug() << "Default path >> "+engine.offlineStoragePath();
engine.setOfflineStoragePath(QString(customPath));
qDebug() << "New path >> "+engine.offlineStoragePath();
}
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
engine.clearComponentCache();
if (engine.rootObjects().isEmpty())
return -1;
return app.exec();
}

Renaming files with Qt

I'm trying to write a program that renames a certain list of files in a chosen directory with a new extension. Pretty much to replace all .dx90 files with .dx80 files. This is the code that I've written so far and it's not working. All the files are being placed into the failed files list but I'm getting no errors.
#include <QFileDialog>
#include <QString>
#include <QApplication>
#include <QDir>
#include <QStringList>
#include <QTextStream>
QTextStream cout(stdout);
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
QString dirName = QFileDialog::getExistingDirectory(0, "Open Directory", QDir::currentPath(), QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
QDir directory(dirName);
QStringList filters;
filters << "*.dx90";
QStringList files = directory.entryList(filters);
QStringList changedFiles, failedFiles;
foreach(QString filename, files)
{
QFileInfo info(filename);
QString rawFileName = filename.section(".", 0, 0);
QString newName = info.absoluteFilePath().section("/", 0, -2) + "/" + rawFileName + ".dx80";
bool success = directory.rename(info.absoluteFilePath(), newName);
if(success)
{
changedFiles << info.absoluteFilePath();
}
else
{
failedFiles << info.absoluteFilePath();
}
}
return 0;
}
I figured it out. The error I made was in the line:
QFileInfo info(filename);
It was not finding the file as the variable filename was not the absolute path. This made the info variable default to the QFileInfo of the current working directory of the application. I fixed the code by changing that line to:
QFileInfo info(directory.absolutePath() + "/" + filename);
Thanks to anyone that tried to help me fix the code. I hope this helps others that have similar problems.

Save QList<int> to QSettings

I want to save a QList<int> to my QSettings without looping through it.
I know that I could use writeArray() and a loop to save all items or to write the QList to a QByteArray and save this but then it is not human readable in my INI file..
Currently I am using the following to transform my QList<int> to QList<QVariant>:
QList<QVariant> variantList;
//Temp is the QList<int>
for (int i = 0; i < temp.size(); i++)
variantList.append(temp.at(i));
And to save this QList<Variant> to my Settings I use the following code:
QVariant list;
list.setValue(variantList);
//saveSession is my QSettings object
saveSession.setValue("MyList", list);
The QList is correctly saved to my INI file as I can see (comma seperated list of my ints)
But the function crashes on exit.
I already tried to use a pointer to my QSettings object instead but then it crashes on deleting the pointer ..
QSettings::setValue() needs QVariant as a second parameter. To pass QList as QVariant, you have to declare it as a Qt meta type. Here's the code snippet that demonstrates how to register a type as meta type:
#include <QCoreApplication>
#include <QDebug>
#include <QMetaType>
#include <QSettings>
#include <QVariant>
Q_DECLARE_METATYPE(QList<int>)
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
qRegisterMetaTypeStreamOperators<QList<int> >("QList<int>");
QList<int> myList;
myList.append(1);
myList.append(2);
myList.append(3);
QSettings settings("Moose Soft", "Facturo-Pro");
settings.setValue("foo", QVariant::fromValue(myList));
QList<int> myList2 = settings.value("foo").value<QList<int> >();
qDebug() << myList2;
return 0;
}
You might have to register QList as a meta-type of its own for it to work. This is a good starting point to read up on meta-types in Qt: http://qt.nokia.com/doc/4.6/qmetatype.html#details .
I was also struggling with this ... and I believe I now have a decent solution.
I hope this saves someone the trouble, it caused me.
#include <QCoreApplication>
#include <QSettings>
#include <QList>
#include <QDataStream>
#include <QVariant>
#include <QVariantList>
#include <QDebug>
#include <deque>
template <class T> static QVariant toVariant(const QList<T> &list)
{
QVariantList variantList;
variantList.reserve(list.size());
for (const auto& v : list)
{
variantList.append(v);
}
return variantList;
}
template <class T> static QList<T> toList(const QVariant &qv)
{
QList <T> dataList;
foreach(QVariant v, qv.value<QVariantList>()) {
dataList << v.value<T>();
}
return dataList;
}
void Gen()
{
QList<QString> data {"hello", "world","how", "are", "you"};
QList<int> ages {10,20,30,40};
QSettings setup("stuff.ini", QSettings::IniFormat);
setup.beginWriteArray("rules");
for (int groups=0;groups<3;groups++)
{
setup.setArrayIndex(groups);
setup.setValue("rule",QString("Rule-%1").arg(groups));
setup.setValue("data", toVariant (data));
setup.setValue("ages", toVariant (ages));
}
setup.endArray();
setup.sync();
}
void Read()
{
QSettings setupR("stuff.ini", QSettings::IniFormat);
int rule_count = setupR.beginReadArray("rules");
for (int groups=0;groups<rule_count;groups++)
{
setupR.setArrayIndex(groups);
QString nameRead = setupR.value("rule").toString();
QList<QString> dataRead = toList<QString>(setupR.value("data"));
qDebug() << "Rule " << groups;
qDebug() << "Rule Read" << nameRead;
qDebug() << "Data Read2" << dataRead;
}
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
// Write
Gen();
// Read
Read();
exit(0);
//return a.exec();
}
You should end up with an INI file which looks like this ...
[rules]
1\ages=10, 20, 30, 40
1\data=hello, world, how, are, you
1\rule=Rule-0
2\ages=10, 20, 30, 40
2\data=hello, world, how, are, you
2\rule=Rule-1
3\ages=10, 20, 30, 40
3\data=hello, world, how, are, you
3\rule=Rule-2
size=3
The nice thing here is you can edit this outside of QT (Just be careful) ...

Resources