如何设置QScrollArea的初始大小?
我想在滚动区域内显示图像。 滚动区域的视口应具有定义的初始大小。 这意味着,如果图像的尺寸大于视口的初始尺寸,则滚动条将可见,否则不可见。
// create label for displaying an image
QImage image( ":/test.png" );
QLabel *label = new QLabel( this );
label->setPixmap( image.toPixmap() );
// put label into scroll area
QScollArea *area = new QScrollArea( this );
area->setWidget( label );
// set the initial size of the view port
// NOTE: This is what I'd like to do, but this method does not exist
area->setViewPortSize( QSize( 300, 300 ) );
应该可以调整整个应用程序的大小,以便视口获得不同于初始大小的另一种大小。
不幸的是,我无法找到如何设置视口的大小。 Qt的布局机制似乎为视口设置了默认大小,但到目前为止我无法更改它。
使用 area->setMinimumSize( QSize( 300, 300 ) ); 设置新大小实际上会设置所需的大小,但随后滚动区域将失去将大小调整为小于 300x300 的能力。
I want to display an image within a scroll area. The view port of the scroll area shall have a defined initial size. That means, if the image's size is bigger than the initial size of the view port, scroll bars will be visible, otherwise not.
// create label for displaying an image
QImage image( ":/test.png" );
QLabel *label = new QLabel( this );
label->setPixmap( image.toPixmap() );
// put label into scroll area
QScollArea *area = new QScrollArea( this );
area->setWidget( label );
// set the initial size of the view port
// NOTE: This is what I'd like to do, but this method does not exist
area->setViewPortSize( QSize( 300, 300 ) );
It shall be possible to resize the whole application so that the view port will get another size than the initial one.
Unfortunately, I was not able to find out, how to set the size of the view port. Qt's layout mechanism seems to set a default size for the view port, but up to now I was not able to change it.
Setting a new size with area->setMinimumSize( QSize( 300, 300 ) );
will actually set the demanded size, but then the scroll area looses the ability to get resized to a size smaller than 300x300.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我认为你看待问题的方式是错误的。 QScrollArea 只是一个放置在框架或 QMainWindow 中的小部件。 小部件的大小由包含它的小部件的布局控制。
看一下 Trolltech 的这个示例:图像查看器示例
I think that you are looking at the problem the wrong way. The QScrollArea is just a widget that you put in a frame or QMainWindow. The size of the widget is controlled by the layout of the widget that contains it.
Take a look at this example from Trolltech: Image Viewer Example
你可以尝试一下:
然而布局和Qt是令人惊奇的Voodoo。 在我看来,它是功能最少的部分。
如果这不起作用,请尝试在各种小部件上调用 QWidget::resize() 。
You can try:
However layout and Qt is amazingly Voodoo. It is IMO its least functional part.
if that doesn't work, try calling QWidget::resize() on various widgets.
滚动区域是顶级小部件吗? 如果是这样,只需调用
如果它在层次结构内,您需要适当地调整顶层的大小(复杂),或设置区域的最小大小。 您还可以尝试使用 LayoutPolicy 进行实验 - 假设 sizeHint 是 QSize(300,300) 您可以根据 https://doc.qt.io/qt-5/qsizepolicy.html#Policy-enum
Is the scroll area the top level widget? If so, simply call
If it's inside a hierarchy you need to resize the toplevel appropriately (complex), or set the minimumSize of the area. You could also try to experiment with the LayoutPolicy - assuming the sizeHint is QSize(300,300) you can give it the appropriate size policy according to what's defined in https://doc.qt.io/qt-5/qsizepolicy.html#Policy-enum
我不认为你可以很容易地做到这一点,也就是说(如果我没读错的话),调整小部件的大小,使内部区域为 300x300。 不过,您也许可以捏造它,因为滚动区域是一种框架,它继承自 QWidget。 这意味着您只需调用
area->resize( 300 + fudge, 300 + fudge )
,其中您的 fudge 值会考虑框架绘制所占用的额外位。但是,我不确定这是否适用于动态调整大小的对话框。 我从来没有做过这样的事情。
I don't think you can do exactly that very easily, which is (if I'm reading correctly), size the widget so that the internal area is 300x300. You might be able to fudge it, however, since a scroll area is a type of frame, which inherits from QWidget. This means you could just call
area->resize( 300 + fudge, 300 + fudge )
, where your fudge values account for the extra bit taken up by the frame's drawing.I'm not sure this would work in a dynamically resizable dialog, however. I haven't ever done anything quite like this.
怎么样使用
How about using
如果您尝试在滚动区域内显示图像,那么最好的选择不是使用标签。
您应该尝试使用 QGraphicsView/QGraphicsScene/QGraphicPixmapItem (而不是滚动区域和标签)。 显示图像时性能要好得多。 当您使用滚动条移动时,滚动区域和标签将非常糟糕地重新绘制图像。
例如,您有一个“.ui”文件,GUI 上有一个名为“qgvImageView”的 QGraphicsView 和一个名为“image”的 QImage。
查看 Qt 文档,这是在 Qt 4.2 中引入的,
我不确定这是否会具体解决问题,但 QGraphicsView 有可能对您尝试执行的操作做出更好的反应。
If you're trying to display an image inside a scroll area, your best bet isn't going with a label.
You should try using a QGraphicsView/QGraphicsScene/QGraphicPixmapItem (instead of the Scroll Area and label). The performance is far better when displaying images. The scroll area and label will re-draw the image very poorly as you move around using the scroll bars.
For example, you have a ".ui" file with a QGraphicsView on the GUI called "qgvImageView" and a QImage called "image".
Check out the Qt Documentation, this was introduced in Qt 4.2
I'm not sure if this will specifically fix the problem, but there is a chance that the QGraphicsView will react better to what you're trying to do.