Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9217221
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:35:27+00:00 2026-06-18T02:35:27+00:00

I was trying to build a small UI using PyQt . It has a

  • 0

I was trying to build a small UI using PyQt .

It has a window , a button(Rotate) , and a polygon(rectangle) in a QGraphicsView. One aim of
the app is to allow the user to rotate the polygon. That is after the
button is clicked and the user clicks at a point , the nearest vertex
automatically shifts or tilts towards the user click. Ive also set the
polygon to be movable before the click and not movable after the click.

The problem is if the user moves the polygon and then clicks , the polygon
rotates in a weird manner. Could someone help me figure out the error? My guess is that it might be with the setTransformOriginPoint.

EDIT:
I have two classes inherited from QtGui.QWidget and QtGui.QGraphicsScene.

class Window(QtGui.QWidget):
    def polychange(self , sender): //Called by the button , 
        if sender:
                self.view.polyrotate = 1 //self.view is an instance of QGraphicsScene class
                self.view.polyf.setFlag(QtGui.QGraphicsItem.ItemIsMovable , False)

        else:
                self.view.polyrotate = 0 
                self.view.degrees = 0
                self.view.polyf.setFlag(QtGui.QGraphicsItem.ItemIsMovable)

class Example(QtGui.QGraphicsView):
    def mousePressEvent(self , e):
        super(Example , self).mousePressEvent(e)
        self.x = e.x()
        self.y = e.y()
        if self.polyrotate == 1:
            self.Rotate()

    def Rotate(self):

        self.pverticesx = []
        self.pverticesy = []
        distances = []
        for i in range(4):
            self.pverticesx.append(self.polyf.mapToScene(self.polyf.polygon()[i]).x())
            self.pverticesy.append(self.polyf.mapToScene(self.polyf.polygon()[i]).y())

        x1 = self.x
        y1 = self.y      

        for i in range(4):
             distance = math.hypot(self.pverticesx[i] - x1 , self.pverticesy[i] - y1)
             distances.append(distance)
        midpointx = (self.pverticesx[0] +  self.pverticesx[2]) / 2
        midpointy = (self.pverticesy[0] +  self.pverticesy[2]) / 2
        index = distances.index(min(distances))          
        pointx = self.pverticesx[index]                
        pointy = self.pverticesy[index]
        vector1 = [x1 - midpointx , y1 - midpointy]
        vector2 = [pointx - midpointx , pointy - midpointy]
        num = 0

        for i in [0 , 1]:
            num = num + (vector1[i] * vector2[i])  
        den = math.sqrt(sum(map(lambda x : x * x , vector1))) *  math.sqrt(sum(map(lambda x : x * x , vector2)))

        degree = math.degrees(math.acos(num / den))
        self.degrees = degree + self.degrees
        if self.degrees > 360:
            rotation = self.degrees / 360
            self.degrees = self.degrees - (rotation * 360)

        self.polyf.setTransformOriginPoint(midpointx , midpointy)
        self.polyf.setRotation(self.degrees)

Here is a more exhaustive link to my code. RotateApp . Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T02:35:28+00:00Added an answer on June 18, 2026 at 2:35 am

    I’ve found out the answer to my own question. Before rotating , midpointx and midpointy are in scene coordinates. So I had to map them back to QGraphicsItem coordinates system. This line before self.setRotation() would do the trick.

    self.polyf.setTransformOriginPoint(self.polyf.mapFromScene(QtCore.QPointF(midpointx , midpointy)))
    

    Now my polygon doesnt move randomnly.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to build a small testing app with erlang+mnesia. I have a user
I'm trying to build a small webmail app. When I read all the emails
I am trying to build a small table using NSString. I cannot seem to
I'm trying to build small web-system (url shortcutting) using nonsql Cassandra DB, the problem
G'day guys, trying to build a small invoicing system (that can generate PDF's using
I'm trying to build a small app which allows downloading of multiple files at
I am trying to build a small app for a college with WPF, it
I'm trying to build a small certificate authority using bouncycastle. The certificate is generated
I'm trying to build a small Java app for connecting to an application called
Well I am trying to build a few small windows applications using MFC and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.