开发者

Proper way to encapsulate QWidgets?

开发者 https://www.devze.com 2023-02-01 05:23 出处:网络
I\'m making a Qt widget (let us call it A) that really is a subclassed QGraphicsView. However, I don\'t want to expose QGraphicsView methods in my widget, just the basic QWidget interface and some of

I'm making a Qt widget (let us call it A) that really is a subclassed QGraphicsView. However, I don't want to expose QGraphicsView methods in my widget, just the basic QWidget interface and some of my own methods. Thus, I'd like to encapsulate an instance of A as a membe开发者_Go百科r of a simple QWidget subclass we'll call B.

Now I wonder, what is the proper way to draw this? So far I've given B a layout whose only item is the A member. But this feels sort of wrong; for example, I'll need to override default layout settings to avoid adding extra margins etc. This leads me to feel that there is some correct way to do this that I'm missing. Any thoughts?

Edit: In the setting of RedX' answer, my question becomes: What is the proper way to use gv in this setting? Give A a layout and add gv to it, or override A's painting methods to instead use those of gv? Or something else?


I don't think there is a better way. If you don't want to use a layout, you can override the parent's resizeEvent() like this:

void A::resizeEvent( QResizeEvent* ) {
    m_graphicsView->setGeometry( QRect( 0, 0, size() ) );
} 


I think you are trying to do this?

class A : public QWidget{
  QGraphicsView* gv; //use this to do whatever you need
};

This should expose a as a QWidget and internally you'd use the gv to do whatever you need from the QGraphicsWidget.

0

精彩评论

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

关注公众号