#include "mainwindow.h" #include "ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow), m_scene(NULL), m_view(NULL), m_table(NULL), m_aGraphicItemGroup(NULL) { ui->setupUi(this); // create our scene m_scene = new QGraphicsScene(this); m_scene->addText("Hello, world!"); // create a simple QTableWidget, 2 rows, 2 columns, filled with "x" m_table = new QTableWidget; m_table->setColumnCount(2); m_table->setRowCount(2); for (int i=0;i<2;++i) for (int j=0;i<2;++i) { QTableWidgetItem* item = new QTableWidgetItem("x"); m_table->setItem(i,j,item); } // create a simple QGraphicItem and add it to the scene m_aGraphicItemGroup = new QGraphicsItemGroup(); m_scene->addItem(m_aGraphicItemGroup); // create a proxy that will contain our QTableWidget // add the proxy not to the scene, but to the QGrpahicItem we just added QGraphicsProxyWidget* tableProxy = new QGraphicsProxyWidget(); tableProxy->setParentItem(m_aGraphicItemGroup); tableProxy->setWidget(m_table); // create a menu action that will delete the QGrpahicItem from the scene QMenu* deleteMenu = menuBar()->addMenu("Menu"); QAction* deleteItem = new QAction("Delete Item",deleteMenu); connect(deleteItem,SIGNAL(triggered(bool)),SLOT(deleteItem())); deleteMenu->addAction(deleteItem); // creates the view with your scene, and add it to the main window m_view = new QGraphicsView(m_scene); setCentralWidget(m_view); } MainWindow::~MainWindow() { delete ui; } void MainWindow::deleteItem() { if (m_aGraphicItemGroup != NULL) { delete m_aGraphicItemGroup; m_aGraphicItemGroup = NULL; } }