繁体   English   中英

在顺时针方向获得正确形状的正确角度是多少

[英]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:
                # pass
                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):
        # 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("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_())

我需要 Qpainterpath 的 arcTo 功能的帮助。我添加了我在此处提到的 Painterpath 代码的图像。

在第一张图片中,我以逆时针方向绘制了路径项。 在第二张图片中,我以顺时针方向绘制了路径项,它看起来像是扭曲的。

我已经尝试通过使用负号更改 arcTo 中提到的角度。但它并没有像第一张图片那样给出完美的项目。

在顺时针方向获得正确形状的正确角度是多少

你可以尝试这样的事情。 我添加了注释以表明第 2 项中的元素与第 1 项中的元素有何关联。

第 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)

第 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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM