
1.概要
QTimer::singleShot有几种重载,并分别举例
2.内容
QTimer::singleShot 是 Qt 框架中用于创建单次定时器的静态方法。它有多种重载,可以方便地在一定时间后执行某个函数或代码块。以下是 QTimer::singleShot 的几种常见重载及其示例:
-
毫秒延迟和接收者对象,槽函数指针
QTimer::singleShot(int msec, QObject *receiver, const char *member);示例:
class MyClass : public QObject { Q_OBJECT public slots: void mySlot() { qDebug() << "Slot executed!"; } }; MyClass obj; QTimer::singleShot(1000, &obj, SLOT(mySlot())); -
毫秒延迟和函数对象(C++11 lambda 表达式或 std::function)
QTimer::singleShot(int msec, const QObject *context, std::function<void()> func);示例:
QTimer::singleShot(1000, nullptr, []() { qDebug() << "Lambda executed!"; }); -
毫秒延迟和函数指针
QTimer::singleShot(int msec, void (*func)());示例:
void myFunction() { qDebug() << "Function executed!"; } QTimer::singleShot(1000, myFunction); -
毫秒延迟和接收者对象,成员函数指针
template <typename Func> static void singleShot(int msec, const QObject *context, Func func);示例:
class MyClass { public: void myMemberFunction() { qDebug() << "Member function executed!"; } }; MyClass obj; QTimer::singleShot(1000, &obj, &MyClass::myMemberFunction);
这些重载提供了多种方式来使用 QTimer::singleShot,你可以根据实际需求选择最合适的一种。每种重载方式都有其独特的用例,从传统的信号槽机制到现代的 C++11 lambda 表达式和 std::function,Qt 都提供了良好的支持。
3.关联链接
QTimer::singleShot有几种重载,并分别举例-优快云博客
QTimer::start 在启动但状态下调用会有什么后果-优快云博客
QTimer::stop在停止的状态下调用会有什么后果-优快云博客
1万+

被折叠的 条评论
为什么被折叠?



