Questions: I am currently using the QT 15.15.2
I am currently using the
<ApplicationServices/ApplicationServices.h>
library. Based on my understanding:- I can use
PasteboardSetPromiseKeeper
to set a promise keeper function for clipboard handling (which I believe is a callback function for rendering clipboard data). - I can use
PasteboardPutItemFlavor
to place empty data. - When the user presses Ctrl+V, it should trigger the
PromiseKeeper
event.
- I can use
However, I am currently facing an issue where the
PromiseKeeper
function is running on my main thread, causing my UI to freeze and certain control flows to hang.I have already created a subthread using
QThread
, and bothPasteboardCreate
andPasteboardSetPromiseKeeper
are running within the subthread.
Partial pseudocode
// in child thread context
OSStatus err = PasteboardCreate(kPasteboardClipboard, &m_pasteboard);
err = PasteboardSetPromiseKeeper(m_pasteboard, promiseKeeper, this);
OSStatus MacClipboard::promiseKeeper(PasteboardRef pasteboard, PasteboardItemID pasteboardItemID, CFStringRef uti, void *_macClipboard)
{
qDebug() << "promiseKeeper Thread id=" << QThread::currentThreadId();
// In this case, I need to access the data on the network in another thread.
macClipboard->_promisMutex.lock();
macClipboard->_createPromise(pasteboardItemID, formatName);
bool timeouted = !(macClipboard->_promisWaitCondition.wait(&macClipboard->_promisMutex, 5000));
macClipboard->_promisMutex.unlock();
const CFDataRef cfData = CFDataCreate(nullptr, (UInt8*)macClipboard->promiseData()->constData(), macClipboard->promiseData()->size());
resultStatus = PasteboardPutItemFlavor(pasteboard, pasteboardItemID, uti, cfData, kPasteboardFlavorNoFlags);
return noErr;
}
I have already tried nesting multiple layers of threads, but it had no effect. The
promiseKeeper
function still runs on the main thread.I suspect it may be related to the event loop of the macOS system. After referring to the QT source code, it seems that I need to create a "run loop context" in the context of the subthread. However, I'm not very clear on its usage, so I haven't tried it yet.
Did I misunderstand? I don't need to block promiseKeeper? Then how can I achieve the goal of retrieving data from the network and placing it into the clipboard when the user presses CTRL+V?