2017-02-26 1199 views
0

我有一个背景图像,我想在Qt 5.7中的QGraphicsView中显示。我希望窗口的大小能够完全覆盖我的整个屏幕,而不需要滚动。所以,我想确保它为Windows中的任务栏和Ubuntu的顶部和左侧面板留出空间(我正在使用Ubuntu 14.04)。QDesktopWidget Qt中的availableGeometry()无法正常工作,需要垂直滚动以查看完整图像

我从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(); 
} 

我的屏幕分辨率是1366×768像素,因此我已经使用该尺寸的图像,以及使用该功能QImage::scaled()显示之前缩放它。

例如,如果输入是: input image 图片归因:Mirela12341366x768-Natural-desktop-wallpaperCC BY-SA 4.0

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

这是怎么发生的? availableGeometry()不是说说标题栏吗?我该如何解决这个问题?

谢谢。

回答

1

availableGeometry()的作品完全正确:)

的问题是,你没有考虑到帐户帧大小。您将图像调整为最大可用几何体,但窗体框架也占用一些位置。最后,你的图片变大了,然后可以在widget上留下空间。这就是为什么你有滚动条。

你可以做什么,例如:

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

这会作品。