Is QFileInfo synchronized with file? - c++

Is QFileInfo synchronized with source file? Or it just stores information from the time when the instance was created?
In otherwords, if I create QFileInfo instance for my file. Then I change the file. And then, I create second instance of QFileInfo for this file, will they have different lastModified value?

QFileInfo is not syncronized, the information is read when the object is created.
I have used this small example to verify it.
If you run this in a terminal and then change the file you will see the date changing when you save the file.
#include <QCoreApplication>
#include <QDebug>
#include <QFileInfo>
#include <QDateTime>
#include <QThread>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
while (1)
{
QFileInfo f("./file.txt");
qDebug() << f.lastModified().toString();
QThread::sleep ( 1 );
}
return a.exec();
}

Related

QSettings iniFormat

I am trying to create an ini file from my application (Qt5.9.6):
#include <QCoreApplication>
#include <QSettings>
int main(int argc, char* argv[])
{
QCoreApplication app(argc, argv);
QSettings settings(QStringLiteral("C:\\test.ini"), QSettings::IniFormat);
settings.setValue(QStringLiteral("global/mykey"), 1234);
settings.sync();
app.exec();
}
The file is created, however the content is not what I expect:
[global]
mykey="\x1\x1\x1\x1"
Looks like it was a bug introduced by a colleage into our custom built Qt library.

Dark theme does not apply to the title bar

I have applied a dark style to my application, so good until then, my question is why the style is not applied to the title bar of my application, and the rest of the forms that open in me application, as you can see, it stays white and looks very bad, any suggestions would be appreciated.
#include "mainwindow.h"
#include <QApplication>
#include <QFile>
#include <QTextStream>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QFile f(":/qdarkstyle/style.qss");
f.open(QIODevice::ReadOnly|QIODevice::Text);
QTextStream ts(&f);
a.setStyleSheet(ts.readAll());
MainWindow w;
w.show();
return a.exec();
}
I got the subject from here.
https://github.com/ColinDuquesnoy/QDarkStyleSheet
[Works on Qt 6.2.2, not sure about Qt5]
There might be a configuration file qt.conf in the same folder as your executable file. If it does not exist, create. And add the lines
[Platforms]
WindowsArguments = darkmode=1
Use darkmode=2 if you also want to change the title bar colour on the fly.

I am unable to reference QSettings using Qt

I am running a project that uses Qt. I have installed version 5.10.1 (msvc2017_64). I receive an error that the QSettings is undefined. I cannot find the reference in the libraries installed.
#include "mainwindow.h"
#include <QtWidgets/QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QCoreApplication::setApplicationName("Circuit Editor");
QSettings settings;
MainWindow w;
w.setWindowTitle("Circuit Editor");
w.show();
return a.exec();
}
My two cents:
1) You forgot to include the header:
#include <QSettings>
2) You declared a QSetting object but you are not doing anything with it. Is that what you want to do?
Never use #include <QtModule/QClass> format of includes. They hide project misconfiguration. Use #include <QClass> instead.
If you're unsure of what to include, include the entire module and deal with details later: #include <QtModule>.
Thus:
#include "mainwindow.h"
#if 1
// entire module
#include <QtWidgets>
#else
// needed classes
#include <QApplication>
#include <QSettings>
#endif
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QCoreApplication::setApplicationName("Circuit Editor");
QSettings settings;
MainWindow w;
w.setWindowTitle("Circuit Editor");
w.show();
return a.exec();
}

Qt Creator _IMP error while connecting to MySql

I'm trying to create an app that connects to my local MySql through XAMPP and outputs the data into a tableWidget.
After writing all the code for it and correcting all the mistakes, I'm getting 17 errors about "undefined reference to `_imp__ZN.....' "
1st Question: Do I have to put all my project files inside XAMPP/htdocs like I did using PHP?
2nd Question: I've only written something inside main.cpp, did I miss something for other classes or maybe I need something else inside the header file?
I've heard that "imp_" problems are related to the compiler, but I'm not sure what to do!
Without further ado, this is my main.cpp:
#include "mainwindow.h"
#include <QApplication>
#include <QtSql/QSql>
#include <QtGui/QtGui>
#include <QMessageBox>
#include <QtSql/QSqlDatabase>
#include <QTableWidget>
#include <QtSql/QSqlError>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QTableWidget* tableWidget = new QTableWidget();
tableWidget->setWindowTitle("Connect to Mysql Database");
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("localhost");
db.setDatabaseName("pizzeria mabo");
db.setUserName("root");
db.setPassword("");
if (!db.open())
{
QSqlError err = db.lastError();
QMessageBox::critical(0, QObject::tr("Database Error"), err.text());
}
MainWindow w;
w.show();
return a.exec();
}
And here are my errors:
http://i57.tinypic.com/nodow7.png
Thank you for your help!
Well, the errors mean that you are missing a library containing the necessary symbols. If you are using qmake to build the project you have to enable SQL via "QT += sql". Otherwise you will have to find and add the library manually.

C++:QX11EmbedContainer not working on Qt

Given below is the code that I have written for opening VLC from Qt. I want to use the QX11EmbedContainer so that I can use it to "contain" VLC.
#include "mainwindow.h"
#include <QApplication>
#include <QProcess>
#include <QX11EmbedContainer>
using namespace std;
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QString prog="C:/Users/Administrator/Desktop/... .exe";
QProcess *proc=new QProcess();
proc->start(prog);
return a.exec();
}
On adding the header file QX11EmbedContainer I get the following error:
C:\Users\Administrator\Documents\HA\main.cpp:4: error: C1083: Cannot open include file: 'QX11EmbedContainer': No such file or directory
Is QX11EmbedContainer an add-on? If not then where am I going wrong?