How to change QVector to QString
-
My paint project need save and read DashPattern.
Basic use:
QVector<qreal> pattern { 5,5 };
pen.setDashPattern(pattern);QVector to QString needed:
settings.setValue("DashPattern", pattern);QString to QVector needed:
QVector<qreal> pattern = settings.value(("DashPattern", "{ 5,5 }"; -
My paint project need save and read DashPattern.
Basic use:
QVector<qreal> pattern { 5,5 };
pen.setDashPattern(pattern);QVector to QString needed:
settings.setValue("DashPattern", pattern);QString to QVector needed:
QVector<qreal> pattern = settings.value(("DashPattern", "{ 5,5 }";@sonichy "settings" are QSettings? You don't need to change this to QString, you need a QVariant.
constexpr auto PatternKey = QLatin1String("DashPattern"); settings.setValue(PatternKey, QVariant::fromValue(pattern)); // ... auto pattern = settings.value(PatternKey).value<QVector<qreal>>(); -
@sonichy "settings" are QSettings? You don't need to change this to QString, you need a QVariant.
constexpr auto PatternKey = QLatin1String("DashPattern"); settings.setValue(PatternKey, QVariant::fromValue(pattern)); // ... auto pattern = settings.value(PatternKey).value<QVector<qreal>>(); -
@sierdzio I need set the value by QLineEdit:
settings.setValue("DashPattern", lineEdit->text());Code you wrote now is correct but seems unrelated to the original question, I am confused.
-
@sierdzio I need set the value by QLineEdit:
settings.setValue("DashPattern", lineEdit->text());@sonichy said in How to change QVector to QString:
I need set the value by QLineEdit:
Why?
If you are using Qt6 then:
QVector<qreal> pattern { 5,5 }; constexpr auto PatternKey = QLatin1String("DashPattern"); settings.setValue(PatternKey, QVariant::fromValue(pattern));will save something to QSettings that can be restored using:
auto pattern = settings.value(PatternKey).value<QVector<qreal>>();If the QSetting backing store is an INI file then it might look like this:
$ cat test.ini [General] DashPattern=@Variant(\0\0\0\x7f\0\0\0\xeQList<double>\0\0\0\0\x2@\x14\0\0\0\0\0\0@\x14\0\0\0\0\0\0)but you really do not need to worry about that.
-
@sonichy said in How to change QVector to QString:
I need set the value by QLineEdit:
Why?
If you are using Qt6 then:
QVector<qreal> pattern { 5,5 }; constexpr auto PatternKey = QLatin1String("DashPattern"); settings.setValue(PatternKey, QVariant::fromValue(pattern));will save something to QSettings that can be restored using:
auto pattern = settings.value(PatternKey).value<QVector<qreal>>();If the QSetting backing store is an INI file then it might look like this:
$ cat test.ini [General] DashPattern=@Variant(\0\0\0\x7f\0\0\0\xeQList<double>\0\0\0\0\x2@\x14\0\0\0\0\0\0@\x14\0\0\0\0\0\0)but you really do not need to worry about that.
-
@ChrisW67 How to change the dash value ? So I need QLineEdit and QVector QString exchange.
@sonichy said in How to change QVector to QString:
how to change the dash value ? So I need QLineEdit and QVector QString exchange.
You can create a float vector like this:
QString text="1.2, 2.4, 5.75, 10.25"; QStringList list=text.split(','); QVector<float> fVec; for(QString& s : list) fVec<<s.toFloat(); qDebug()<<fVec; // --> QVector(1.2, 2.4, 5.75, 10.25) -
@sonichy said in How to change QVector to QString:
how to change the dash value ? So I need QLineEdit and QVector QString exchange.
You can create a float vector like this:
QString text="1.2, 2.4, 5.75, 10.25"; QStringList list=text.split(','); QVector<float> fVec; for(QString& s : list) fVec<<s.toFloat(); qDebug()<<fVec; // --> QVector(1.2, 2.4, 5.75, 10.25) -
@mpergand I accept your code
https://gitlink.org.cn/sonichy/HTYPaint/commits/a3275f0062@sonichy
What is your actual question/problem? If you have a list of numbers (integers or floats) and you want to save it to settings as a string and restore it as a list of numbers, and for some reason you don't want to do it via aQVariantlike @ChrisW67 suggested, (is that what you want?) you need to:- To save, you need to iterate the list calling
QString::number()on each one to build up a comma (or other character)-separated string, then pass that to settings. - To restore, you need to call
QString.split()to split the string into items and callQString::toInt/Float()on each one to convert back to numbers.
Of course, if you have the numbers stored as a separated string in your text edit or elsewhere and you only want to deal with that then you don't need to do joining/splitting/converting from/to numbers.
UPDATE
@sonichy said in How to change QVector to QString:
@mpergand I accept your code
You have gone back and changed your post to say "I accept your code" now, maybe you mean you have no problem, I don't know.
- To save, you need to iterate the list calling