QTableWidget in QGraphicsScene
-
I seek help of this situation:
I added a QTableWidget in QGraphicsScene
same time i re implementation about QGraphicsView mouse event to drag and Mouse Roaming while pressing middle mouse button;
but once my mouse hover on this TableWidget and pressing middle button it will take no effect about view roaming;
i want the same behavior as i pressing middle mouse button and view will roaming when the mouse crusor in view's margin
i suppose it is because QTableWidget swallow mouse event but how to slove the problems:
heres the code:class CustomView : public QGraphicsView
{
public:
CustomView(QGraphicsScene *scene) : QGraphicsView(scene)
{
setCacheMode(CacheNone);
setViewportUpdateMode(QGraphicsView::FullViewportUpdate); // mashj bug 5662setDragMode(RubberBandDrag); setRenderHint(QPainter::Antialiasing, true); setTransformationAnchor(QGraphicsView::AnchorUnderMouse); setResizeAnchor(QGraphicsView::AnchorUnderMouse); // mashj setInteractive(true); setMouseTracking(true); lastMousePos = QPoint(); m_bIsMiddleButtonClicked = false ; }
protected:
void mouseReleaseEvent(QMouseEvent *event) override
{
if (event->button() == Qt::MiddleButton)
{
setDragMode(QGraphicsView::NoDrag);
m_bIsMiddleButtonClicked = false;
}
QGraphicsView::mouseReleaseEvent(event);
}
void mousePressEvent(QMouseEvent *event) override
{
if (event->button() == Qt::MiddleButton)
{
if (m_bIsMiddleButtonClicked)
{
m_bIsMiddleButtonClicked = false;
setDragMode(QGraphicsView::NoDrag);
}
else
{
setDragMode(QGraphicsView::ScrollHandDrag);
m_bIsMiddleButtonClicked = true;
m_lastMousePosForPan = event->pos() ;
QMouseEvent leftMouseButtonPressEvent(QEvent::MouseButtonPress, event->pos(), Qt::LeftButton,
event->buttons() | Qt::LeftButton,
event->modifiers());
QGraphicsView::mousePressEvent(&leftMouseButtonPressEvent);
return ;
}
}
QGraphicsView::mousePressEvent(event);
}
private:
QPoint lastMousePos;
QPointF m_lastMousePosForPan;
bool m_bIsMiddleButtonClicked;
};int main(int argc, char *argv[])
{
QApplication app(argc, argv);// 创建场景 QGraphicsScene scene; scene.setSceneRect(0,0,1000,1000); // 创建视图 CustomView view(&scene); view.setFixedSize(800,600); QTableWidget tableWidget(10, 10); for (int row = 0; row < 10; ++row) { for (int col = 0; col < 10; ++col) { QTableWidgetItem* item = new QTableWidgetItem(QString("(%1, %2)").arg(row).arg(col)); tableWidget.setItem(row, col, item); } } QGraphicsProxyWidget* proxyWidget = scene.addWidget(&tableWidget) ; proxyWidget->setPos(500,500); //MouseFilter *mouseFilter = new MouseFilter();
// MouseFilter* mouseFilter = new MouseFilter();
// proxyWidget->installEventFilter(mouseFilter);view.show(); return app.exec();
}
-
Hi and welcome to devnet,
One way is to have any edit mode during which you disable the widget or replace it by a pixmap item and move that one.
-
@SGaist thanks for replying Mr SGaist,maybe i did not understand or express myself clearly;
my need is i want the view move , not the widget , while my mouse hovering on top of the QtableWidget , i pressing middle button and then drag mose , i want program view changed ,not the widget , just the view followed my cursor;i want the QTableWidget remain still in QGraphicsScene, its pos() or absolute position did not changed in QGraphicsScene,just the QGraphicsView move
same as operation which i did berfore my mouse hover on QtableWidget in that gif.
Could you please help me or tell me how to solve it?
thanks a lot -
@LiuXiaoHhan
Do you even receive mouse events in QGraphicsView when pressing the middle mouse button on top of the QTableWidget?
If you don't, I see two possible solutions:- Derive from QTableWidget and explicitly ignore all mouse events that you want passed to the QGraphicsView
- Install an event filter before Qt even decides which QWidget should get the event. An event filter installed into your parent window should work. But you will need to perform coordinate system transformations yourself in this case.
-
@LiuXiaoHhan i figureout
try this one in middlebtn pressing event
setInteractive(false);
and after releasing restore this status in QGraphicsView
setInteractive(true); -