开发者

What's the easiest way to write a custom layout in QT?

I ran in to a problem where I was doing property animations on widgets but they animation positions wo开发者_如何学编程uld seem to get overwritten on the next layout pass. The widgets would seem to jump position right at the beginning of animation, making it look bad. As a result I thought maybe if I wrote my own layout code I could have more control over things.

What is the easiest way to write a custom layout in QT? I tried following the steps in the "Manual Layout" section of the documentation: http://doc.qt.nokia.com/latest/layout.html#manual-layout However I seem to be hitting a crash when I try calling setGeometry on the child widgets. Also, the docs seem to conflict with the "Manual Layout" saying to call setGeometry from within resizeEvent and the resizeEvent doc saying not to do that.

I suppose I could just write a real layout class. Doesn't look like too much code. But I'm wondering what the easiest/simplest way to get custom layout is.


I think this might work.

  1. Start with widget A positioned entirely outside of the parent widget. Do not add that widget to the layout. A's geometry may well share an edge with the parent.

  2. Create a dummy widget D that forwards A's sizeHint() but does nothing else. Put it into the layout.

  3. Animate widget A toward the geometry of widget D.

  4. Swap the widgets in the layout (remove widget D, add widget A).

If widgets A and D are of your own making, then you should be using the PIMPL idiom. If your widget has no child widgets, swapping the widgets would be trivial -- swap the pimpl pointers. The widgets themselves don't have to be swapped then. DO NOT derive your PIMPL class from QWidgetPrivate, not that it's supported anyway, but I've seen code that does it. Not a good idea.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