#include "graphicsview.h" #include #include #include #include #include #include #include class ColoredBoxItem : public QGraphicsWidget { public: ColoredBoxItem( QColor color ) : m_color( color ) { setMinimumSize( 50, 50 ); setMaximumSize( 50, 50 ); } protected: void paint( QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget * ) { painter->fillRect( option->rect, m_color ); } private: QColor m_color; }; GraphicsView::GraphicsView(QWidget *parent) : QGraphicsView(parent) { QGraphicsScene* scene = new QGraphicsScene( this ); setScene( scene ); m_green = new ColoredBoxItem( Qt::green ); QGraphicsLinearLayout* layout = new QGraphicsLinearLayout( Qt::Horizontal ); layout->addStretch(); layout->addItem( new ColoredBoxItem( Qt::red ) ); layout->addItem( m_green ); layout->addItem( new ColoredBoxItem( Qt::blue ) ); layout->addStretch(); m_pb = new QPushButton( "Hide" ); connect( m_pb, SIGNAL( clicked() ), this, SLOT( clicked() ) ); QGraphicsProxyWidget* proxy = new QGraphicsProxyWidget; proxy->setWidget( m_pb ); layout->addItem( proxy ); QGraphicsWidget* widget = new QGraphicsWidget; widget->setLayout( layout ); scene->addItem( widget ); } void GraphicsView::clicked() { if( m_green->isVisible() ) { m_pb->setText( "Show" ); m_green->hide(); } else { m_pb->setText( "Hide" ); m_green->show(); } } GraphicsView::~GraphicsView() { }