Browse Source

UI: Add CreateQThread helper function

Allows creating a QThread via an std::function.  A backward-compatible
alternative to QThread::create for Qt versions older than 5.10 (when it
became available in Qt).
jp9000 6 years ago
parent
commit
74cda9a2cc
2 changed files with 23 additions and 0 deletions
  1. 20 0
      UI/qt-wrappers.cpp
  2. 3 0
      UI/qt-wrappers.hpp

+ 20 - 0
UI/qt-wrappers.cpp

@@ -203,3 +203,23 @@ void DeleteLayout(QLayout *layout)
 
 	delete layout;
 }
+
+class QuickThread : public QThread {
+public:
+	explicit inline QuickThread(std::function<void()> func_)
+		: func(func_)
+	{}
+
+private:
+	virtual void run() override
+	{
+		func();
+	}
+
+	std::function<void()> func;
+};
+
+QThread *CreateQThread(std::function<void()> func)
+{
+	return new QuickThread(func);
+}

+ 3 - 0
UI/qt-wrappers.hpp

@@ -23,6 +23,7 @@
 #include <QThread>
 #include <obs.hpp>
 
+#include <functional>
 #include <memory>
 #include <vector>
 
@@ -64,6 +65,8 @@ QDataStream &operator>>(QDataStream &in, OBSScene &scene);
 QDataStream &operator<<(QDataStream &out, const OBSSceneItem &si);
 QDataStream &operator>>(QDataStream &in, OBSSceneItem &si);
 
+QThread *CreateQThread(std::function<void()> func);
+
 class SignalBlocker {
 	QWidget *widget;
 	bool blocked;