为什么图像出现在我的 QGraphicsView 派生类中的不正确位置?
我已经创建了 QGraphicsView 的替代,用于在我的应用程序中的 QMdiSubWindow 内显示图像。后来我计划在此基础上添加对其他浮动形状的支持,因此 QLabel 还不够。这是该类的基本布局:
class ImageView : public QGraphicsView
{
public:
ImageView(QWidget *parent);
~ImageView();
bool clean() const
{
return _clean;
}
void showImage(const QString &path);
private:
virtual void resizeEvent(QResizeEvent *event);
bool _clean;
QGraphicsScene *_scene;
QPixmap _viewPixmap;
QGraphicsPixmapItem *_viewPixmapItem;
};
我想避免每次加载新图像时都分配 QPixmap 元素,因此我只在类中保留一个,并在构造函数中创建一个像素图图形项,如下所示:
ImageView::ImageView(QWidget *parent) : QGraphicsView(parent), _clean(true)
{
_scene = new QGraphicsScene(this);
_scene->setBackgroundBrush(Qt::lightGray);
_viewPixmapItem = _scene->addPixmap(_viewPixmap);
setScene(_scene);
}
稍后当有人单击时在应用程序中的图像上,我(从主 gui 类)调用 showImage 函数:
void ImageView::showImage(const QString &path)
{
_clean = false;
_viewPixmap.load(path);
_viewPixmapItem->setPixmap(_viewPixmap);
}
我的问题是,第一次使用 showImage() 时,图像没有放置在视图的左上角,而是放置在某处靠近中间,大部分在视图之外,但看不到滚动条。下次我尝试显示图像时,在调用 showImage() 后收到调整大小事件,图像显示正确,并且所有后续显示都正常;图像与视图的左上角对齐,并且滚动条可见以反映图像对于视图来说太大(我计划稍后添加缩放功能)。我一遍又一遍地阅读文档,但感到更加困惑,您能给我一些我缺少的内容的提示吗?谢谢。
编辑: 这是图像的外观第一次调用 showImage 时,这里是它的外观后面的,就是我想要的。您可以看到图像视图小部件被放置在 QMdiArea 内的 MDI 样式子窗口中。
编辑2:应用程序将信息写入日志文件,这是我尝试显示前三个图像的部分:
项目列表中激活的图像,名称:obiekt_blisko-0000_0.bmp
ViewMdiSubWindow::prefit:预拟合,新尺寸:516x352
MDI 窗口尺寸:516x352
子QWidget窗口大小:628x477
ImageView::resizeEvent: 收到调整大小事件,旧尺寸为:-1x-1,新尺寸:500x313
ImageView::showImage: 显示图像:D:\praca\dane\pomiar3d_3\obiekt_blisko_L\obiekt_blisko-0000_0.bmp
设置新像素图后查看尺寸:502x315
项目位置(父坐标):0,0
(此后图像显示不正确)
项目列表中激活的图像,名称:obiekt_blisko-0001_0.bmp
MDI 窗口尺寸:516x352
子QWidget窗口大小:502x315
ImageView::showImage: 显示图像:D:\praca\dane\pomiar3d_3\obiekt_blisko_L\obiekt_blisko-0001_0.bmp
设置新像素图后查看尺寸:502x315
项目位置(父坐标):0,0
ImageView::resizeEvent:收到调整大小事件,旧尺寸为:500x313,新尺寸:483x296
(现在图像正常)
项目列表中激活的图像,名称:obiekt_blisko-0002_0.bmp
MDI 窗口尺寸:516x352
子QWidget窗口大小:502x315
ImageView::showImage: 显示图像: D:\praca\dane\pomiar3d_3\obiekt_blisko_L\obiekt_blisko-0002_0.bmp
设置新像素图后查看尺寸:502x315
项目位置(父坐标):0,0
(此事件和下一个事件相同,图像正常)
I have created an override of QGraphicsView for displaying images inside a QMdiSubWindow in my application. Later I plan to add support for other floating shapes on top of that so a QLabel just wasn't enough. Here's the basic layout of the class:
class ImageView : public QGraphicsView
{
public:
ImageView(QWidget *parent);
~ImageView();
bool clean() const
{
return _clean;
}
void showImage(const QString &path);
private:
virtual void resizeEvent(QResizeEvent *event);
bool _clean;
QGraphicsScene *_scene;
QPixmap _viewPixmap;
QGraphicsPixmapItem *_viewPixmapItem;
};
I want to avoid allocating a QPixmap element every time I load a new image, so I just keep one in the class, and also create a pixmap graphics item in the constructor like this:
ImageView::ImageView(QWidget *parent) : QGraphicsView(parent), _clean(true)
{
_scene = new QGraphicsScene(this);
_scene->setBackgroundBrush(Qt::lightGray);
_viewPixmapItem = _scene->addPixmap(_viewPixmap);
setScene(_scene);
}
Later when someone clicks on an image in the app, I call (from the main gui class) the showImage function:
void ImageView::showImage(const QString &path)
{
_clean = false;
_viewPixmap.load(path);
_viewPixmapItem->setPixmap(_viewPixmap);
}
My problem is that the first time I use showImage(), the image is not placed at the top left corner in the view, but rather somewhere near the middle, with most of it outside the view and yet no scrollbar is visible. The next time I try to show an image, I get a resize event after the call to showImage(), the image is displayed correctly, and all subsequent displays are okay; the image is aligned with the top-left corner of the view, and scrollbars are visible to reflect that the image is too large for the view (I plan to add zoom capability later). I read the docs over and over but just got more confused, can you please give me a hint of what I am missing? Thanks.
EDIT: Here's how the image looks at the first call to showImage, and here is how it looks on the subsequent ones, which is what I want. You can see that the image view widget is placed in a MDI-style sub window within a QMdiArea.
EDIT2: The application writes information to a log file, here's the part where I try to show the first three images:
Activated image in project list, name: obiekt_blisko-0000_0.bmp
ViewMdiSubWindow::prefit: Pre-fitting, new size: 516x352
MDI window size: 516x352
Child QWidget window size: 628x477
ImageView::resizeEvent: Received resize event, old size was: -1x-1, new size: 500x313
ImageView::showImage: Showing image: D:\praca\dane\pomiar3d_3\obiekt_blisko_L\obiekt_blisko-0000_0.bmp
View size after setting new pixmap: 502x315
Item position (parent coords): 0,0
(After this, the image is displayed incorrectly)
Activated image in project list, name: obiekt_blisko-0001_0.bmp
MDI window size: 516x352
Child QWidget window size: 502x315
ImageView::showImage: Showing image: D:\praca\dane\pomiar3d_3\obiekt_blisko_L\obiekt_blisko-0001_0.bmp
View size after setting new pixmap: 502x315
Item position (parent coords): 0,0
ImageView::resizeEvent: Received resize event, old size was: 500x313, new size: 483x296
(Now the image is OK)
Activated image in project list, name: obiekt_blisko-0002_0.bmp
MDI window size: 516x352
Child QWidget window size: 502x315
ImageView::showImage: Showing image: D:\praca\dane\pomiar3d_3\obiekt_blisko_L\obiekt_blisko-0002_0.bmp
View size after setting new pixmap: 502x315
Item position (parent coords): 0,0
(This and the next events are identical and the images are OK)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
图像显示其左上角位于视图中心的原因是因为您的场景的
sceneRect
为 (0,0,0,0) 和QGraphicsView
> 默认情况下显示居中的场景。将
seneRect
设置为修复矩形可能会解决你的问题。在图像更改后调用centerOn
也会有所帮助。The reason the image is showing with its top-left corner at the center of the view is because your scene has a
sceneRect
of (0,0,0,0) andQGraphicsView
shows the scene centered by default.Set
seneRect
to a fix rect will probably solve your problem. CallingcenterOn
after the image change will also help._viewPixmapItem->setPos(0, 0) 在 showImage 中不起作用吗?正如你所描述的,它只是处于错误的位置。也许以初始零大小为中心。
Wouldn't _viewPixmapItem->setPos(0, 0) work in showImage? As you describe it, it's just in the wrong postion. Maybe centered with the initial zero size.