Qvariant无法区分QDATETIME和QSTRINT

QVariant is not able to distinguish between QDateTime and QString

本文关键字:QSTRINT QDATETIME 法区 Qvariant      更新时间:2023-10-16

我有一个我存储在 QVariant中的 QDateTime object,然后用 type()检查 QVariant

void MainWindow::Test()
{
    QDateTime myDate; // QDateTime;
    myDate.setDate(QDate::currentDate());
    QVariant myVariant(myDate);
    qDebug() << myVariant.canConvert(QMetaType::QDateTime); // return true 
    // here is the problem
    qDebug() << myVariant.canConvert(QMetaType::QString); // return true as well
}

canConvert仅表示转换是可能的,而不是变体包含特定类型。验证类型使用此方法:

qDebug() << (myVariant.type()==QVariant::DateTime);
qDebug() << (myVariant.type()==QVariant::String);