什么角度会得到正确的顺时针方向的正确形状?

huangapple go评论84阅读模式
英文:

What will be the correct angle to get the correct shape in clockwise direction

问题

以下是您提供的代码的翻译部分:

from PyQt5.QtCore import (QByteArray,QDataStream, QIODevice,pyqtSlot, QMimeData, QPointF, QPoint, Qt, QRect,QTimer,QLineF, QEvent,QRectF)
from PyQt5.QtGui import QColor,QDrag, QPainter, QPixmap,QFont,QFontMetrics,QBrush, QLinearGradient, QIcon, QPen, QPainterPath, QTransform,QCursor,QMouseEvent,QClipboard
from PyQt5.QtWidgets import QApplication,QGraphicsTextItem,QStyleOptionGraphicsItem,QStyle,QGraphicsItemGroup,QErrorMessage, QSizePolicy,QShortcut, QScrollArea, QPushButton,QLineEdit, QMainWindow,QInputDialog, QGraphicsPathItem,QDialog, QVBoxLayout,QGraphicsItem,QStatusBar,QTextEdit, QAction,QMenu, qApp,QSplitter, QButtonGroup, QToolButton, QFrame, QHBoxLayout, QGraphicsView, QGraphicsItem, QGraphicsPixmapItem, QLabel, QGraphicsScene, QWidget
import importlib

