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 4.4k 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 Offline
    P Offline
    Peter_Dev
    wrote on last edited by
    #1

    Hello everyone!
    I'm making an app for resizing bmp images. I want to be able to choose area for resizing, so i implemented QRubberBand for this aim. Everything is fine except mapToScene.
    When i'm trying to get point coordinates according to original image, i get them with offset. Moreover, offset is different from image to image, but around 50 pixels on x axis and 70 on y axis.
    cpp file

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include "QGraphicsPixmapItem"
    #include "QPixmap"
    #include "QGraphicsScene"
    #include "QFileDialog"
    #include "QDir"
    #include "QStandardItemModel"
    #include "QLineEdit"
    #include "QMouseEvent"
    #include "QRubberBand"
    #include "QDebug"
    
    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    void MainWindow::mousePressEvent(QMouseEvent *event)
    {
        QPoint origin = event->pos();
        QPointF pic_pos = ui->graphicsView->mapToScene(event->pos());
        QPoint p = pic_pos.toPoint();
        qDebug() << p.rx(); //prints position with some offset
        qDebug()<<p.ry();
        origin.setX(origin.rx()-50); //i want cursor to be at center of square
        origin.setY(origin.ry()-50);
        QSize pos(100,100); //chosing area is a square with 100 by 100 size
        rubberBand->setGeometry(QRect(origin, pos));
        QPalette pal;
        pal.setBrush(QPalette::Highlight, QBrush(Qt::green));
        rubberBand->setPalette(pal);
        rubberBand->show();
    }
    
    void MainWindow::on_actionOpen_file_triggered()
    {
        QString filter = "BMP image (*.bmp)";
        QString file_name = QFileDialog::getOpenFileName(this, "Choose image",QDir::homePath(), filter);
        QGraphicsScene *scene = new QGraphicsScene(this);
        QPixmap pixmap(file_name);
        scene->addPixmap(pixmap);
    
        ui->graphicsView->setScene(scene);
        ui->graphicsView->fitInView(scene->sceneRect(), Qt::KeepAspectRatio);
        ui->graphicsView->show();
    }
    
    

    main

    #include "mainwindow.h"
    
    #include <QApplication>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        MainWindow w;
        w.show();
        return a.exec();
    }
    
    

    header

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    #include <QRubberBand>
    
    QT_BEGIN_NAMESPACE
    namespace Ui { class MainWindow; }
    QT_END_NAMESPACE
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        MainWindow(QWidget *parent = nullptr);
        ~MainWindow();
    
    private slots:
        void on_actionOpen_file_triggered();
        void mousePressEvent(QMouseEvent *event) override;
    
    private:
        Ui::MainWindow *ui;
        QRubberBand *rubberBand = new QRubberBand(QRubberBand::Rectangle, this);
    };
    #endif // MAINWINDOW_H
    
    
    1 Reply Last reply
    0
    • Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      See QMouseEvent::pos() - you have a position relative to your mainmwindow, not to your view here.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      P 1 Reply Last reply
      2
      • Christian EhrlicherC Christian Ehrlicher

        See QMouseEvent::pos() - you have a position relative to your mainmwindow, not to your view here.

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

        @Christian-Ehrlicher Thanks for your help! But could you please explain how to do it?

        1 Reply Last reply
        0
        • Christian EhrlicherC Online
          Christian EhrlicherC Online
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          You have to map the coordinates from your mainwindow to your scene -> QWidget::mapTo()

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          P 1 Reply Last reply
          0
          • Christian EhrlicherC Christian Ehrlicher

            You have to map the coordinates from your mainwindow to your scene -> QWidget::mapTo()

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

            @Christian-Ehrlicher I got the idea, but still don't know how to implement it. Could you please provide some code?

            1 Reply Last reply
            0
            • Christian EhrlicherC Online
              Christian EhrlicherC Online
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @Peter_Dev said in mapToScene returns position with offset:

              Could you please provide some code?

              So hard to call mapTo(ui->graphicsView, event->pos()) ?

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              P 1 Reply Last reply
              0
              • Christian EhrlicherC Christian Ehrlicher

                @Peter_Dev said in mapToScene returns position with offset:

                Could you please provide some code?

                So hard to call mapTo(ui->graphicsView, event->pos()) ?

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

                @Christian-Ehrlicher when i make it like this, program finishes unexpectedly if i click anywhere on picture

                1 Reply Last reply
                0
                • Christian EhrlicherC Online
                  Christian EhrlicherC Online
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  Use a debugger and take a look at the backtrace to see where exactly t crashes.

                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

                  P 1 Reply Last reply
                  0
                  • Christian EhrlicherC Christian Ehrlicher

                    Use a debugger and take a look at the backtrace to see where exactly t crashes.

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

                    @Christian-Ehrlicher Segmentation fault. Seems to happen at mapTo

                    1 Reply Last reply
                    0
                    • Christian EhrlicherC Online
                      Christian EhrlicherC Online
                      Christian Ehrlicher
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      Then make sure all your pointers you're using are valid. And please show some code...

                      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                      Visit the Qt Academy at https://academy.qt.io/catalog

                      P 1 Reply Last reply
                      1
                      • Christian EhrlicherC Christian Ehrlicher

                        Then make sure all your pointers you're using are valid. And please show some code...

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

                        @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 1 Reply Last reply
                        0
                        • 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

                                          • Login

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