已创建一组手柄,可用于调整形状的大小。 通过跟踪手柄位置的变化,可以计算形状的新几何形状。
我不能在场景中添加多个对象。
$text-color: #373737;
$gray-base: #494949;
$gray-darker: #222;
$gray-dark: #333;
$gray: #555;
$gray-light: #777;
$gray-lighter: #eee;
$brand-primary: #1f9eba;
$brand-success: #59ba1f;
$brand-info: #5bc0de;
$brand-warning: #d1bd10;
$brand-danger: #ba1f1f;
/* Timeline */
.timeline {
list-style: none;
padding: 20px;
position: relative;
&:before {
top: 40px;
bottom: 0;
position: absolute;
content: " ";
width: 3px;
background-color: #eeeeee;
left: 50%;
margin-left: -1.5px;
}
.timeline-item {
margin-bottom: 20px;
position: relative;
&:before,
&:after {
content: "";
display: table;
}
&:after {
clear: both;
}
.timeline-badge {
color: #fff;
width: 54px;
height: 54px;
line-height: 52px;
font-size: 22px;
text-align: center;
position: absolute;
top: 18px;
left: 50%;
margin-left: -25px;
background-color: $gray-dark;
border: 3px solid #ffffff;
z-index: 100;
border-top-right-radius: 50%;
border-top-left-radius: 50%;
border-bottom-right-radius: 50%;
border-bottom-left-radius: 50%;
i,
.fa,
.glyphicon {
top: 2px;
left: 0px;
}
&.primary {
background-color: $brand-primary;
}
&.info {
background-color: $brand-info;
}
&.success {
background-color: $brand-success;
}
&.warning {
background-color: $brand-warning;
}
&.danger {
background-color: $brand-danger;
}
}
.timeline-panel {
position: relative;
text-align: right;
width: 46%;
float: left;
right: 16px;
// border: 1px solid $gray-light;
background: #ffffff;
border-radius: 2px;
padding: 20px;
-webkit-box-shadow: 0 1px 6px rgba(0, 0, 0, 0.175);
box-shadow: 0 1px 6px rgba(0, 0, 0, 0.175);
&:before {
position: absolute;
top: 26px;
right: -16px;
display: inline-block;
border-top: 16px solid transparent;
border-left: 16px solid $gray-light;
border-right: 0 solid $gray-light;
border-bottom: 16px solid transparent;
// content: " ";
}
.timeline-title {
margin-top: 0;
color: inherit;
}
.timeline-body > p,
.timeline-body > ul {
margin-bottom: 0;
}
.timeline-body > p + p {
margin-top: 5px;
}
}
}
.timeline-panel-right {
position: relative;
text-align: left;
width: 46%;
float: right;
left: 16px;
background: #ffffff;
border-radius: 2px;
padding: 20px;
-webkit-box-shadow: 0 1px 6px rgba(0, 0, 0, 0.175);
box-shadow: 0 1px 6px rgba(0, 0, 0, 0.175);
}
.timeline-item:nth-child(even) {
.timeline-panel {
float: right;
text-align: left;
left: 16px;
&:before {
border-left-width: 0;
border-right-width: 14px;
left: -14px;
right: auto;
}
}
.timeline-panel-right {
float: left;
text-align: right;
right: 16px;
left: auto;
}
}
}
我希望场景中会定位几个对象(矩形),并且每个对象都可以自由移动。
答案 0 :(得分:2)
您仅添加了一个GraphicsRectItem,所以您仅看到一个项目,如果要查看更多项目,则必须创建更多GraphicsRectItem并使用addItem将它们添加到场景中。
# ...
import random
def main():
app = QApplication(sys.argv)
grview = QGraphicsView()
scene = QGraphicsScene(grview)
scene.setSceneRect(0, 0, 680, 459)
grview.setScene(scene)
for _ in range(10):
item = GraphicsRectItem(0, 0, 300, 150)
scene.addItem(item)
item.setPos(QPointF(*random.sample(range(300), 2)))
grview.fitInView(scene.sceneRect(), Qt.KeepAspectRatio)
grview.show()
sys.exit(app.exec_())
# ...
更新:
import sys
from PyQt5.QtCore import Qt, QRectF, QPointF, pyqtSlot
from PyQt5.QtGui import QBrush, QPainterPath, QPainter, QColor, QPen, QPixmap
from PyQt5.QtWidgets import (
QGraphicsRectItem,
QApplication,
QGraphicsView,
QGraphicsScene,
QGraphicsItem,
QPushButton,
QWidget,
QVBoxLayout,
)
class GraphicsRectItem(QGraphicsRectItem):
# ...
class Widget(QWidget):
def __init__(self, parent=None):
super(Widget, self).__init__(parent)
button = QPushButton("Add Rectangle", clicked=self.on_clicked)
self.grview = QGraphicsView()
scene = QGraphicsScene(self.grview)
scene.setSceneRect(0, 0, 680, 459)
self.grview.setScene(scene)
self.grview.fitInView(scene.sceneRect(), Qt.KeepAspectRatio)
lay = QVBoxLayout(self)
lay.addWidget(button)
lay.addWidget(self.grview)
@pyqtSlot()
def on_clicked(self):
item = GraphicsRectItem(0, 0, 300, 150)
item.setPos(QPointF(*random.sample(range(300), 2)))
self.grview.scene().addItem(item)
import random
def main():
app = QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()