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. QLabel Pixmap doesnt match with true size
Forum Updated to NodeBB v4.3 + New Features

QLabel Pixmap doesnt match with true size

Scheduled Pinned Locked Moved Unsolved General and Desktop
14 Posts 5 Posters 1.1k Views 3 Watching
  • 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.
  • I Offline
    I Offline
    Infestor
    wrote on last edited by
    #1

    Hi,

    I want to implement a minimap into my application and have the following code for that

    	layout_ = new QBoxLayout(QBoxLayout::TopToBottom, this);
    	p = new QPixmap("minimap.png");
            l = new QLabel();
    
    	l->setPixmap(*p);
    	layout_->addWidget(l, 1, Qt::AlignCenter);
    

    Via mousePressEvent i can now click on the minimap and change my position on the real map. However i noticed that the point 0,0 in the minimap widget is not part of the image. (and so are other points likewise). Is there a way to fix this such that the 0,0 coordinate is the top left point of the image?

    Pl45m4P 1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Can you show what you get ?

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

      1 Reply Last reply
      1
      • I Infestor

        Hi,

        I want to implement a minimap into my application and have the following code for that

        	layout_ = new QBoxLayout(QBoxLayout::TopToBottom, this);
        	p = new QPixmap("minimap.png");
                l = new QLabel();
        
        	l->setPixmap(*p);
        	layout_->addWidget(l, 1, Qt::AlignCenter);
        

        Via mousePressEvent i can now click on the minimap and change my position on the real map. However i noticed that the point 0,0 in the minimap widget is not part of the image. (and so are other points likewise). Is there a way to fix this such that the 0,0 coordinate is the top left point of the image?

        Pl45m4P Offline
        Pl45m4P Offline
        Pl45m4
        wrote on last edited by
        #3

        @Infestor said in QLabel Pixmap doesnt match with true size:

        point 0,0 in the minimap widget is not part of the image.

        There might be some margin / border.

        Btw: What coordinate system do you use, when you print out the point, where you have clicked?
        The result you get could be in parent coordinates.


        If debugging is the process of removing software bugs, then programming must be the process of putting them in.

        ~E. W. Dijkstra

        I 1 Reply Last reply
        1
        • Pl45m4P Pl45m4

          @Infestor said in QLabel Pixmap doesnt match with true size:

          point 0,0 in the minimap widget is not part of the image.

          There might be some margin / border.

          Btw: What coordinate system do you use, when you print out the point, where you have clicked?
          The result you get could be in parent coordinates.

          I Offline
          I Offline
          Infestor
          wrote on last edited by Infestor
          #4

          @Pl45m4 Yes i think thats the case too. I have a layout that adds the minimap. However i thought that implementing the mousePressEvent in the minimap widget would yield the minimap coordinates and not the parent ones. Is there a way to fix this?

          mrjjM 1 Reply Last reply
          0
          • I Infestor

            @Pl45m4 Yes i think thats the case too. I have a layout that adds the minimap. However i thought that implementing the mousePressEvent in the minimap widget would yield the minimap coordinates and not the parent ones. Is there a way to fix this?

            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @Infestor
            Hi
            Can you show us how you done as mousePress event override directly in the MiniMap
            should work and have top,left as 0,0

            1 Reply Last reply
            0
            • I Offline
              I Offline
              Infestor
              wrote on last edited by Infestor
              #6

              Sure, here you go

              void MiniMap::mousePressEvent(QMouseEvent* event)
              {
              	int mouse_x = event->pos().x() ;
              	int mouse_y = event->pos().y();
              
                    //...logic
              
              
              }
              

              Is it needed to pass the event on to the parent?

              mrjjM 1 Reply Last reply
              0
              • I Infestor

                Sure, here you go

                void MiniMap::mousePressEvent(QMouseEvent* event)
                {
                	int mouse_x = event->pos().x() ;
                	int mouse_y = event->pos().y();
                
                      //...logic
                
                
                }
                

                Is it needed to pass the event on to the parent?

                mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by mrjj
                #7

                @Infestor

                Hi
                It's often best to call the base class when you override a function but
                since QLabel don't use it for any features, it's not needed in this
                case. ( but not hurt either)

                so if you qDebug()
                mouse_x and mouse_y

                they are not in local coordinates?

                I tested with

                class ClickImage : public QLabel
                {
                    Q_OBJECT
                public:
                    explicit ClickImage(QWidget *parent = nullptr)
                        : QLabel(parent) {}
                    ~ClickImage() {}
                signals:
                    void clicked(int x, int y);
                protected:
                    void mousePressEvent(QMouseEvent *event) override
                    {
                        int mx = event->pos().x();
                        int my = event->pos().y();
                        qDebug() << mx << "-" << my;
                        emit clicked(mx, my);
                    }
                };
                

                and the mx and my are as expected so not sure why it would be for you.

                Are you sure you adjusted the logic to now be inside the minimap ?

                1 Reply Last reply
                1
                • I Offline
                  I Offline
                  Infestor
                  wrote on last edited by
                  #8

                  Sorry but what do you mean by adjusting the logic? The only thing this class does or add can be seen in my initial post. Does adding stretching/spacing mess with the coordinates? I appreciate your effort though.

                  mrjjM 1 Reply Last reply
                  0
                  • I Infestor

                    Sorry but what do you mean by adjusting the logic? The only thing this class does or add can be seen in my initial post. Does adding stretching/spacing mess with the coordinates? I appreciate your effort though.

                    mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @Infestor
                    Nope, if the widget is shown(visible) it should always be correct.

                    By adjusting logic i mean if you fixed the offset before and then move code, then
                    that part would no longer be needed.

                    So if you print out your mouse_x and mouse_y what do they show ?

                    1 Reply Last reply
                    1
                    • I Offline
                      I Offline
                      Infestor
                      wrote on last edited by
                      #10

                      The top left corner of the image starts at 10, 10. It generally seems like that there is a ~10 pixel wide border around the image. And no the image doesnt have that border so thats not the problem.

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

                        Did you set the label size to the size of the pixmap? I don't see this so the label may be bigger than the pixmap. Also you're leaking the QPixmap instance - no need to create a pointer here.

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

                        1 Reply Last reply
                        2
                        • mrjjM Offline
                          mrjjM Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

                          Hi
                          Also notice the layout_ has a margin of 9 pr default.

                          layout_->setContentsMargins(0,0,0,0); to be sure its not that.

                          1 Reply Last reply
                          2
                          • I Offline
                            I Offline
                            Infestor
                            wrote on last edited by Infestor
                            #13

                            Edit: @mrjj Thank you so much, that was the issue. How would you know that though...

                            Also how am i leaking pixmap memory? I free the pixmap in the destructor which isnt shown here.

                            mrjjM 1 Reply Last reply
                            0
                            • I Infestor

                              Edit: @mrjj Thank you so much, that was the issue. How would you know that though...

                              Also how am i leaking pixmap memory? I free the pixmap in the destructor which isnt shown here.

                              mrjjM Offline
                              mrjjM Offline
                              mrjj
                              Lifetime Qt Champion
                              wrote on last edited by
                              #14

                              @Infestor
                              Super
                              well i have had other cases with mysterious gaps and your "~10 pixel wide border " reminded me.

                              Well then you dont leak it then but you also don't need to as QPixmap is meant to be copied (and its cheap)

                              QPixmap p("minimap.png");
                              l->setPixmap(p);

                              is better as then we cant forget to delete the pixmap and setPixmap copies it anyway.

                              When ever you need to use * with Qt classes you give to other Qt class, its a sign you shouldn't need to new it.

                              1 Reply Last reply
                              1

                              • Login

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