仅模板中的 If 语句 运行 第一个 if 语句 - QT
If statement in a template only run the first if-statement - QT
template <class T> void IOcalibrationWindow::setChildIntoIOcalibrationObject(int i, QString firstChildName, QString lastChildName, QString firstIocalibrationMethodName, QString lastIOcalibrationMethodName){
/* Get the index as string */
QString index = QString::number(i);
/* Create the UI field name */
QString childName = firstChildName + index + lastChildName;
/* Create the object name */
const char *IOcalibrationMethodName = (firstIocalibrationMethodName + index + lastIOcalibrationMethodName).toLatin1().data();
/* Get UI child */
T *uIchild = findChild<T*>(childName);
/* Get calibration object */
const QMetaObject *IOcalibrationMetaObject = iOcalibration.metaObject();
/* Insert into object */
if(std::strcmp(T::staticMetaObject.className(), "QDoubleSpinBox") != 0)
IOcalibrationMetaObject->invokeMethod(&iOcalibration, IOcalibrationMethodName, Qt::DirectConnection, Q_ARG(double, uIchild->value()));
else if(std::strcmp(T::staticMetaObject.className(), "QLineEdit") != 0)
IOcalibrationMetaObject->invokeMethod(&iOcalibration, IOcalibrationMethodName, Qt::DirectConnection, Q_ARG(QString, uIchild->text()));
}
这是 QT 中的一个 if 语句,我在其中访问对象 iOcalibration
中的特定方法。方法名叫做IOcalibrationMethodName
.
有时方法 IOcalibrationMethodName
有一个参数 double
或 QString
。
这完全取决于模板 T
,可以是 QDoubleSpinBox
或 QLineEdit
.
当我调用该函数时,您可以看到我使用了不同的模板。
setChildIntoIOcalibrationObject<QDoubleSpinBox>(i, "analogSingleInput", "MaxDoubleSpinBox", "setAnalogSingleInput", "Max");
setChildIntoIOcalibrationObject<QDoubleSpinBox>(i, "analogSingleInput", "MinDoubleSpinBox", "setAnalogSingleInput", "Min");
setChildIntoIOcalibrationObject<QLineEdit>(i, "analogSingleInput", "UnitLineEdit", "setAnalogSingleInput", "Unit");
问题:
我的代码问题是,当我使用模板 class QLineEdit
时,我在该代码行遇到错误。
IOcalibrationMetaObject->invokeMethod(&iOcalibration, IOcalibrationMethodName, Qt::DirectConnection, Q_ARG(double, uIchild->value()));
由于if-statement
,这应该是不可能的。
错误是:
iocalibrationwindow.cpp:102:133: error: no member named 'value' in 'QLineEdit'
qobjectdefs.h:95:51: note: expanded from macro 'Q_ARG'
iocalibrationwindow.cpp:80:9: note: in instantiation of function template
specialization 'IOcalibrationWindow::setChildIntoIOcalibrationObject<QLineEdit>'
requested here
这意味着,第一个 if-statement
在我使用模板 QLineEdit
时运行。
但是 if 语句检查模板是否为 QDoubleSpinBox
if(std::strcmp(T::staticMetaObject.className(), "QDoubleSpinBox") != 0)
问题:
这怎么可能 QLineEdit
直到等于 QDoubleSpinBox
?
我不完全确定您要做什么,但是根据给出的代码,您为什么不能简单地将 uIchild->*
调用拆分为几个函数重载?这将导致类似(未经测试)...
QGenericArgument fetch_data (QDoubleSpinBox *p)
{
return Q_ARG(double, p->value());
}
QGenericArgument fetch_data (QLineEdit *p)
{
return Q_ARG(QString, p->text());
}
template <class T> void IOcalibrationWindow::setChildIntoIOcalibrationObject(int i, QString firstChildName, QString lastChildName, QString firstIocalibrationMethodName, QString lastIOcalibrationMethodName){
/* Get the index as string */
QString index = QString::number(i);
/* Create the UI field name */
QString childName = firstChildName + index + lastChildName;
/* Create the object name */
const char *IOcalibrationMethodName = (firstIocalibrationMethodName + index + lastIOcalibrationMethodName).toLatin1().data();
/* Get UI child */
T *uIchild = findChild<T*>(childName);
/* Get calibration object */
const QMetaObject *IOcalibrationMetaObject = iOcalibration.metaObject();
/* Insert into object */
if(std::strcmp(T::staticMetaObject.className(), "QDoubleSpinBox") == 0)
IOcalibrationMetaObject->invokeMethod(&iOcalibration, IOcalibrationMethodName, Qt::DirectConnection, fetch_data(uIchild));
else if(std::strcmp(T::staticMetaObject.className(), "QLineEdit") == 0)
IOcalibrationMetaObject->invokeMethod(&iOcalibration, IOcalibrationMethodName, Qt::DirectConnection, fetch_data(uIchild));
}
[请注意,我已经改变了 if (strcmp(...
比较的含义:如果字符串相等,strcmp
returns 为零。]
template <class T> void IOcalibrationWindow::setChildIntoIOcalibrationObject(int i, QString firstChildName, QString lastChildName, QString firstIocalibrationMethodName, QString lastIOcalibrationMethodName){
/* Get the index as string */
QString index = QString::number(i);
/* Create the UI field name */
QString childName = firstChildName + index + lastChildName;
/* Create the object name */
const char *IOcalibrationMethodName = (firstIocalibrationMethodName + index + lastIOcalibrationMethodName).toLatin1().data();
/* Get UI child */
T *uIchild = findChild<T*>(childName);
/* Get calibration object */
const QMetaObject *IOcalibrationMetaObject = iOcalibration.metaObject();
/* Insert into object */
if(std::strcmp(T::staticMetaObject.className(), "QDoubleSpinBox") != 0)
IOcalibrationMetaObject->invokeMethod(&iOcalibration, IOcalibrationMethodName, Qt::DirectConnection, Q_ARG(double, uIchild->value()));
else if(std::strcmp(T::staticMetaObject.className(), "QLineEdit") != 0)
IOcalibrationMetaObject->invokeMethod(&iOcalibration, IOcalibrationMethodName, Qt::DirectConnection, Q_ARG(QString, uIchild->text()));
}
这是 QT 中的一个 if 语句,我在其中访问对象 iOcalibration
中的特定方法。方法名叫做IOcalibrationMethodName
.
有时方法 IOcalibrationMethodName
有一个参数 double
或 QString
。
这完全取决于模板 T
,可以是 QDoubleSpinBox
或 QLineEdit
.
当我调用该函数时,您可以看到我使用了不同的模板。
setChildIntoIOcalibrationObject<QDoubleSpinBox>(i, "analogSingleInput", "MaxDoubleSpinBox", "setAnalogSingleInput", "Max");
setChildIntoIOcalibrationObject<QDoubleSpinBox>(i, "analogSingleInput", "MinDoubleSpinBox", "setAnalogSingleInput", "Min");
setChildIntoIOcalibrationObject<QLineEdit>(i, "analogSingleInput", "UnitLineEdit", "setAnalogSingleInput", "Unit");
问题:
我的代码问题是,当我使用模板 class QLineEdit
时,我在该代码行遇到错误。
IOcalibrationMetaObject->invokeMethod(&iOcalibration, IOcalibrationMethodName, Qt::DirectConnection, Q_ARG(double, uIchild->value()));
由于if-statement
,这应该是不可能的。
错误是:
iocalibrationwindow.cpp:102:133: error: no member named 'value' in 'QLineEdit'
qobjectdefs.h:95:51: note: expanded from macro 'Q_ARG'
iocalibrationwindow.cpp:80:9: note: in instantiation of function template
specialization 'IOcalibrationWindow::setChildIntoIOcalibrationObject<QLineEdit>'
requested here
这意味着,第一个 if-statement
在我使用模板 QLineEdit
时运行。
但是 if 语句检查模板是否为 QDoubleSpinBox
if(std::strcmp(T::staticMetaObject.className(), "QDoubleSpinBox") != 0)
问题:
这怎么可能 QLineEdit
直到等于 QDoubleSpinBox
?
我不完全确定您要做什么,但是根据给出的代码,您为什么不能简单地将 uIchild->*
调用拆分为几个函数重载?这将导致类似(未经测试)...
QGenericArgument fetch_data (QDoubleSpinBox *p)
{
return Q_ARG(double, p->value());
}
QGenericArgument fetch_data (QLineEdit *p)
{
return Q_ARG(QString, p->text());
}
template <class T> void IOcalibrationWindow::setChildIntoIOcalibrationObject(int i, QString firstChildName, QString lastChildName, QString firstIocalibrationMethodName, QString lastIOcalibrationMethodName){
/* Get the index as string */
QString index = QString::number(i);
/* Create the UI field name */
QString childName = firstChildName + index + lastChildName;
/* Create the object name */
const char *IOcalibrationMethodName = (firstIocalibrationMethodName + index + lastIOcalibrationMethodName).toLatin1().data();
/* Get UI child */
T *uIchild = findChild<T*>(childName);
/* Get calibration object */
const QMetaObject *IOcalibrationMetaObject = iOcalibration.metaObject();
/* Insert into object */
if(std::strcmp(T::staticMetaObject.className(), "QDoubleSpinBox") == 0)
IOcalibrationMetaObject->invokeMethod(&iOcalibration, IOcalibrationMethodName, Qt::DirectConnection, fetch_data(uIchild));
else if(std::strcmp(T::staticMetaObject.className(), "QLineEdit") == 0)
IOcalibrationMetaObject->invokeMethod(&iOcalibration, IOcalibrationMethodName, Qt::DirectConnection, fetch_data(uIchild));
}
[请注意,我已经改变了 if (strcmp(...
比较的含义:如果字符串相等,strcmp
returns 为零。]