qt - open a QGraphicScene child widget in the mainwindow -
i'm trying create qgraphicscene (with appropriate view) in mainwindow. scene defined in seperate class (a child widget mainwindow).
the open action works , can open every picture, open in new window , not within in mainwindow.
when create label (or so) in child widget, displayed correctly within mainwindow. problem seems qgraphicscene or qgraphicview.
mainwindow:
mainwindow::mainwindow() { qwidget *widget = new qwidget; setcentralwidget(widget); picturearea = new picturearea(this); qhboxlayout *hlayout = new qhboxlayout(this); hlayout->addwidget(picturearea,1); widget->setlayout(hlayout); createactions(); createmenus(); this->setminimumsize(800,600); this->resize(800,600); } ... void mainwindow::open() { qstring filename = qfiledialog::getopenfilename(this, tr("open image"), qdir::currentpath(), tr("image files (*.png *.jpg)")); if (!filename.isempty()) { qimage image(filename); if (image.isnull()) { qmessagebox::information(this, tr("image viewer"), tr("cannot load %1.").arg(filename)); return; } //transfer child widget, guess no mistakes far picturearea->setpicture(image); } }
picturearea:
picturearea::picturearea(qwidget *parent) : qwidget(parent) { } void picturearea::setpicture(qimage image) { qgraphicsscene* scene = new qgraphicsscene(); qgraphicsview* view = new qgraphicsview(scene); qgraphicspixmapitem* item = new qgraphicspixmapitem(qpixmap::fromimage(image)); scene->additem(item); view->show(); }
how can create scene within mainwindow , not in separate window? i'm using qt 4.7.4, windows7 64bit.
you creating new qgraphicsscene
, qgraphicsview
every time set picture. , not putting view
inside layout or setting parent it, it's opening in new window when call view->show()
.
you should create qgraphicsview
, qgraphicsscene
inside constructor.
//picturearea.h ... public: qgraphicsview *view; qgraphicsscene *scene; ...
//pircurearea.cpp picturearea::picturearea(qwidget *parent) : qwidget(parent) { this->setlayout(new qvboxlayout); view = new qgraphicsview(this); this->layout()->addwidget(view); scene = new qgraphicsscene; view->setscene(scene); } void picturearea::setpicture(qimage image) { scene->clear(); scene->addpixmap(qpixmap::fromimage(image)); }
Comments
Post a Comment