c++ - Qt 中的 QDesktopWidget availableGeometry() 无法正常工作,需要垂直滚动才能查看完整图像

标签 c++ image qt scroll screen

我有一张背景图片,想在 Qt 5.7 的 QGraphicsView 中显示。我希望窗口的大小能够完全覆盖我的整个屏幕,而无需滚动。因此,我想确保它为 Windows 中的任务栏和 Ubuntu 中的顶部和左侧面板留出空间(我使用的是 Ubuntu 14.04)。

我从 Qt 文档中发现 QDesktopWidget::availableGeometry()是用于此目的的函数。这已在 StackOverflow 中得到重申 herehere .

但是,当我尝试使用它时,我观察到图像的一小部分被截断了,如果我将滚动条策略设置为 Qt::ScrollBarAlwaysOff,如果没有,我需要稍微垂直滚动。

这是一个小示例代码:

#include <QGraphicsView>
#include <QGraphicsScene>
#include <QApplication>
#include <QImage>
#include <QBrush>
#include <QDebug>
#include <QDesktopWidget>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QGraphicsScene *scene = new QGraphicsScene;
    QGraphicsView *view = new QGraphicsView;
    view->setScene(scene);
    QRect rec = QApplication::desktop()->availableGeometry();
    int screenHeight = rec.height();
    int screenWidth = rec.width();
    QImage *back = new QImage("image.jpg");
    QImage *background = new QImage(back->scaled(screenWidth,screenHeight,Qt::KeepAspectRatio,Qt::FastTransformation));
    QBrush *brush = new QBrush(*background);
    view->setBackgroundBrush(*brush);
 //   view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
 //   view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    view->setFixedSize(screenWidth,screenHeight);
    view->setSceneRect(0,0,screenWidth,screenHeight);
    view->show();

    return a.exec();
}

我的屏幕分辨率是 1366x768 像素,所以我使用了那个尺寸的图像,并在显示之前使用 QImage::scaled() 函数对其进行了缩放。

例如,如果输入是: input image 图片归属:Mirela1234 , 1366x768-Natural-desktop-wallpaper , CC BY-SA 4.0

当我运行上面的代码时,我得到: result (我已经从屏幕上裁剪了顶部和左侧面板)。

为什么会这样? availableGeometry() 不考虑标题栏吗?我该如何纠正这个问题?

谢谢。

最佳答案

availableGeometry() 完全正确:)

问题是您没有考虑帐户框架大小。您将图像大小调整为最大可用几何图形,但窗口框架也占据了一些位置。最后你有更大的图像,然后是小部件上的可用空间。这就是为什么你有滚动条。

你可以做的是例如:

// 2 - because frame border is on the bottom and on the top.
QImage *background = new QImage(back->scaledToHeight(rec.height() - view->frameWidth() * 2));

那行得通。

关于c++ - Qt 中的 QDesktopWidget availableGeometry() 无法正常工作,需要垂直滚动才能查看完整图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42474183/

相关文章:

c++ - 无限循环不接受输入

c++ - 从文本文件加载链接列表并打印时无限循环

html - 图片将内容推离页面

php - 如何将 Gimp 预加载到网络服务器的内存中?

java,调整图片大小

windows - Qt/C++ : Icons not showing up when program is run under windows O. 小号

c++ - g++ 失败,标准 C++ 库出现 "undefined reference"错误

c++ - 如何运行在启动时运行的 Qt 应用程序?

Qt - 信号槽中的参数

c++ - 了解 GLM 4x4 矩阵函数