本文整理汇总了C++中QVariant::convert方法的典型用法代码示例。如果您正苦于以下问题:C++ QVariant::convert方法的具体用法?C++ QVariant::convert怎么用?C++ QVariant::convert使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类QVariant
的用法示例。
在下文中一共展示了QVariant::convert方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: convertCompatible
bool QgsField::convertCompatible( QVariant& v ) const
{
if ( v.isNull() )
{
v.convert( d->type );
return true;
}
if ( d->type == QVariant::Int && v.toInt() != v.toLongLong() )
{
v = QVariant( d->type );
return false;
}
//String representations of doubles in QVariant will return false to convert( QVariant::Int )
//work around this by first converting to double, and then checking whether the double is convertible to int
if ( d->type == QVariant::Int && v.canConvert( QVariant::Double ) )
{
bool ok = false;
double dbl = v.toDouble( &ok );
if ( !ok )
{
//couldn't convert to number
v = QVariant( d->type );
return false;
}
double round = qgsRound( dbl );
if ( round > INT_MAX || round < -INT_MAX )
{
//double too large to fit in int
v = QVariant( d->type );
return false;
}
v = QVariant( qRound( dbl ) );
return true;
}
if ( !v.convert( d->type ) )
{
v = QVariant( d->type );
return false;
}
if ( d->type == QVariant::Double && d->precision > 0 )
{
double s = qPow( 10, d->precision );
double d = v.toDouble() * s;
v = QVariant(( d < 0 ? ceil( d - 0.5 ) : floor( d + 0.5 ) ) / s );
return true;
}
if ( d->type == QVariant::String && d->length > 0 && v.toString().length() > d->length )
{
v = v.toString().left( d->length );
return false;
}
return true;
}
示例2: setMetaData
void CameraBinMetaData::setMetaData(const QString &key, const QVariant &value)
{
QVariant correctedValue = value;
if (value.isValid()) {
if (key == QMediaMetaData::Orientation) {
correctedValue = toGStreamerOrientation(value);
} else if (key == QMediaMetaData::GPSSpeed) {
// kilometers per hour to meters per second.
correctedValue = (value.toDouble() * 1000) / 3600;
}
}
Q_FOREACH (const QGStreamerMetaDataKey &metadataKey, *qt_gstreamerMetaDataKeys()) {
if (metadataKey.qtName == key) {
const char *name = metadataKey.gstName;
if (correctedValue.isValid()) {
correctedValue.convert(metadataKey.type);
m_values.insert(QByteArray::fromRawData(name, qstrlen(name)), correctedValue);
} else {
m_values.remove(QByteArray::fromRawData(name, qstrlen(name)));
}
emit QMetaDataWriterControl::metaDataChanged();
emit metaDataChanged(m_values);
return;
}
}
}
示例3: setProperty
void QDBusViewer::setProperty(const BusSignature &sig)
{
QDBusInterface iface(sig.mService, sig.mPath, sig.mInterface, c);
QMetaProperty prop = iface.metaObject()->property(iface.metaObject()->indexOfProperty(sig.mName.toLatin1()));
bool ok;
QString input = QInputDialog::getText(this, tr("Arguments"),
tr("Please enter the value of the property %1 (type %2)").arg(
sig.mName, QString::fromLatin1(prop.typeName())),
QLineEdit::Normal, QString(), &ok);
if (!ok)
return;
QVariant value = input;
if (!value.convert(prop.type())) {
QMessageBox::warning(this, tr("Unable to marshall"),
tr("Value conversion failed, unable to set property"));
return;
}
QDBusMessage message = QDBusMessage::createMethodCall(sig.mService, sig.mPath, QLatin1String("org.freedesktop.DBus.Properties"), QLatin1String("Set"));
QList<QVariant> arguments;
arguments << sig.mInterface << sig.mName << qVariantFromValue(QDBusVariant(value));
message.setArguments(arguments);
c.callWithCallback(message, this, SLOT(dumpMessage(QDBusMessage)));
}
示例4: retypeVariant
QVariant Utils::retypeVariant(const QVariant &val, int meta_type_id)
{
if(meta_type_id == QVariant::Invalid) {
//qfWarning() << "Cannot convert" << val << "to QVariant::Invalid type!";
// retype whatever to invalid variant
return QVariant();
}
if(val.userType() == meta_type_id)
return val;
if(!val.isValid())
return QVariant(meta_type_id);
if(val.isNull())
return QVariant(meta_type_id);
if(val.canConvert(meta_type_id)) {
QVariant ret = val;
ret.convert(meta_type_id);
return ret;
}
//if(meta_type_id >= QVariant::UserType) {
// if(val.userType() >= QVariant::UserType) {
// if()
// }
//}
qfWarning() << "Don't know, how to convert variant type" << val.typeName() << "to:" << meta_type_id << QMetaType::typeName(meta_type_id);
return val;
}
示例5: getParameterValue
/** This function will try to find value of parameter corresponding to its type.
* Sometimes, a parameter value is a reference to another one. This functions will recursively
* seek through references until it finds one corresponding to the type specified.
*/
QVariant MOomc::getParameterValue(QString parentClass, QString parameterName,VariableType type, QVariant defaultValue)
{
QString commandRes= evalCommand("getParameterValue(" + parentClass +","+ parameterName+")");
if(commandRes=="Error" || commandRes.isEmpty())
return defaultValue;
QVariant::Type variantType;
switch(type)
{
case OMREAL:
variantType = QVariant::Double;
break;
case OMBOOLEAN :
variantType = QVariant::Bool;
break;
case OMINTEGER :
variantType = QVariant::Int;
break;
case OMSTRING :
variantType = QVariant::String;
break;
default:
variantType = QVariant::String;
break;
}
int i=0; // to avoid infinite loop (if for whatever reason, incorrect case is catched by error boolean)
QVariant result = commandRes;
bool error = false;
while(!result.convert(variantType) && (i<100) &&!error)
{
parentClass = commandRes.section(".",0,-2);
parameterName= commandRes.section(".",-1,-1);
commandRes = evalCommand("getParameterValue(" + parentClass +","+ parameterName+")");
error = (commandRes=="Error" || commandRes.isEmpty());
result = commandRes;
i++;
}
if(result.convert(variantType) && result.convert(variantType))
return result;
else
return defaultValue;
}
示例6: set
void QJnextMainLoop::set(QObject *object, QVariantList& args,
QByteArray* retval) {
QByteArray property = args.takeFirst().toByteArray();
const QMetaObject * meta = object->metaObject();
int propertyIndex = meta->indexOfProperty(property);
if (propertyIndex < 0) {
retval->append("No such property " + property);
return;
}
QMetaProperty metaprop = meta->property(propertyIndex);
if (!metaprop.isWritable()) {
retval->append("Property " + property + " is not writable");
return;
}
QVariant vValue = args.takeFirst();
/* handle enum inputs as text */
if (metaprop.isEnumType()) {
int id;
const QMetaEnum &enumerator = metaprop.enumerator();
QByteArray keys;
for (int i = 0; i < enumerator.keyCount(); ++i)
keys += enumerator.key(i) + QByteArray(" ");
#ifdef DEBUG_QJnextMainLoop
qDebug() << "[QJnextMainLoop]\tEnumerator" << enumerator.isFlag() << enumerator.scope() << enumerator.name() <<
keys;
#endif
if (enumerator.isFlag())
id = enumerator.keyToValue(vValue.toByteArray().constData());
else
id = enumerator.keysToValue(vValue.toByteArray().constData());
if (id != -1)
vValue = QVariant(id);
}
#ifdef DEBUG_QJnextMainLoop
qDebug() << "[QJnextMainLoop]\tSET" << meta->className() << property << vValue
<< vValue.canConvert(metaprop.type());
#endif
if (!vValue.convert(metaprop.type())) {
retval->append(
"Unable to convert \"" + vValue.toByteArray() + "\" to "
+ metaprop.typeName());
return;
}
if (!metaprop.write(object, vValue))
retval->append(
QByteArray("Unable to set property ") + meta->className() + "."
+ property + " to " + vValue.toByteArray());
else
*retval = QByteArray();
}
示例7:
// Convert a QVariant to a Python object.
static PyObject *convert(const Chimera *ct, const QVariant &value)
{
QVariant converted = value;
if (!converted.convert(static_cast<QVariant::Type>(ct->metatype())))
converted = value;
return ct->toPyObject(converted);
}
示例8: write
QString write(const QVariant &variant)
{
if (!variant.isValid()) {
qWarning() << "Trying to serialize invalid QVariant";
return QString();
}
QString value;
switch (variant.type()) {
case QMetaType::QPoint:
{
QPoint p = variant.toPoint();
value = QString("%1,%2").arg(QString::number(p.x()), QString::number(p.y()));
break;
}
case QMetaType::QPointF:
{
QPointF p = variant.toPointF();
value = QString("%1,%2").arg(QString::number(p.x(), 'f'), QString::number(p.y(), 'f'));
break;
}
case QMetaType::QSize:
{
QSize s = variant.toSize();
value = QString("%1x%2").arg(QString::number(s.width()), QString::number(s.height()));
break;
}
case QMetaType::QSizeF:
{
QSizeF s = variant.toSizeF();
value = QString("%1x%2").arg(QString::number(s.width(), 'f'), QString::number(s.height(), 'f'));
break;
}
case QMetaType::QRect:
{
QRect r = variant.toRect();
value = QString("%1,%2,%3x%4").arg(QString::number(r.x()), QString::number(r.y()),
QString::number(r.width()), QString::number(r.height()));
break;
}
case QMetaType::QRectF:
{
QRectF r = variant.toRectF();
value = QString("%1,%2,%3x%4").arg(QString::number(r.x(), 'f'), QString::number(r.y(), 'f'),
QString::number(r.width(), 'f'), QString::number(r.height(), 'f'));
break;
}
default:
QVariant strVariant = variant;
strVariant.convert(QVariant::String);
if (!strVariant.isValid())
qWarning() << Q_FUNC_INFO << "cannot serialize type " << QMetaType::typeName(variant.type());
value = strVariant.toString();
}
return value;
}
示例9: typeCheckedFromSettings
T MetaParams::typeCheckedFromSettings(const QString &name, const T &defaultValue) {
QVariant cfgVariable = qsSettings->value(name, defaultValue);
if (!cfgVariable.convert(QVariant(defaultValue).type())) { // Bit convoluted as canConvert<T>() only does a static check without considering whether say a string like "blub" is actually a valid double (which convert does).
qCritical() << "Configuration variable" << name << "is of invalid format. Set to default value of" << defaultValue << ".";
return defaultValue;
}
return cfgVariable.value<T>();
}
示例10: NormalizeVariants
void Expression::NormalizeVariants( QVariant & l, QVariant & r ) const
{
if ( l.isValid() && r.isValid() ) {
if ( l.type() != r.type() ) {
if ( l.type() == QVariant::String && l.canConvert( r.type() ) )
l.convert( r.type() );
else if ( r.type() == QVariant::String && r.canConvert( l.type() ) )
r.convert( l.type() );
else {
QVariant::Type t = l.type() > r.type() ? l.type() : r.type();
if ( r.canConvert( t ) && l.canConvert( t ) ) {
l.convert( t );
r.convert( t );
}
}
}
}
}
示例11:
QVariant Nuria::RestfulHttpNode::convertArgumentToVariant (const QString &argumentData, int targetType) {
QVariant variant = argumentData;
if (targetType == QMetaType::QVariant) {
return variant;
}
//
variant.convert (targetType);
return variant;
}
示例12: invokeMethod
QJsonValue QMetaObjectPublisher::invokeMethod(QObject *const object, const int methodIndex,
const QJsonArray &args)
{
const QMetaMethod &method = object->metaObject()->method(methodIndex);
if (method.name() == QByteArrayLiteral("deleteLater")) {
// invoke `deleteLater` on wrapped QObject indirectly
deleteWrappedObject(object);
return QJsonValue();
} else if (!method.isValid()) {
qWarning() << "Cannot invoke unknown method of index" << methodIndex << "on object" << object << '.';
return QJsonValue();
} else if (method.access() != QMetaMethod::Public) {
qWarning() << "Cannot invoke non-public method" << method.name() << "on object" << object << '.';
return QJsonValue();
} else if (method.methodType() != QMetaMethod::Method && method.methodType() != QMetaMethod::Slot) {
qWarning() << "Cannot invoke non-public method" << method.name() << "on object" << object << '.';
return QJsonValue();
} else if (args.size() > 10) {
qWarning() << "Cannot invoke method" << method.name() << "on object" << object << "with more than 10 arguments, as that is not supported by QMetaMethod::invoke.";
return QJsonValue();
} else if (args.size() > method.parameterCount()) {
qWarning() << "Ignoring additional arguments while invoking method" << method.name() << "on object" << object << ':'
<< args.size() << "arguments given, but method only takes" << method.parameterCount() << '.';
}
// construct converter objects of QVariant to QGenericArgument
VariantArgument arguments[10];
for (int i = 0; i < qMin(args.size(), method.parameterCount()); ++i) {
QVariant arg = args.at(i).toVariant();
if (method.parameterType(i) != QMetaType::QVariant && !arg.convert(method.parameterType(i))) {
qWarning() << "Could not convert argument" << args.at(i) << "to target type" << method.parameterTypes().at(i) << '.';
}
arguments[i].value = arg;
}
// construct QGenericReturnArgument
QVariant returnValue;
if (method.returnType() != qMetaTypeId<QVariant>() && method.returnType() != qMetaTypeId<void>()) {
// Only init variant with return type if its not a variant itself, which would
// lead to nested variants which is not what we want.
// Also, skip void-return types for obvious reasons (and to prevent a runtime warning inside Qt).
returnValue = QVariant(method.returnType(), 0);
}
QGenericReturnArgument returnArgument(method.typeName(), returnValue.data());
// now we can call the method
method.invoke(object, returnArgument,
arguments[0], arguments[1], arguments[2], arguments[3], arguments[4],
arguments[5], arguments[6], arguments[7], arguments[8], arguments[9]);
return wrapResult(returnValue);
}
示例13: jsNull
KJS::JSValue *callCast(KJS::ExecState *exec, KJS::JSObject *self, const KJS::List &args)
{
KJSEmbed::VariantBinding *imp = KJSEmbed::extractBindingImp<KJSEmbed::VariantBinding>(exec, self);
if (imp) {
QVariant val = imp->variant();
QVariant::Type type = QVariant::nameToType(args[0]->toString(exec).ascii());
KJS::JSValue *returnValue = KJS::jsBoolean(val.convert(type));
imp->setValue(val);
return returnValue;
}
return KJS::jsNull();
}
示例14: convertCompatible
bool QgsField::convertCompatible( QVariant& v ) const
{
if ( v.isNull() )
{
v.convert( mType );
return true;
}
if ( !v.convert( mType ) )
{
return false;
}
if ( mType == QVariant::Double && mPrecision > 0 )
{
v = qRound64( v.toDouble() * qPow( 10, mPrecision ) ) / qPow( 10, mPrecision );
return true;
}
return true;
}
示例15: convert
bool convert( const QVariant & from, QVariant & to )
{
const QVariant::Type toType = (QVariant::Type) to.userType();
if( from.canConvert( toType ) )
{
to.setValue( from );
return to.convert( toType );
}
return Data::convert(
from.userType(), from.constData(),
toType, const_cast<void*>(to.constData()) );
}