欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 房产 > 建筑 > Qt中QVariant的使用

Qt中QVariant的使用

2025/4/29 23:40:46 来源:https://blog.csdn.net/weixin_40933496/article/details/145357383  浏览:    关键词:Qt中QVariant的使用

1.使用QVariant实现不同类型数据的相加
方法:通过type函数返回数值的类型,然后通过setValue来构造一个QVariant类型的返回值。
函数:

QVariant mainPage::dataPlus(QVariant a, QVariant b)
{QVariant ret;if ((a.type() == QVariant::Int) && (b.type() == QVariant::Int)){ret.setValue(a.toInt() + b.toInt());}else if ((a.type() == QVariant::String) && (b.type() == QVariant::String)){ret.setValue(a.toString() + b.toString());}return ret;
}

调用:

qDebug() << dataPlus(10, 20).toInt();
qDebug() << dataPlus("hello ", "world!!!").toString();

2.获取系统当前时间

	//需求:获取当前的日期QDate currentDateVal = QDate::currentDate();qDebug() << currentDateVal.toString("yyyy-MM-dd");//获取当前的时间QTime currentTimeVal = QTime::currentTime();qDebug() << currentTimeVal.toString("H-mm-ss");//获取当前日期和时间QDateTime currentDateAndTime = QDateTime::currentDateTime();qDebug() << currentDateAndTime.toString("yyyy-MM-dd H-mm-ss");

结果:
在这里插入图片描述
3.信号槽的使用

    //关联按钮和lineedit和labelconnect(ui_.m_btn, &QPushButton::clicked, this, [=]() {QString imagePath = ":/img/fengjing.jpg";ui_.m_label->setPixmap(QPixmap(imagePath));ui_.m_lineEdit->setText(imagePath);qDebug() << "button slot is running..." << Qt::endl;});

结果:
在这里插入图片描述
4.定时器QTimer

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词