Iterate through registry folders with Qt - qt

I need to list the devices in the HID Bluetooth LE (BTHLE) folder of the HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\ directory. By using
#define REG_PATH "HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Enum\\BTHLE"
QSettings settings(REG_PATH, QSettings::NativeFormat);
QStringList regReturn = settings.allKeys();
I can get to all folders and subfolders of the path, but I need to scan through the folders, find those devices with a given PID and VID and a given value of the registrykey FriendlyName to identify my device(s).
How can this be done?

In order to obtain content of a particular sub folder, you can do the following (I used another example, because I don't know the structure of the BTHLE directory):
[..]
// Get the value of HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB\VID_045E&PID_00B4\5&1b6962&0&1\Class key
const QString top("HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Enum\\USB");
const QString device("VID_045E&PID_00B4");
QSettings settings(top, QSettings::NativeFormat);
settings.beginGroup(device); // Limit keys by this device only.
QStringList regReturn = settings.allKeys();
QString vStr = regReturn[1]; // 5&1b6962&0&1/Class
QString v = settings.value(regReturn[1]).toString(); // returns "USB"

Related

How to enter input separated by semi colon by using setInputMask()

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);

QSettings setvalue method writes QVariant datatype to windows registry

I try to update FTDI settings from windows registry. I can read and modify the ConfigData values from registry and change some values with converting it to QByteArray.
QSettings settings("HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Enum\\FTDIBUS\\VID_0403+PID_6015+" + port.serialNumber() + "\\0000\\Device Parameters" , QSettings::NativeFormat);
QStringList regReturn = settings.allKeys();
QVariant ccc = settings.value(regReturn.at(0));
QString kkkk = ccc.toString();
QByteArray b((const char*) (kkkk.utf16()), kkkk.size() * 2);
b[2] = 0x00;
b[3] = 0x00;
kkkk = QString::fromUtf16((ushort *)(b.data()),kkkk.size());
settings.setValue("LatencyTimer", 1);
settings.setValue("try", QVariant::fromValue(kkkk));
After execution, I have controled the registry value I saw that QVariant encapsulate the data with type like picture below. How can I prevent from this?
Raw Data from registery
Written data
It's not supported currently (and actually for long time) by Qt. You need to use native WinAPI methods to do this.

Config file gets overwritten in qt

I am writing data to a config file using the following code.
QSettings settings("/root/configFile.ini",QSettings::IniFormat);
QString userName = lineEditUsername.text();
QString password = lineEditPassword.text();
QList<QString> listUsername;
QList<QString> listPassword;
settings.beginWriteArray("UserData");
for(i=0;i<listUsername.size();i++)
{
Qstring user = listUsername.at(i);
Qstring pass = listPassword.at(i);
settings.setArryIndex(i);
settings.setValue("Username",user);
settings.setValue("Password",pass);
}
settings.endArray();
}
Now when I run the code first time and give 4 or 5 values they are formed in proper order in the file. However if I run the application for second time the values start overwriting from first position. Can some one suggest me some solution for this?
Instead of creating and maintaining arrays and indexes, I would propose to create user credentials map and store it in the settings file as follows:
QSettings settings("/root/configFile.ini", QSettings::IniFormat);
QString userName = lineEditUsername.text();
QString password = lineEditPassword.text();
QList<QString> listUsername;
QList<QString> listPassword;
//settings.beginWriteArray("UserData");
QVariantMap userDataMapping;
for(int i = 0; i < listUsername.size() ; i++)
{
QString user = listUsername.at(i);
QString pass = listPassword.at(i);
userDataMapping[user] = pass;
//settings.setArryIndex(i);
//settings.setValue("Username",user);
//settings.setValue("Password",pass);
}
// Store the mapping.
settings.setValue("UserData", userDataMapping);
//settings.endArray();
// ...
This will store your data in ini file in the following format:
UserData=#Variant(\0\0\0\b\0\0\0\x1\0\0\0\x6\0\x64\0\x64\0\x64\0\0\0\n\0\0\0\x6\0\x62\0\x62\0\x62)
When you read settings, do something like this:
[..]
QVariant v = settings.value("UserData");
QVariantMap map = v.value<QVariantMap>();
QMapIterator<QString, QVariant> i(map);
while (i.hasNext()) {
i.next();
QString user = i.key();
QString pass = i.value().toString();
}
You need to retrieve the amount of existing entries before adding a new one. Something like this:
int size = settings.beginReadArray( "UserData" );
settings.endArray();
settings.beginWriteArray( "UserData" );
settings.setArrayIndex( size ); // Note: Maybe 'size - 1', not sure
// ...
settings.endArray();
setArrayIndex( size ) will move the array index to the end and will thus no longer override an existing entry

Qt translation file (qm) info

I have some number of qm files for my app. (pr_en.qm, pr_ru.qm). I can load they by
translator.load(fileName, '.');
qApp->installTranslator(translator);
I want build dynamic menu (English, Russian) to select language. But, how can I extract such constants (English, Russian) from qm file instead of it's names (pr_en.qm, pr_ru.qm). Thanks.
I would suggest two ways of doing it:
First would be declaring special translator field like:
tr("__LANGNAME__") that would be in every translation file filled with proper language name (even native). Then you could list all available translations, load them one by one and use QTranslator::translate(const char * context, const char * sourceText, const char * disambiguation = 0) method.
Example:
QStringList availableLanguages;
QDirIterator qmIt(pathToQm, QStringList() << "*.qm", QDir::Files);
while(qmIt.hasNext())
{
qmIt.next();
QFileInfo finfo = qmIt.fileInfo();
QTranslator translator;
translator.load(finfo.baseName(), pathToQm);
availableLanguages << translator.translate("__LANGNAME__");
}
qDebug() << availableLanguages;
My second aproach would be with QLocale and QLocale::Language. I would create QLocale object for each base name of file in qm dir, and then use QLocale::Language enum to get language name with QLocale::languageToString method.

String creation of file pathin char format with Qt (VC++)

I want to create a GUI application using Qt. For that I need to get the filename using:
QString fileName=getOpenFileName(.....)
I am using Windows and want to have the filename path in C:\a\b\c format and pass it to a function accepting char variables. How can I implement this?
According to the Qt FAQ
QString path = QFileDialog::getOpenFileName(...);
QByteArray byteArray = path.toLocal8Bit();
const char *charPath = byteArray.data();

Resources