I am working with QSettings function. Once successfully set in the .h file variable
QSettings *settings;
inside the constructor (cpp. file) I set the variable in this way, to obtain a path like Draw/Input/Cells/Width
settings = new QSettings("MySoft", "Star Runner");
settings->beginGroup("Draw");
settings->beginGroup("Input");
settings->beginGroup("Cells");
settings->setValue("width", 80);
settings->endGroup();
settings->endGroup();
settings->endGroup();
The problem is that width value is properly set to 80 only if during the declaration of the organization name is set to "MySoft": if you assign any other value (e.g. "foobar"), doing a test via
qDebug() << settings->value("width", "").toString();
the width key as no value
You should also start and end the groups when reading the value. So you could either try
qDebug() << settings->value("Draw/Input/Cells/width", "").toString();
or
settings->beginGroup("Draw");
settings->beginGroup("Input");
settings->beginGroup("Cells");
qDebug() << settings->value("width", "").toString();
settings->endGroup();
settings->endGroup();
settings->endGroup();
Related
So I have the tutorial project Notepad. I am trying to use QSettings so that it saves the font,font size and other font related options in a file but it never loads them properly and when I check the config file, the values look quite weird.
void Notepad::saveSettings()
{
QSettings setting("MyTE","myte");
QFont font = this->font();
setting.beginGroup("MainWindow");
setting.setValue("text.font",font.toString());
setting.setValue("text.font.family",font.family());
setting.setValue("text.font.size",font.pointSize());
setting.setValue("text.font.bold",font.bold());
setting.setValue("text.font.italic",font.italic());
setting.endGroup();
qDebug() << "Saved";
}
void Notepad::loadSettings(){
QSettings setting ("MyTE","myte");
QFont font = setting.value("text.font",QString()).toString();
setting.beginGroup("MainWindow");
QString fontFamily = setting.value("text.font.family",QString()).toString();
int fontSize = setting.value("text.font.size",12).toInt();
setting.setValue("text.font.size",fontSize);
//bool fontIsBold = setting.value("text.font.bold",false).toBool();
//bool fontIsItalic = setting.value("text.font.italic",false).toBool();
setFont(font);
font.setPointSize(fontSize);
setting.endGroup();
qDebug() << "Loaded";
}
I uncommented the bold ones because they simply don't work.
I have 2 buttons that correspond to these 2 functions and I also call loadSettings() when the app first starts.
Here is the output in the config file, that never changes and just resets to these weird values.
[MainWindow]
text.font=",9,-1,5,50,0,0,0,0,0"
text.font.bold=false
text.font.family=
text.font.italic=false
text.font.size=9
I tried this in my project in mainwindow.cpp:
QString dir = ":/nodesDir/nodesDir/";
QFile baseFile(dir + "allNodeNames.txt");
qDebug() << baseFile.exists(); // true
qDebug() << baseFile.readAll(); // ""
but it is wrong, the content of the file is
plusOperator
Why does it say, that there would be nothing written in the file? Or What did I miss in my code?`
Thanks for answers!
In order to read the file you need to open it for it we use open () and we indicate the way we want it to open. We must also bear in mind that the files stored in the resources are read only, so they can not be modified.
QString dir = ":/nodesDir/nodesDir/";
QFile baseFile(dir + "allNodeNames.txt");
qDebug() << baseFile.exists(); // true
qDebug()<< baseFile.open(QFile::ReadOnly);
qDebug() << baseFile.readAll(); // ""
Output:
true
true
"plusOperator"
How can I pull the names of my spinBoxes? I tried looking at a lot of the documentation, however, I couldn't find anything that would show the names of each of the child spinBoxes. I've tried changing the result to a string. However, I just get a Hex or Long Int, of the address I’d imagine, returned instead.
QList<QSpinBox*> spinBoxes= findChildren<QSpinBox*>();
//create the QSignalMapper object
QSignalMapper* signalMapper= new QSignalMapper(this);
//loop through your spinboxes list
QSpinBox* spinBox;
foreach(spinBox, spinBoxes){
//setup mapping for each spin box
connect(spinBox, SIGNAL(valueChanged(int)), signalMapper, SLOT(map()));
signalMapper->setMapping(spinBox, spinBox);
}
//connect the unified mapped(QWidget*) signal to your spinboxWrite slot
connect(signalMapper, SIGNAL(mapped(QWidget*)), this, SLOT(spinboxWrite(QWidget*)));
.
.
.
void GuiTest::SpinBoxChanged(QWidget* wSp){
QSpinBox* sp= (QSpinBox*)wSp; //now sp is a pointer to the QSpinBox that emitted the valueChanged signal
int value = sp->value(); //and value is its value after the change
//do whatever you want to do with them here. . .
qDebug() << value << "SpinBoxChanged";
}
void GuiTest::spinboxWrite(QWidget* e){
SpinBoxChanged(e);
QString* value = (QString*)e;
qDebug() << e << value << " SpinBoxWrite";
}
Please note qDebug() << e as this is where I'm having trouble getting some information about the spinboxes
The name you are trying to retrieve is the objectName property, which every QObject and QObject-derived class has. Call objectName() to retrieve this value.
You can also use this with the QObject::findChild() function.
This should get what you want:
void GuiTest::spinboxWrite(QWidget* e){
SpinBoxChanged(e);
qDebug() << e->objectName() << " SpinBoxWrite";
And will output:
"norm_spinBox_10" SpinBoxWrite
Note
This line is dangerous:
QSpinBox* sp= (QSpinBox*)wSp;
Use qobject_cast instead of C-style casts.
There is no direct way to get the name of a variable as a string.
However, you can use a QMap<QSpinBox*, QString> to map each spin-box to its name.
In the constructor you have to assign these manually:
map[ui->spinBox] = "spinBox";
map[ui->spinBoxWithStrangeName] = "spinBoxWithStrangeName";
Then you can simply get the strings using:
QString name = map[ui->spinBox];
Just give them names in the designer file and then use that name to retrieve them in the C++ code.
QSpinBox* mySpinner = findChild<QSpinBox*>("myGivenName");
I want to display the contents of a QList just like how it is displayed in the console with qDebug()
For example:
QList<QNetworkCookie> cookies = mManager->cookieJar()->cookiesForUrl(mUrl);
qDebug() << "COOKIES for" << mUrl.host() << cookies;
Output:
QNetworkCookie("MSession=kr6i819jbvkorherbe76oh23c7; domain=website.com; path=/)"
Is there a function that I can use?
You can create a QDebug object that will store anything streamed into it, inside a string. Here is it:
QString str;
QDebug dStream(&str);
dStream << mUrl.host();
Now you can put str wherever you want. For example a QTextBrowser:
ui->textBrowser->insertPlainText(str);
This should work everywhere that qDebug() works. Because qDebug() itself returns a QDebug object according to this documentation.
I've come across a anomaly in Qt (v4.8.4) when re-assigning a new path to a pre-existing QDir object. Here is a reduced example demonstrating this:
QString path1("F:/"); //Path must exist...
QString path2("F:/Some/Valid/Path/For/You/"); //Path must exist...
//Set default...
QFileInfo fi1(path1);
QDir d(fi1.absoluteDir());
//CASE 1...
if(!d.setCurrent(path2)) {
qDebug() << QString("Cannot set path (%1)").arg(path2).toAscii().data();
return -1;
}
qDebug() << "CASE 1:";
qDebug() << QString("path2: %1").arg(path2).toAscii().data();
qDebug() << QString("d : %1").arg(d.absolutePath()).toAscii().data();
//END of CASE 1...
//CASE 2...
QFileInfo fi2(path2);
d = fi2.absoluteDir();
qDebug() << "CASE 2:";
qDebug() << QString("path2: %1").arg(path2).toAscii().data();
qDebug() << QString("d : %1").arg(d.absolutePath()).toAscii().data();
//END of CASE 2...
Even though the call to d.setCurrent(path2) returns true, the new path is not set in the QDir object. OTOH, assigning the new path 1st to a QFileInfo object, and then calling absoluteDir() on that object returns an updated QDir object.
You can then directly assign the returned object to a pre-existing QDir object (through the overridden assignment operator), and the path in the QDir object will be correctly updated.
Why does the CASE 1 not work?
QDir::setCurrent is a static function that sets the current path of the application. It doesn't modifiy any QDir instance.
You should use QDir::setPath to assign a new path (or assign the QString directly to QDir with the = operator, since the conversion is implicit).