- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在网上搜索了解决方案,其中大多数都是 C++ 语言,而不是完整的答案。我想知道是否有人可以帮助我找出如何删除 qgraphicsitem 周围的选择矩形,如下图所示?
import sys
from PySide.QtGui import *
from PySide.QtCore import *
import random
class MyGraphicsView(QGraphicsView):
def __init__(self):
super(MyGraphicsView, self).__init__()
self.setDragMode(QGraphicsView.RubberBandDrag)
self._isPanning = False
self._mousePressed = False
# self.setBackgroundBrush(QImage("C:/Users/jmartini/Desktop/Temp/images/flag_0140.jpg"))
self.setCacheMode(QGraphicsView.CacheBackground)
self.setHorizontalScrollBarPolicy( Qt.ScrollBarAlwaysOff )
self.setVerticalScrollBarPolicy( Qt.ScrollBarAlwaysOff )
def mousePressEvent(self, event):
if event.button() == Qt.LeftButton:
self._mousePressed = True
if self._isPanning:
self.setCursor(Qt.ClosedHandCursor)
self._dragPos = event.pos()
event.accept()
else:
super(MyGraphicsView, self).mousePressEvent(event)
elif event.button() == Qt.MiddleButton:
self._mousePressed = True
self._isPanning = True
self.setCursor(Qt.ClosedHandCursor)
self._dragPos = event.pos()
event.accept()
def mouseMoveEvent(self, event):
if self._mousePressed and self._isPanning:
newPos = event.pos()
diff = newPos - self._dragPos
self._dragPos = newPos
self.horizontalScrollBar().setValue(self.horizontalScrollBar().value() - diff.x())
self.verticalScrollBar().setValue(self.verticalScrollBar().value() - diff.y())
event.accept()
else:
super(MyGraphicsView, self).mouseMoveEvent(event)
def mouseReleaseEvent(self, event):
if event.button() == Qt.LeftButton:
if self._isPanning:
self.setCursor(Qt.OpenHandCursor)
else:
self._isPanning = False
self.setCursor(Qt.ArrowCursor)
self._mousePressed = False
elif event.button() == Qt.MiddleButton:
self._isPanning = False
self.setCursor(Qt.ArrowCursor)
self._mousePressed = False
super(MyGraphicsView, self).mouseReleaseEvent(event)
def mouseDoubleClickEvent(self, event):
self.fitInView(self.sceneRect(), Qt.KeepAspectRatio)
pass
def keyPressEvent(self, event):
if event.key() == Qt.Key_Space and not self._mousePressed:
self._isPanning = True
self.setCursor(Qt.OpenHandCursor)
else:
super(MyGraphicsView, self).keyPressEvent(event)
def keyReleaseEvent(self, event):
if event.key() == Qt.Key_Space:
if not self._mousePressed:
self._isPanning = False
self.setCursor(Qt.ArrowCursor)
else:
super(MyGraphicsView, self).keyPressEvent(event)
def wheelEvent(self, event):
# zoom factor
factor = 1.25
# Set Anchors
self.setTransformationAnchor(QGraphicsView.NoAnchor)
self.setResizeAnchor(QGraphicsView.NoAnchor)
# Save the scene pos
oldPos = self.mapToScene(event.pos())
# Zoom
if event.delta() < 0:
factor = 1.0 / factor
self.scale(factor, factor)
# Get the new position
newPos = self.mapToScene(event.pos())
# Move scene to old position
delta = newPos - oldPos
self.translate(delta.x(), delta.y())
class MyGraphicsScene(QGraphicsScene):
def __init__(self, parent):
super(MyGraphicsScene, self).__init__()
self.setBackgroundBrush(QBrush(QColor(50,50,50)))
# self.setSceneRect(50,50,0,0)
class MyMainWindow(QMainWindow):
def __init__(self):
super(MyMainWindow, self).__init__()
self.setWindowTitle("Test")
self.resize(800,600)
self.gv = MyGraphicsView()
self.gv.setScene(MyGraphicsScene(self))
self.setCentralWidget(self.gv)
self.gv.scene().selectionChanged.connect(self.selection_changed)
self.populate()
def populate(self):
scene = self.gv.scene()
for i in range(300):
x = random.randint(-1000, 1000)
y = random.randint(-1000, 1000)
r = random.randint(1, 50)
rect = scene.addEllipse(x, y, r, r,
QPen(QColor(255,128,0), 0.5, Qt.SolidLine, Qt.RoundCap, Qt.RoundJoin),
QBrush(QColor(255,128,20,128)))
rect.setFlag( QGraphicsItem.ItemIsSelectable )
rect.setFlag( QGraphicsItem.ItemIsMovable )
rect = scene.addEllipse(10, 20, 20, 20,
QPen(QColor(255,128,0), 0.5, Qt.SolidLine, Qt.RoundCap, Qt.RoundJoin),
QBrush(QColor(255,0,0,128)))
rect.setFlag( QGraphicsItem.ItemIsSelectable )
rect.setFlag( QGraphicsItem.ItemIsMovable )
points = QPolygonF()
points.append(QPointF(10.4, 10.5))
points.append(QPointF(40.2, 60.2))
points.append(QPointF(30.2, 90.2))
points.append(QPointF(10.2, 80.2))
poly = scene.addPolygon(points,
QPen(QColor(255,128,0), 0.5, Qt.SolidLine, Qt.RoundCap, Qt.RoundJoin),
QBrush(QColor(255,0,0,128)))
poly.setFlag( QGraphicsItem.ItemIsSelectable )
poly.setFlag( QGraphicsItem.ItemIsMovable )
def selection_changed(self):
selection = self.gv.scene().selectedItems()
print 'Selected:', len(selection)
for i in selection:
i.setPen(QPen(QColor(255,255,255), 0.5, Qt.SolidLine, Qt.RoundCap, Qt.RoundJoin))
def main():
app = QApplication(sys.argv)
ex = MyMainWindow()
ex.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
最佳答案
默认项在选中时会绘制boundingRect()
,解决方案是覆盖paint(...)
方法:
class EllipseItem(QGraphicsEllipseItem):
def paint(self, painter, option, widget):
option.state &= ~QStyle.State_Selected
super(EllipseItem, self).paint(painter, option, widget)
class PolygonItem(QGraphicsPolygonItem):
def paint(self, painter, option, widget):
option.state &= ~QStyle.State_Selected
super(PolygonItem, self).paint(painter, option, widget)
<小时/>
所以现在您必须在代码中使用它:
def populate(self):
scene = self.gv.scene()
for i in range(300):
x = random.randint(-1000, 1000)
y = random.randint(-1000, 1000)
r = random.randint(1, 50)
rect = EllipseItem(x, y, r, r)
rect.setPen(QPen(QColor(255,128,0), 0.5, Qt.SolidLine, Qt.RoundCap, Qt.RoundJoin))
rect.setBrush(QBrush(QColor(255,128,20,128)))
scene.addItem(rect)
rect.setFlag( QGraphicsItem.ItemIsSelectable )
rect.setFlag( QGraphicsItem.ItemIsMovable )
rect = EllipseItem(10, 20, 20, 20)
rect.setPen(QPen(QColor(255,128,0), 0.5, Qt.SolidLine, Qt.RoundCap, Qt.RoundJoin))
rect.setBrush(QBrush(QColor(255,0,0,128)))
rect.setFlag( QGraphicsItem.ItemIsSelectable )
rect.setFlag( QGraphicsItem.ItemIsMovable )
scene.addItem(rect)
points = QPolygonF()
points.append(QPointF(10.4, 10.5))
points.append(QPointF(40.2, 60.2))
points.append(QPointF(30.2, 90.2))
points.append(QPointF(10.2, 80.2))
poly = PolygonItem(points)
poly.setPen(QPen(QColor(255,128,0), 0.5, Qt.SolidLine, Qt.RoundCap, Qt.RoundJoin))
poly.setBrush(QBrush(QColor(255,0,0,128)))
poly.setFlag( QGraphicsItem.ItemIsSelectable )
poly.setFlag( QGraphicsItem.ItemIsMovable )
scene.addItem(poly)
关于python - 删除 QGraphicsItem 周围的虚线选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51989790/
我在 Qt 中遇到了帖子标题中描述的错误。 我有一个类调用“ball”,它继承类调用“tableItem”,后者继承了 QGraphicsItem。我正在尝试使用原型(prototype)设计模式,因
我想让 QGraphicsScene 中的一个 QGraphicsItem 在另一个移动时移动(或改变大小)。但是,尝试从 QGraphicsScene 对象访问任一 QGraphicsItem 会导
这个问题是关于:Forcing QGraphicsItem To Stay Put 我想在场景中四处移动时在固定位置放置一个QGraphicsItem。 建议的解决方案是覆盖子类 QGraphicsV
我想我的问题与此类似post但在 C++ 中和在 QGraphicsItem 中。 我想将对象的可移动区域固定在另一个 QGraphicsItem 中。如果我试图将它移到外面,我希望我的对象留在里面。
总结 我在SUSE 64位下使用qt 4.8.7 我有 2 个具有不同刷新率的 QGraphicsItem。但是当我对其中一个调用“update()”时,对它们两个都调用了“paint()”。所以两个
我从 OOAD 类(class)中了解到 dynamic_cast 是一个糟糕的设计但我不知道在 Qt 中没有 dynamic_cast 我怎么能做我想做的事因为我不能只在 QGraphicsItem
我有一个 QGraphicsItem 的子类,我想通过“Control+LMB click”将它的实例添加到场景中。问题是项目被添加到坐标比它们应该大两倍的位置。同时使用 scene.addEllip
我有一个自定义 QGraphicsItem 实现。我需要能够限制可以移动项目的位置 - 即将其限制在某个区域。当我检查 Qt 文档时,这是它的建议: QVariant Component::itemC
我被困在如何解决这个问题上。我在一个场景中有一个 QGraphicsItem,我正在将一个悬停事件从场景传递给这个 child 。当移动事件发生时(我只是使用带有鼠标跟踪的 mouseMoveEven
我应该怎么做才能删除 QGraphicsItem? 从我使用的场景中删除项目 QGraphicsScene::removeItem(QGraphicsItem * item); 来自此方法的文档:
我正在尝试围绕鼠标光标缩放对象。我很容易获得鼠标位置,并且可以使用 item->setScale(n) 毫无问题地缩放对象。但是,我不确定如何实际合并翻译以解释任意点。 有没有办法设置刻度中心?如果没
是否可以为QGraphicsItem设置动画,使其沿着QGraphicsScene/QGraphicsView内的某个路径移动? 类似于 this demo ,其中白点沿着齿轮移动 - 一条闭合曲线。
我想在按下鼠标按钮时检测鼠标光标何时移入QGraphicsItem,即在鼠标进入项目之前按下按钮。我的第一个想法是使用 hoverEnterEvent,但按下鼠标左键时似乎没有触发。我的另一个想法是使
我目前正在创建一个使用 QGraphicsView 的应用程序,并允许用户移动 QGraphicsItems,以便他们可以创建类似图表的结构。 我需要这些项目在单击时改变颜色,但在释放鼠标按钮时变回原
我正在尝试使用rubberBand来选择一些与某些预定的X和Y坐标关联的QGraphicItems。用户可以使用鼠标滚轮进行缩放,并使用鼠标中心按钮进行平移。用户可以单击鼠标左键并绘制一个选择窗口,但
我想在两个或多个 QGraphicsItem 之间实现像素完美碰撞检测器。 QGraphicsItem 类提供了一个使用 QPainterPath 对象的碰撞检测器,所以现在我想将图像从文件加载到 Q
我有很多 qgraphicsitem,它们是 map 上的航路点。我想将它们一起移动。因此,我使用了一个 for 循环来调用它们的 setPos() 函数。但是当项目数量变大(超过 100)时。 Ac
我怎样才能像这张图片一样绘制矩形和椭圆形。在这段代码中,它创建了带有单线边框的矩形和椭圆形。但是我需要像这张给定的图片那样更改边框样式。 Widget::Widget(QWidget *parent)
我正在使用一个 QGraphincsView,它包含几个继承自 QGraphicsItem 的元素。一切正常,我可以根据需要选择它们。当我按住 Ctrl 键时,我可以选择其中的几个。 现在我想实现一个
我有 QGraphicsScene,上面显示了一些自定义 QGraphicsItems。这些项目在 MeasurePoint 类中描述,该类继承自 QGraphicsItem。它们也存储在 QList
我是一名优秀的程序员,十分优秀!