Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QTableWidget in QGraphicsScene

QTableWidget in QGraphicsScene

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 368 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    LiuXiaoHhan
    wrote on 9 Oct 2023, 08:29 last edited by LiuXiaoHhan 10 Sept 2023, 09:05
    #1

    2.gif 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 5662

        setDragMode(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();
    

    }

    L 1 Reply Last reply 12 Oct 2023, 09:41
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 9 Oct 2023, 19:14 last edited by
      #2

      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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      L 1 Reply Last reply 10 Oct 2023, 03:10
      1
      • S SGaist
        9 Oct 2023, 19:14

        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.

        L Offline
        L Offline
        LiuXiaoHhan
        wrote on 10 Oct 2023, 03:10 last edited by LiuXiaoHhan 10 Oct 2023, 03:13
        #3

        @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

        A 1 Reply Last reply 10 Oct 2023, 15:48
        0
        • L LiuXiaoHhan
          10 Oct 2023, 03:10

          @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

          A Offline
          A Offline
          Asperamanca
          wrote on 10 Oct 2023, 15:48 last edited by Asperamanca 10 Oct 2023, 15:48
          #4

          @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:

          1. Derive from QTableWidget and explicitly ignore all mouse events that you want passed to the QGraphicsView
          2. 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.
          1 Reply Last reply
          3
          • L LiuXiaoHhan
            9 Oct 2023, 08:29

            2.gif 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 5662

                setDragMode(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();
            

            }

            L Offline
            L Offline
            LiuXiaoHhan
            wrote on 12 Oct 2023, 09:41 last edited by
            #5

            @LiuXiaoHhan i figureout
            try this one in middlebtn pressing event
            setInteractive(false);
            and after releasing restore this status in QGraphicsView
            setInteractive(true);

            1 Reply Last reply
            0
            • L LiuXiaoHhan has marked this topic as solved on 18 Sept 2024, 08:46

            2/5

            9 Oct 2023, 19:14

            • Login

            • Login or register to search.
            2 out of 5
            • First post
              2/5
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved