boost::shared_ptr<QDialog> dlg(new QDialog); dlg->setModal(true); qgraphics_window* view = new qgraphics_window(dlg.get()); QVBoxLayout* lay = new QVBoxLayout(dlg.get()); lay->addWidget(view); lay->setContentsMargins(0, 0, 0, 0); view->setFrameShape(QFrame::NoFrame); QGraphicsScene* scene = view->scene(); QGraphicsLinearLayout* l2 = new QGraphicsLinearLayout(Qt::Vertical); view->proxy_widget()->setLayout(l2); QGraphicsProxyWidget* pw1 = new QGraphicsProxyWidget(view->proxy_widget()); pw1->setWidget(new QTextEdit()); l2->addItem(pw1); pw1->setSizePolicy(QSizePolicy(QSizePolicy::Minimum, QSizePolicy::Maximum)); QGroupBox* gb = new QGroupBox(); QGraphicsProxyWidget* pw2 = new QGraphicsProxyWidget(view->proxy_widget()); pw2->setWidget(gb); l2->addItem(pw2); pw2->setSizePolicy(QSizePolicy(QSizePolicy::Minimum, QSizePolicy::Maximum)); gb->setTitle("Group Box"); QGraphicsLinearLayout* l3 = new QGraphicsLinearLayout(Qt::Vertical); pw2->setLayout(l3); QGraphicsProxyWidget* pw3 = new QGraphicsProxyWidget(pw2); pw3->setWidget(new QPushButton()); l3->addItem(pw3); QGraphicsProxyWidget* pw = new QGraphicsProxyWidget(pw2); pw->setWidget(new QLineEdit()); l3->addItem(pw); pw = new QGraphicsProxyWidget(pw2); pw->setWidget(new QLineEdit()); l3->addItem(pw); dlg->exec();
C++ (Qt)#include <QApplication>#include <QDialog>#include <QGraphicsView>#include <QGraphicsScene>#include <QGraphicsProxyWidget>#include <QGraphicsLinearLayout>#include <QGroupBox>#include <QVBoxLayout>#include <QTextEdit>#include <QLineEdit>#include <QPushButton>#include <QMenu> static void reloadContextMenu( QMenu* orig_menu, QWidget* parent ){ QList< QAction* > la = orig_menu->actions(); QMenu* menu = new QMenu( parent ); menu->addActions( la ); menu->setAttribute( Qt::WA_DeleteOnClose ); menu->popup( QCursor::pos() ); orig_menu->close();} class Dialog : public QDialog{public: Dialog( QWidget* parent = 0 ) : QDialog( parent ) { qApp->installEventFilter( this ); } ~Dialog() {}protected: bool eventFilter( QObject *obj, QEvent *event ) { if( event->type() == QEvent::ContextMenu ) { QMenu* menu = 0; if( QLineEdit* le = qobject_cast< QLineEdit* >( obj ) ) menu = le->createStandardContextMenu(); else if( QTextEdit* te = qobject_cast< QTextEdit* >( obj->parent() ) ) menu = te->createStandardContextMenu(); if( menu ) { reloadContextMenu( menu, this ); return true; } } return false; }}; int main( int argc, char** argv ){ QApplication app( argc, argv ); QDialog* dlg = new Dialog(); QVBoxLayout* lay = new QVBoxLayout( dlg ); QGraphicsView* gv = new QGraphicsView(); gv->setFrameStyle( QFrame::NoFrame ); lay->addWidget( gv ); QGraphicsScene* scene = new QGraphicsScene( gv ); gv->setScene( scene ); QGraphicsProxyWidget* pw = new QGraphicsProxyWidget(); scene->addItem( pw ); QGraphicsLinearLayout* l2 = new QGraphicsLinearLayout( Qt::Vertical ); pw->setLayout( l2 ); QGraphicsProxyWidget* pw1 = new QGraphicsProxyWidget( pw ); pw1->setWidget( new QTextEdit() ); l2->addItem( pw1 ); pw1->setSizePolicy( QSizePolicy( QSizePolicy::Minimum, QSizePolicy::Maximum ) ); QGroupBox* gb = new QGroupBox(); QGraphicsProxyWidget* pw2 = new QGraphicsProxyWidget( pw ); pw2->setWidget( gb ); l2->addItem( pw2 ); pw2->setSizePolicy( QSizePolicy( QSizePolicy::Minimum, QSizePolicy::Maximum ) ); gb->setTitle("Group Box"); lay = new QVBoxLayout( gb ); lay->addWidget( new QPushButton() ); lay->addWidget( new QLineEdit() ); lay->addWidget( new QLineEdit() ); dlg->exec();}
item widget_item graphics_item layout