PDA

View Full Version : QChart not getting keypress events.



yagabey
7th June 2018, 18:20
Hi,

I am subclassing QChart. But QChart does not seem to receive keypress events.



class Chart : public QChart
{
public:
explicit Chart(QGraphicsItem *parent = 0, Qt::WindowFlags wFlags = 0);

protected:
void keyPressEvent(QKeyEvent *event);
void mousePressEvent (QGraphicsSceneMouseEvent* me);

private:

};


Chart::Chart(QGraphicsItem *parent, Qt::WindowFlags wFlags)
: QChart(QChart::ChartTypeCartesian, parent, wFlags)
{
setFlags(ItemIsFocusable| ItemIsSelectable | ItemIsMovable | ItemSendsScenePositionChanges);
setAcceptHoverEvents(true);
setFocus();
}

void Chart::keyPressEvent(QKeyEvent *event)
{
qDebug("keyPressEvent");
QChart::keyPressEvent(event);
}

void Chart::mousePressEvent(QGraphicsSceneMouseEvent *me)
{
qDebug("mousePressEvent");
QChart::mousePressEvent(me);
}

I am adding the item into a QGraphicsscene:


QGraphicsScene* sceneGraph= new QGraphicsScene();
sceneGraph->setSceneRect(0,0,1024,768);
sceneGraph->addItem(chart);
ui->graphicsView->setScene(sceneGraph);

It gets mouse events but not key press events.

Any idea what could be the reason?

Thanks.

yagabey
10th June 2018, 14:34
Ok, i got the problem. MousePress event does not set the focus on ChartItem or it is setting focus on some sub items, i dont know. Setting focus manually after mouse press event solves the issue.


void Chart::mousePressEvent(QGraphicsSceneMouseEvent *me)
{
qDebug("mousePressEvent");
QChart::mousePressEvent(me);
setFocus();
}