gpt4 book ai didi

c++ - 查看QCursor::setPos是否有效果

转载 作者:行者123 更新时间:2023-12-01 19:25:15 25 4
gpt4 key购买 nike

QCursor::setPos() 在某些条件下在某些平台上不起作用。例如。在 MacOS 上,有辅助功能设置,允许应用程序允许或禁止控制光标。有没有办法查明QCursor::setPos()当前是否有效果?

最简单的解决方案,使用QCursor::setPos稍微改变光标并检查它是否改变(使用QCursor::pos)不起作用,至少不起作用在 MacOS 上。

注意:至少需要适用于 Linux、Windows、Mac 的通用解决方案。

最佳答案

我不知道跨平台解决方案,但这并不意味着不存在。但对于 macOS,您可以使用特定于平台的代码。

ma​​cOS 解决方案

macOS的ApplicationServices框架中有AXIsProcessTrusted函数,它

Returns whether the current process is a trusted accessibility client.

参见https://developer.apple.com/documentation/applicationservices/1460720-axisprocesstrusted?language=objc

除了调用上述函数外,还需要:

  • 使用 Q_OS_MAC预处理器有条件包含 macOS 特定代码
  • 链接到 ApplicationServices 框架
  • 使用包含 #include <ApplicationServices/ApplicationServices.h>

示例

一个完整的独立示例可能如下所示:

#include <iostream>
#include <QApplication>
#include <QMainWindow>
#ifdef Q_OS_MAC
#include <ApplicationServices/ApplicationServices.h>
#endif

class MainWindow : public QMainWindow {

public:
explicit MainWindow(QWidget *parent = nullptr) : QMainWindow(parent) {
this->setWindowTitle("Qt Cursor Test");
this->resize(320, 240);
}
};

int main(int argc, char *argv[]) {
QApplication appl(argc, argv);
MainWindow mainw_window;
mainw_window.show();

QCursor cursor;

#ifdef Q_OS_MAC
if(AXIsProcessTrusted()) {
std::cout << "trusted accessibility client" << std::endl;
cursor.setPos(10,10);
} else {
std::cout << "not a trusted trusted accessibility client, so give the user a hint or do whatever is appropriate" << std::endl;
}
#else
cursor.setPos(10,10);
#endif

return QApplication::exec();
}

建筑

您需要链接到 ApplicationServices框架。如果您使用 CMake 进行构建,它可能如下所示:

 cmake_minimum_required(VERSION 3.15)
project(CursorTest)

set(CMAKE_CXX_STANDARD 14)
set(Qt5_DIR "/usr/local/Cellar/qt/5.14.1/lib/cmake/Qt5/")

find_package(Qt5 COMPONENTS Widgets REQUIRED)

add_executable(CursorTest main.cpp)

target_link_libraries(CursorTest PRIVATE Qt5::Widgets "-framework ApplicationServices")

系统对话框

也许您想向 MacOS 用户显示系统对话框,这说明应用程序希望获得可访问性信任并提供打开系统默认设置,如下所示:

macOS system dialog

然后你会使用Boolean AXIsProcessTrustedWithOptions(CFDictionaryRef options) ,其中

Returns whether the current process is a trusted accessibility client.

options

A dictionary of options, or NULL to specify no options. The following options are available: KEY: kAXTrustedCheckOptionPrompt VALUE: ACFBooleanRef indicating whether the user will be informed if the current process is untrusted. This could be used, for example, on application startup to always warn a user if accessibility is not enabled for the current process. Prompting occurs asynchronously and does not affect the return value.

参见https://developer.apple.com/documentation/applicationservices/1459186-axisprocesstrustedwithoptions?language=objc

只需稍微修改一下上面的例子:

...
#ifdef Q_OS_MAC
CFStringRef keys[] = { kAXTrustedCheckOptionPrompt };
CFTypeRef values[] = { kCFBooleanTrue };
CFDictionaryRef options = CFDictionaryCreate(NULL,
(const void **)&keys,
(const void **)&values,
sizeof(keys) / sizeof(keys[0]),
&kCFTypeDictionaryKeyCallBacks,
&kCFTypeDictionaryValueCallBacks);
if(AXIsProcessTrustedWithOptions(options)) {
std::cout << "trusted accessibility client" << std::endl;
cursor.setPos(10,10);
} else {
std::cout << "not a trusted trusted accessibility client, so give the user a hint or do whatever is appropriate" << std::endl;
}
CFRelease(options);
#else
...

关于c++ - 查看QCursor::setPos是否有效果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60064492/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com