class GraphicsSceneClass(QGraphicsScene):
    global selectedObjType
    def __init__(self, parent=None):
        super(GraphicsSceneClass, self).__init__(parent)
        self.gridOn = 0
        self.setSceneRect(0, 0, 1920, 1080)
        self.setItemIndexMethod(QGraphicsScene.NoIndex)
        self.setBackgroundBrush(QBrush(Qt.black))

    def mousePressEvent(self, event):
            sampleTransform = QTransform()
            objectAtMouse = self.itemAt(event.scenePos(), sampleTransform)

            if objectAtMouse and event.button()== Qt.LeftButton:
                objectAtMouse.setSelected(True)
                pass
            elif objectAtMouse==None and event.button()==Qt.RightButton:
                self.grid = self.TargPosForLine(event.scenePos(), "ForLine")
            self.grid = self.TargPosForLine(event.scenePos(), "ForLine")
            opt=QStyleOptionGraphicsItem()
            opt.State=QStyle.State_None
            painter=QPainter()
            painter.setPen(Qt.NoPen)
            painter.setBrush(Qt.green)
            painter.drawRect(self.grid.x(),self.grid.y(),16,16)
    def TargPosForLine(self, position, mode):

        clicked_column = int((position.y() // 16)) * 16
        clicked_row = int((position.x() // 16)) * 16
        if clicked_column < 0:
            clicked_column = 0
        if clicked_row < 0:
            clicked_row = 0
        if(mode == "ForRect"):
            return QRect(clicked_row, clicked_column,16,16)
        elif(mode == "ForLine"):
            return QPointF(clicked_row,clicked_column)

    def mouseReleaseEvent(self, event):
        pass

class MainWindow(QMainWindow):
    global selectedObjType
    def __init__(self,):
        super(MainWindow, self).__init__()
        self.createToolbars()

        self.scene = GraphicsSceneClass()
        MainWindow.obj = self.scene
        self.view = QGraphicsView(self.scene)
        self.view.setMouseTracking(True)
        self.view.setRenderHint(QPainter.HighQualityAntialiasing)
        self.widg = QWidget()
        self.horizontalLayout = QHBoxLayout()
        self.horizontalLayout.addWidget(self.view)
        self.widg.setMouseTracking(True)
        self.widget = QWidget()
        self.widget.setLayout(self.horizontalLayout)
        self.setCentralWidget(self.widget)
        self.obj=None

    def contextMenuEvent(self, event):
        contextMenu = QMenu(self)

        Cutaction = contextMenu.addAction("Cut")
        Coaction = contextMenu.addAction("Copy")
        Paaction = contextMenu.addAction("Paste")
        Propaction = contextMenu.addAction("draw1")
        Propaction1=contextMenu.addAction("draw2")
        quitAct = contextMenu.addAction("quit")
        action = contextMenu.exec_(self.mapToGlobal(event.pos()))
        if action == quitAct:
            self.close()

        elif action == Propaction:
            objectDrop = QGraphicsPathItem()
            painterPath = QPainterPath()
            painterPath.moveTo(10, 6)
            painterPath.lineTo(10 + 44.3479, 6)
            painterPath.arcTo(10 + 44.3479 - 6, 6 - 4, 4, 4, 270, 90)
            painterPath.lineTo(10 + 44.3479 - 2, 6 - 4)
            painterPath.arcTo(10 + 44.3479 - 6, 6 - 6, 4, 4, 0, 90)
            painterPath.lineTo(10, 0)
            gradient = QLinearGradient(1, 1, 1, 5)
            gradient.setColorAt(0, QColor(Qt.gray))
            gradient.setColorAt(0.5, QColor(192, 192, 192, 255))
            gradient.setColorAt(1, QColor(Qt.darkGray))
            painterPath.closeSubpath()
            objectDrop.setPos(self.scene.grid)
            objectDrop.setPen(QPen(Qt.NoPen))
            objectDrop.setPath(painterPath)
            objectDrop.setBrush(QBrush(gradient))
            objectDrop._position = QPointF(self.scene.grid)
            print("line", self.scene.grid)
            objectDrop.setFlag(QGraphicsItem.ItemIsSelectable)
            objectDrop.setFlag(QGraphicsItem.ItemIsMovable)
            objectDrop._type1 = "line"
            self.scene.addItem(objectDrop)
            print(objectDrop)
        elif action==Propaction1:
            objectDrop = QGraphicsPathItem()
            painterPath = QPainterPath()
            painterPath.moveTo(10, 0)
            painterPath.lineTo(10 + 44.3479, 0)
            painterPath.arcTo(10 + 44.3479 - 6, 0 - 4, 4, 4, 270, 90)
            painterPath.lineTo(10 + 44.3479 - 2, 0 - 4)
            painterPath.arcTo(10 + 44.3479 - 6, 0 - 6, 4, 4, 0, 90)
            painterPath.lineTo(10, 6)
            gradient = QLinearGradient(1, 1, 1, 5)
            gradient.setColorAt(0, QColor(Qt.gray))
            gradient.setColorAt(0.5, QColor(192, 192, 192, 255))
            gradient.setColorAt(1, QColor(Qt.darkGray))
            painterPath.closeSubpath()
            objectDrop.setPos(self.scene.grid)
            objectDrop.setPen(QPen(Qt.NoPen))
            objectDrop.setPath(painterPath)
            objectDrop.setBrush(QBrush(gradient))
            objectDrop._position=QPointF(self.scene.grid)
            print("line",self.scene.grid)
            objectDrop.setFlag(QGraphicsItem.ItemIsSelectable)
            objectDrop.setFlag(QGraphicsItem.ItemIsMovable)
            objectDrop._type1 = "line"
            self.scene.addItem(objectDrop)
            print(objectDrop)

if __name__=="__main__":
    import sys
    app=QApplication(sys.argv)
    mainWindow = MainWindow()

    mainWindow.show()
    sys.exit(app.exec_())

请注意,这是您提供的代码的翻译部分,我没有翻译代码中

英文:

什么角度会得到正确的顺时针方向的正确形状?

什么角度会得到正确的顺时针方向的正确形状?

from PyQt5.QtCore import (QByteArray,QDataStream, QIODevice,pyqtSlot, QMimeData, QPointF, QPoint, Qt, QRect,QTimer,QLineF, QEvent,QRectF)
from PyQt5.QtGui import QColor,QDrag, QPainter, QPixmap,QFont,QFontMetrics,QBrush, QLinearGradient, QIcon, QPen, QPainterPath, QTransform,QCursor,QMouseEvent,QClipboard
from PyQt5.QtWidgets import QApplication,QGraphicsTextItem,QStyleOptionGraphicsItem,QStyle,QGraphicsItemGroup,QErrorMessage, QSizePolicy,QShortcut, QScrollArea, QPushButton,QLineEdit, QMainWindow,QInputDialog, QGraphicsPathItem,QDialog, QVBoxLayout,QGraphicsItem,QStatusBar,QTextEdit, QAction,QMenu, qApp,QSplitter, QButtonGroup, QToolButton, QFrame, QHBoxLayout, QGraphicsView, QGraphicsItem, QGraphicsPixmapItem, QLabel, QGraphicsScene, QWidget
import importlib
class GraphicsSceneClass(QGraphicsScene):
global selectedObjType
def __init__(self, parent=None):
super(GraphicsSceneClass, self).__init__(parent)
self.gridOn = 0
self.setSceneRect(0, 0, 1920, 1080)
self.setItemIndexMethod(QGraphicsScene.NoIndex)
self.setBackgroundBrush(QBrush(Qt.black))
def mousePressEvent(self, event):
sampleTransform = QTransform()
objectAtMouse = self.itemAt(event.scenePos(), sampleTransform)
if objectAtMouse and event.button()== Qt.LeftButton:
objectAtMouse.setSelected(True)
pass
elif objectAtMouse==None and event.button()==Qt.RightButton:
# pass
self.grid = self.TargPosForLine(event.scenePos(), &quot;ForLine&quot;)
self.grid = self.TargPosForLine(event.scenePos(), &quot;ForLine&quot;)
opt=QStyleOptionGraphicsItem()
opt.State=QStyle.State_None
painter=QPainter()
painter.setPen(Qt.NoPen)
painter.setBrush(Qt.green)
painter.drawRect(self.grid.x(),self.grid.y(),16,16)
def TargPosForLine(self, position, mode):
clicked_column = int((position.y() // 16)) * 16
clicked_row = int((position.x() // 16)) * 16
if clicked_column &lt; 0:
clicked_column = 0
if clicked_row &lt; 0:
clicked_row = 0
if(mode == &quot;ForRect&quot;):
return QRect(clicked_row, clicked_column,16,16)
elif(mode == &quot;ForLine&quot;):
return QPointF(clicked_row,clicked_column)
def mouseReleaseEvent(self, event):
# self.DeselectItems()
pass
class MainWindow(QMainWindow):
global selectedObjType
# global item
def __init__(self,):
super(MainWindow, self).__init__()
self.createToolbars()
self.scene = GraphicsSceneClass()
MainWindow.obj = self.scene
self.view = QGraphicsView(self.scene)
# self.view.setDragMode(QGraphicsView.RubberBandDrag)
self.view.setMouseTracking(True)
self.view.setRenderHint(QPainter.HighQualityAntialiasing)
self.widg = QWidget()
self.horizontalLayout = QHBoxLayout()
self.horizontalLayout.addWidget(self.view)
self.widg.setMouseTracking(True)
self.widget = QWidget()
self.widget.setLayout(self.horizontalLayout)
self.setCentralWidget(self.widget)
self.obj=None
#
def contextMenuEvent(self, event):
contextMenu = QMenu(self)
Cutaction = contextMenu.addAction(&quot;Cut&quot;)
Coaction = contextMenu.addAction(&quot;Copy&quot;)
Paaction = contextMenu.addAction(&quot;Paste&quot;)
Propaction = contextMenu.addAction(&quot;draw1&quot;)
Propaction1=contextMenu.addAction(&quot;draw2&quot;)
quitAct = contextMenu.addAction(&quot;quit&quot;)
action = contextMenu.exec_(self.mapToGlobal(event.pos()))
if action == quitAct:
self.close()
elif action == Propaction:
objectDrop = QGraphicsPathItem()
painterPath = QPainterPath()
painterPath.moveTo(10, 6)
painterPath.lineTo(10 + 44.3479, 6)
painterPath.arcTo(10 + 44.3479 - 6, 6 - 4, 4, 4, 270, 90)
painterPath.lineTo(10 + 44.3479 - 2, 6 - 4)
painterPath.arcTo(10 + 44.3479 - 6, 6 - 6, 4, 4, 0, 90)
painterPath.lineTo(10, 0)
gradient = QLinearGradient(1, 1, 1, 5)
gradient.setColorAt(0, QColor(Qt.gray))
gradient.setColorAt(0.5, QColor(192, 192, 192, 255))
gradient.setColorAt(1, QColor(Qt.darkGray))
painterPath.closeSubpath()
objectDrop.setPos(self.scene.grid)
objectDrop.setPen(QPen(Qt.NoPen))
objectDrop.setPath(painterPath)
objectDrop.setBrush(QBrush(gradient))
objectDrop._position = QPointF(self.scene.grid)
print(&quot;line&quot;, self.scene.grid)
objectDrop.setFlag(QGraphicsItem.ItemIsSelectable)
objectDrop.setFlag(QGraphicsItem.ItemIsMovable)
objectDrop._type1 = &quot;line&quot;
self.scene.addItem(objectDrop)
print(objectDrop)
elif action==Propaction1:
objectDrop = QGraphicsPathItem()
painterPath = QPainterPath()
painterPath.moveTo(10, 0)
painterPath.lineTo(10 + 44.3479, 0)
painterPath.arcTo(10 + 44.3479 - 6, 0 - 4, 4, 4, 270, 90)
painterPath.lineTo(10 + 44.3479 - 2, 0 - 4)
painterPath.arcTo(10 + 44.3479 - 6, 0 - 6, 4, 4, 0, 90)
painterPath.lineTo(10, 6)
gradient = QLinearGradient(1, 1, 1, 5)
gradient.setColorAt(0, QColor(Qt.gray))
gradient.setColorAt(0.5, QColor(192, 192, 192, 255))
gradient.setColorAt(1, QColor(Qt.darkGray))
painterPath.closeSubpath()
objectDrop.setPos(self.scene.grid)
objectDrop.setPen(QPen(Qt.NoPen))
objectDrop.setPath(painterPath)
objectDrop.setBrush(QBrush(gradient))
objectDrop._position=QPointF(self.scene.grid)
print(&quot;line&quot;,self.scene.grid)
objectDrop.setFlag(QGraphicsItem.ItemIsSelectable)
objectDrop.setFlag(QGraphicsItem.ItemIsMovable)
objectDrop._type1 = &quot;line&quot;
self.scene.addItem(objectDrop)
print(objectDrop)
if __name__==&quot;__main__&quot;:
import sys
app=QApplication(sys.argv)
mainWindow = MainWindow()
mainWindow.show()        
sys.exit(app.exec_())

I need help in arcTo function of Qpainterpath.I have added the images of the painterpath code which i have mentioned here.

In first image i have drawn the pathitem in counter clockwise direction.
In second image I have drawn the pathitem in clockwise direction and it looks like twisted one.

I have tried by changing the angle mentioned in arcTo with negative sign.But it is not giving the perfect item like in first image.

What will be the correct angle to get the correct shape in clockwise direction

答案1

得分: 0

你可以尝试类似以下的方式。我已添加注释以指示项目2中的元素与项目1中的元素相关联。

项目1:

painterPath.moveTo(10, 6)
# 线1
painterPath.lineTo(10 + 44.3479, 6)
# 弧1逆时针
painterPath.arcTo(10 + 44.3479 - 6, 6 - 4, 4, 4, 270, 90)
# 线2
painterPath.lineTo(10 + 44.3479 - 2, 6 - 4)
# 弧2逆时针
painterPath.arcTo(10 + 44.3479 - 6, 6 - 6, 4, 4, 0, 90) 
# 线3
painterPath.lineTo(10, 0)

项目2:

painterPath.moveTo(10, 0)
# 线3
painterPath.lineTo(10 + 44.3479, 0)
# 弧2顺时针
painterPath.arcTo(10 + 44.3479 - 6, 6 - 6, 4, 4, 90, -90)
# 线2
painterPath.lineTo(10 + 44.3479 - 2, 6 - 4)
# 弧1顺时针
painterPath.arcTo(10 + 44.3479 - 6, 6 - 4, 4, 4, 360, -90)
# 线1
painterPath.lineTo(10, 6)
英文:

You could try something like this. I've added comments to indicate how the elements in item 2 relate to those in item 1.

Item 1:

painterPath.moveTo(10, 6)
# line 1
painterPath.lineTo(10 + 44.3479, 6)
# arc 1 counter clockwise
painterPath.arcTo(10 + 44.3479 - 6, 6 - 4, 4, 4, 270, 90)
# line 2
painterPath.lineTo(10 + 44.3479 - 2, 6 - 4)
# arc 2 counter clockwise
painterPath.arcTo(10 + 44.3479 - 6, 6 - 6, 4, 4, 0, 90) 
# line 3
painterPath.lineTo(10, 0)

Item 2:

painterPath.moveTo(10, 0)
# line 3
painterPath.lineTo(10 + 44.3479, 0)
# arc 2 clockwise
painterPath.arcTo(10 + 44.3479 - 6, 6 - 6, 4, 4, 90, -90)
# line 2
painterPath.lineTo(10 + 44.3479 - 2, 6 - 4)
# arc 1 clockwise
painterPath.arcTo(10 + 44.3479 - 6, 6 - 4, 4, 4, 360, -90)
# line 1
painterPath.lineTo(10, 6)

huangapple
  • 本文由 发表于 2020年1月3日 18:19:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/59576792.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定