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

2024-09-25 18:27:36 发布

您现在位置:Python中文网/ 问答频道 /正文

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_())

我需要帮助在arcTo功能的Qpainterpath。我已经添加了图像的painterpath代码,我在这里提到。你知道吗

在第一幅图中,我以逆时针方向绘制了pathitem。 在第二幅图中,我以顺时针方向绘制了pathitem,它看起来像是扭曲的。你知道吗

我试着用负片改变arcTo中提到的角度签字。但是它并不像第一张图中那样给出完美的物品。你知道吗

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


Tags: selfeventifdefsceneqtgridgradient
1条回答
网友
1楼 · 发布于 2024-09-25 18:27:36

你可以试试这样的。我添加了一些注释来说明第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)

相关问题 更多 >