QT 界面 1080P 分辨率适配 4K 分辨率:代码实现与步骤
在 QT 界面中,可以使用 QApplication 类的 setOverrideCursor 方法来设置分辨率适配。以下是一个示例代码:
#include <QApplication>
#include <QDesktopWidget>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
// 获取屏幕分辨率
QDesktopWidget desktop;
int screen_width = desktop.width();
int screen_height = desktop.height();
// 设置适配的分辨率
int target_width = 1920;
int target_height = 1080;
// 计算缩放比例
double scale_x = static_cast<double>(screen_width) / target_width;
double scale_y = static_cast<double>(screen_height) / target_height;
double scale_factor = qMin(scale_x, scale_y);
// 设置缩放比例
a.setAttribute(Qt::AA_EnableHighDpiScaling);
a.setAttribute(Qt::AA_UseHighDpiPixmaps);
a.setOverrideCursor(QCursor(Qt::BlankCursor));
a.setOverrideCursor(QCursor(Qt::ArrowCursor));
a.setOverrideCursor(QCursor(Qt::WaitCursor));
a.setOverrideCursor(QCursor(Qt::CrossCursor));
a.setOverrideCursor(QCursor(Qt::SizeVerCursor));
// 展示界面
MainWindow w;
w.resize(target_width * scale_factor, target_height * scale_factor);
w.show();
return a.exec();
}
在上述代码中,我们首先获取屏幕的实际分辨率,并设置需要适配的目标分辨率。然后,计算缩放比例,将其应用到 QApplication 对象上,以实现分辨率适配。最后,创建和展示主窗口对象。
请注意,这只是一个简单的示例代码,实际的适配过程可能需要根据具体需求进行调整。
原文地址: https://www.cveoy.top/t/topic/pWPW 著作权归作者所有。请勿转载和采集!