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. mapToScene returns position with offset
Qt 6.11 is out! See what's new in the release blog

mapToScene returns position with offset

Scheduled Pinned Locked Moved Unsolved General and Desktop
24 Posts 4 Posters 5.1k 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.
  • P Peter_Dev

    @Christian-Ehrlicher Only change i made was in this function:

    void MainWindow::mousePressEvent(QMouseEvent *event)
    {
        QPoint origin = event->pos();
        QPointF res = ui->graphicsView->mapToScene(mapTo(ui->graphicsView,event->pos()));//here
        QPoint p = res.toPoint(); 
        qDebug() << p.rx();
        qDebug()<<p.ry();
        origin.setX(origin.rx()-50);
        origin.setY(origin.ry()-50);
        QSize pos(100,100);
        rubberBand->setGeometry(QRect(origin, pos));
        QPalette pal;
        pal.setBrush(QPalette::Highlight, QBrush(Qt::green));
        rubberBand->setPalette(pal);
        rubberBand->show();
    }
    
    jsulmJ Offline
    jsulmJ Offline
    jsulm
    Lifetime Qt Champion
    wrote on last edited by
    #12

    @Peter_Dev said in mapToScene returns position with offset:

    PointF res = ui->graphicsView->mapToScene(mapTo(ui->graphicsView,event->pos()));//here

    What is "mapTo" here?
    Did you check all the pointers as already suggested?

    https://forum.qt.io/topic/113070/qt-code-of-conduct

    P 1 Reply Last reply
    0
    • jsulmJ jsulm

      @Peter_Dev said in mapToScene returns position with offset:

      PointF res = ui->graphicsView->mapToScene(mapTo(ui->graphicsView,event->pos()));//here

      What is "mapTo" here?
      Did you check all the pointers as already suggested?

      P Offline
      P Offline
      Peter_Dev
      wrote on last edited by
      #13

      @jsulm Checking now. mapTo maps click position relative to graphicsView, i guess. Am i wrong?

      jsulmJ 1 Reply Last reply
      0
      • P Peter_Dev

        @jsulm Checking now. mapTo maps click position relative to graphicsView, i guess. Am i wrong?

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #14

        @Peter_Dev Did you debug and/or check the pointers? This is actually the first thing to do if your app is crashing...

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        P 1 Reply Last reply
        0
        • jsulmJ jsulm

          @Peter_Dev Did you debug and/or check the pointers? This is actually the first thing to do if your app is crashing...

          P Offline
          P Offline
          Peter_Dev
          wrote on last edited by
          #15

          @jsulm it crushes at mapTo, when it tries to call mapToParent inside itself. I think problem is in passing ui->graphicsView as first argument in mapTo

          jsulmJ 1 Reply Last reply
          0
          • P Peter_Dev

            @jsulm it crushes at mapTo, when it tries to call mapToParent inside itself. I think problem is in passing ui->graphicsView as first argument in mapTo

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #16

            @Peter_Dev Again: did you check the pointers? Are those valid?

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            P 1 Reply Last reply
            0
            • jsulmJ jsulm

              @Peter_Dev Again: did you check the pointers? Are those valid?

              P Offline
              P Offline
              Peter_Dev
              wrote on last edited by
              #17

              @jsulm Sorry, i'm new here. I don't quite understand what you mean by checking pointers, but as i understood you, yes, all pointers are valid. Before i put mapTo here, everything was working correctly. Sorry for wasting your time, but it's really important project for me, so i need your help very much.

              JonBJ 1 Reply Last reply
              0
              • P Peter_Dev

                @jsulm Sorry, i'm new here. I don't quite understand what you mean by checking pointers, but as i understood you, yes, all pointers are valid. Before i put mapTo here, everything was working correctly. Sorry for wasting your time, but it's really important project for me, so i need your help very much.

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by
                #18

                @Peter_Dev
                Your code shows

                mapTo(ui->graphicsView,event->pos())
                

                And you wrote

                @jsulm it crushes at mapTo, when it tries to call mapToParent inside itself. I think problem is in passing ui->graphicsView as first argument in mapTo

                If you're saying that crashes, you need to look at its code to determine why. If you're asking for our help, how can we help if you don't show mapTo()'s code?

                jsulmJ P 2 Replies Last reply
                0
                • JonBJ JonB

                  @Peter_Dev
                  Your code shows

                  mapTo(ui->graphicsView,event->pos())
                  

                  And you wrote

                  @jsulm it crushes at mapTo, when it tries to call mapToParent inside itself. I think problem is in passing ui->graphicsView as first argument in mapTo

                  If you're saying that crashes, you need to look at its code to determine why. If you're asking for our help, how can we help if you don't show mapTo()'s code?

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #19

                  @JonB I think it's the mapTo from QMainWindow from QWidget :-)
                  This was my mistake also when reading this code :-)

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0
                  • JonBJ JonB

                    @Peter_Dev
                    Your code shows

                    mapTo(ui->graphicsView,event->pos())
                    

                    And you wrote

                    @jsulm it crushes at mapTo, when it tries to call mapToParent inside itself. I think problem is in passing ui->graphicsView as first argument in mapTo

                    If you're saying that crashes, you need to look at its code to determine why. If you're asking for our help, how can we help if you don't show mapTo()'s code?

                    P Offline
                    P Offline
                    Peter_Dev
                    wrote on last edited by
                    #20

                    @JonB

                    QPoint QWidget::mapTo(const QWidget * parent, const QPoint & pos) const
                    {
                        QPoint p = pos;
                        if (parent) {
                            const QWidget * w = this;
                            while (w != parent) {
                                Q_ASSERT_X(w, "QWidget::mapTo(const QWidget *parent, const QPoint &pos)",
                                           "parent must be in parent hierarchy");
                                p = w->mapToParent(p); 
                                w = w->parentWidget();
                            }
                        }
                        return p;
                    }
                    
                    QPoint QWidget::mapToParent(const QPoint &pos) const
                    {
                        return pos + data->crect.topLeft(); //crushes here
                    }
                    
                    1 Reply Last reply
                    0
                    • JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by JonB
                      #21

                      @jsulm , @Peter_Dev
                      OK then. https://doc.qt.io/qt-5/qwidget.html#mapTo

                      QPoint QWidget::mapTo(const QWidget *parent, const QPoint &pos) const

                      Translates the widget coordinate pos to the coordinate system of parent. The parent must not be nullptr and must be a parent of the calling widget.

                      I think we can assume parent, ui->graphicsView, is not nullptr, else ui->graphicsView->mapToScene(...) would dump, and it wouldn't be to do with mapTo(). But you should check that ui->graphicsView pointer value.

                      If not that, I can't quite get my head around it, but ui->graphicsView cannot be parent of "calling widget" which is MainWindow, can it?

                      EDIT This post crossed with @Peter_Dev's latest.

                       return pos + data->crect.topLeft(); //crushes here
                      

                      Assuming you are seeing this in the debugger, what is the value of data and what does it point to?

                      P 1 Reply Last reply
                      0
                      • JonBJ JonB

                        @jsulm , @Peter_Dev
                        OK then. https://doc.qt.io/qt-5/qwidget.html#mapTo

                        QPoint QWidget::mapTo(const QWidget *parent, const QPoint &pos) const

                        Translates the widget coordinate pos to the coordinate system of parent. The parent must not be nullptr and must be a parent of the calling widget.

                        I think we can assume parent, ui->graphicsView, is not nullptr, else ui->graphicsView->mapToScene(...) would dump, and it wouldn't be to do with mapTo(). But you should check that ui->graphicsView pointer value.

                        If not that, I can't quite get my head around it, but ui->graphicsView cannot be parent of "calling widget" which is MainWindow, can it?

                        EDIT This post crossed with @Peter_Dev's latest.

                         return pos + data->crect.topLeft(); //crushes here
                        

                        Assuming you are seeing this in the debugger, what is the value of data and what does it point to?

                        P Offline
                        P Offline
                        Peter_Dev
                        wrote on last edited by
                        #22

                        @JonB written that "No such data". So, how to solve my problem? Should i use mapTo or something else?

                        JonBJ 1 Reply Last reply
                        0
                        • P Peter_Dev

                          @JonB written that "No such data". So, how to solve my problem? Should i use mapTo or something else?

                          JonBJ Offline
                          JonBJ Offline
                          JonB
                          wrote on last edited by JonB
                          #23

                          @Peter_Dev
                          For the record, am I right that you (or at least the Qt mapTo() code you show) are not compiling for debug? I believe in debug it would have hit the

                                      Q_ASSERT_X(w, "QWidget::mapTo(const QWidget *parent, const QPoint &pos)",
                                                 "parent must be in parent hierarchy");
                          

                          You'll have to ask the experts what's going on/you should do here, I think QMainWindow::mapTo(ui->graphicsView) is the wrong thing to be doing, but I don't claim to know what you should be doing....

                          P 1 Reply Last reply
                          1
                          • JonBJ JonB

                            @Peter_Dev
                            For the record, am I right that you (or at least the Qt mapTo() code you show) are not compiling for debug? I believe in debug it would have hit the

                                        Q_ASSERT_X(w, "QWidget::mapTo(const QWidget *parent, const QPoint &pos)",
                                                   "parent must be in parent hierarchy");
                            

                            You'll have to ask the experts what's going on/you should do here, I think QMainWindow::mapTo(ui->graphicsView) is the wrong thing to be doing, but I don't claim to know what you should be doing....

                            P Offline
                            P Offline
                            Peter_Dev
                            wrote on last edited by Peter_Dev
                            #24

                            Ok, i understand. I'll be trying to figure it out. Thanks to everyone who helped me here and sorry for wasting your time.

                            1 Reply Last reply
                            0

                            • Login

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