PDA

View Full Version : Graphics View context Menu...



maverick_pol
8th May 2007, 09:17
Hi guys,

I have created a graphics view in the designer. I also have a class that works with implementing some solutions for the view. So I create the class object(giving the ui.GraphicsView using refrence -> so the class could change it).
But when I try to set context Menu for the Scene(which is also a member of that class -> add to the graphicsView in the constructor) or the GraphicsView It doesn not show.
I have implemented it for the main window and it worked, but When I try to use context menu for the view it does not show. Maybe there is a problem with passing the ui.GraphicsView(created in the Designer) as a reference and then creating it's context menu? Yet I am looking for a solution. Maybe someone had this kind of dilema.

Beforehand thanks!

Maverick

P.S.

Passing the ui.GraphicsView as a reference should do the job, but doesn not work. Or maybe the problem is somewhere else. ANy hints appreciated.

jpn
8th May 2007, 09:30
Could we see the relevant code?

maverick_pol
8th May 2007, 10:00
I am bit busy right now, but will post the code soon.

I have created, using designer, a graphics view -> ui.gvChart
I also have a class containing m_scene and m_view:


class Chart: public QWidget
{
Q_OBJECT

public :
Chart(QGraphicsView*,QWidget* parent = 0);
~Chart();

View* m_view;
QGraphicsScene* m_scene;
..........
}
Chart::Chart( QGraphicsView* v,QWidget* parent): QWidget(parent)
{
m_view = dynamic_cast<View*>(v);
vSetScene();
vSetView();
}
void Chart::vSetView()
{
m_view->setScene(m_scene);
m_view->show();
//setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
}

void Chart::vSetScene()
{
m_scene = new QGraphicsScene(this);
m_scene->setItemIndexMethod(QGraphicsScene::BspTreeIndex);
m_scene->setSceneRect(0, 0, m_view->width(),m_view->height());
m_scene->setBackgroundBrush(QBrush(Qt::gray));
}


Here is the View class:


class View: public QGraphicsView
{
public:
View(QWidget* parent): QGraphicsView(parent) {}
~View() {}

protected:
void contextMenuEvent(QContextMenuEvent*);
};
void View::contextMenuEvent(QContextMenuEvent* event)
{
QMenu* m_contextMenu;
m_contextMenu = new QMenu;
QAction* act = new QAction(QIcon(""),tr("Buuuuu"),this);
m_contextMenu = new QMenu;
m_contextMenu->addAction(act);
m_contextMenu->exec(event->globalPos());
}


In the main class I create the Chart class:


m_chart = new Chart(ui.gvChart,this);


That's about all. I have added contextMenu code(used here) to show main Window context menu and the context Menu appears, but not workin here.

Thanks in advance for help.

Maverick

P.S.
the code compiles, but crashes in the SetScene(...->setSceneRect);