好吧,我有一个QGraphicsScene
名为eye的课程.我叫一个函数:
void eye::playSequence(int sequenceNum) {
for (int i=0; i<sequences[sequenceNum].numberOfSlides(); i++) {
presentSlide(sequenceNum, i);
time_t start;
time(&start);
bool cont=false;
while (!cont) {
time_t now;
time(&now);
double dif;
dif=difftime(now, start);
if (dif>5.0)
cont=true;
}
}
}
Run Code Online (Sandbox Code Playgroud)
每个幻灯片调用:
void eye::presentSlide(int sequenceNum, int slideNum) {
Slide * slide=sequences[sequenceNum].getSlide(slideNum);
QGraphicsPixmapItem * pic0=scene.addPixmap(slide->getStimulus(0)->getImage());
pic0->setPos(0,0);
QGraphicsPixmapItem * pic1=scene.addPixmap(slide->getStimulus(1)->getImage());
pic1->setPos(horizontalResolution-350,0);
QGraphicsPixmapItem * pic2=scene.addPixmap(slide->getStimulus(2)->getImage());
pic2->setPos(horizontalResolution-350,verticalResolution-450);
QGraphicsPixmapItem * pic3=scene.addPixmap(slide->getStimulus(3)->getImage());
pic3->setPos(0,verticalResolution-450);
}
Run Code Online (Sandbox Code Playgroud)
现在,我希望这会显示一组图像,等待5秒,然后显示下一个,依此类推.相反,它只显示所有幻灯片,然后显示最后四个图像.我试过scene.update()
在每个我能成像的地方打电话,但它没有做任何事情.看起来场景只在playSequence
函数返回时更新.任何想法可能会发生在这里?
我想为QGraphicsItems提供可移动的多色工具提示.例如,当我在场景中单击graphicsItem时,会出现工具提示,然后在拖动鼠标时工具提示应该跟随光标.我可以使用标准QToolTip实现可移动工具提示,但似乎Qt仅支持整个工具提示的1种颜色.另外QToolTip没有paintEvent,所以我决定创建继承自QTextEdit的ColoredTooltip类,但是当我第一次显示ColoredTooltip对象时出现问题.它开始抓住鼠标事件,这对我来说不合适,因为我无法捕获图形场景的mouseMove事件并移动coloredTooltip.我该如何解决这个问题?
该问题与以下问题有关:强制QGraphicsItem保持不变
QGraphicsItem
在场景中移动时,我想在一个固定的位置。
建议的解决方案是覆盖void paintEvent(QPaintEvent*)
子类的QGraphicsView
。
void MyGraphicsView::paintEvent(QPaintEvent*) {
QPointF scenePos = mapToScene(0,0); // map viewport's top-left corner to scene
myItem->setPos(scenePos);
}
Run Code Online (Sandbox Code Playgroud)
但是,问题是我希望场景中的其他所有内容保持不变,即,如果缩放或移动,我希望所有其他内容都QGraphicsItems
保持默认状态。
解决此问题的一种较差的方法是void QGraphicsView::paintEvent(QPaintEvent*)
从内部调用void MyGraphicsView::paintEvent(QPaintEvent*)
。
void MyGraphicsView::paintEvent(QPaintEvent* event) {
QGraphicsView::paintEvent(event);
QPointF scenePos = mapToScene(0,0); // map viewport's top-left corner to scene
myItem->setPos(scenePos);
}
Run Code Online (Sandbox Code Playgroud)
但是,这会增加闪烁的行为,my_item
因为它首先使用QGraphicsView::paintEvent(event);
然后使用添加的代码进行定位
QPointF scenePos = mapToScene(0,0); // map viewport's top-left corner to scene
myItem->setPos(scenePos);
Run Code Online (Sandbox Code Playgroud)
问题是,我是否必须void MyGraphicsView::paintEvent(QPaintEvent*)
从头开始重新实现并myItem
为所有其他代码的期望行为和默认行为编写代码QGraphicsItems
,还是有更简单的方法来做到这一点?
谢谢。
我有一个QGraphicsView
和一个QGraphicsScene
.根据用户输入,QGraphicsItem
可以将一些放置在场景上.此项目既可选择又可移动.
当场景大于视图滚动条时(它们设置为在必要时显示).
当用户在视图边缘附近移动项目时,场景宽度/高度会相应地拉伸 - 我使场景变大.
问题是当项目靠近视图边界时,如何强制滚动条与场景一起滚动?我觉得在任何图形编辑器中都很常见.在MouseMoveEvent
场景中我使场景变大,强制滑块移动并相应地更新可见矩形.
这不符合预期.即使认为卷轴正在调整到新的场景大小,视图中也没有平滑的移动.这样做有更好的方法吗?
一些解释:
itemUnderCursor = currently slected QGraphicsItem
qgv = QGraphicsView
Run Code Online (Sandbox Code Playgroud)
代码段:
// check if item is near the border
QPointF point = itemUnderCursor->mapToScene(itemUnderCursor->boundingRect().topLeft());
double delta = 0;
if(point.x() < visibleRect.left())
{
// prevent from drawing outside the scene
itemUnderCursor->setPos(visibleRect.left(), itemUnderCursor->scenePos().y());
if(event->scenePos().x() < oldMousePos.x()-3)
{
// stretch the scene
if(qgv->horizontalScrollBar()->value() <= 0)
setSceneRect(QRectF(QPointF(sceneRect().x() - 3, sceneRect().y()), sceneRect().bottomRight()));
/*
* disable signals from DrawingArea in order …
Run Code Online (Sandbox Code Playgroud) 我正在编写代码以从文件加载图像并对这个图像做了一些编辑(改变一些像素的值),放大或缩小,然后保存图像。另外,我想知道与点击 qgraphicsscen 相关联的原始图像中的位置。到目前为止,我找不到任何有用的功能。
我加载图像的代码:
qgraphicsscene = myqgraphicsview->getScene();
qgraphicsscene->setSceneRect(image->rect());
myqgraphicsview->setScene(qgraphicsscene);
qgraphicsscene->addPixmap(QPixmap::fromImage(*image)); // this is the original image
Run Code Online (Sandbox Code Playgroud)
我的编辑代码:
mousePressEvent(QMouseEvent * e){
QPointF pt = mapToScene(e->pos());
scene->addEllipse(pt.x()-1, pt.y()-1, 2.0, 2.0,
QPen(), QBrush(Qt::SolidPattern));}
Run Code Online (Sandbox Code Playgroud)
我想知道 e->pos() 与原始图像中的确切位置之间的关系。
我使用pyqt4.8和python2.7
我创建QGraphicsView并插入QGraphicsScene.比View更大的场景,需要滚动.视图有垂直和水平滚动,但我想通过鼠标中键滚动.
我创建鼠标事件功能,但运动到奇怪.
def mousePressEvent(self, event):
self.__mousePressPos = None
self.__mouseMovePos = None
if event.button() == Qt.MidButton:
self.__mousePressPos = event.globalPos()
self.__mouseMovePos = event.globalPos()
else:
super(MyView, self).mousePressEvent(event)
def mouseMoveEvent(self, event):
if event.buttons() == Qt.MidButton:
curPos = self.__mousePressPos - self.__mouseMovePos
self.centerOn(curPos.x(), curPos.y())
self.__mouseMovePos = globalPos
else:
super(MyView, self).mouseMoveEvent(event)
def mouseReleaseEvent(self, event):
if self.__mousePressPos is not None:
moved = event.globalPos() - self.__mousePressPos
if moved.manhattanLength() > 3:
event.ignore()
return
super(MyView, self).mouseReleaseEvent(event)
Run Code Online (Sandbox Code Playgroud)
首先,场景移动与鼠标的移动不对应.我怀疑是视口的相对大小和场景的责任.以及舞台的规模.
其次,我无法移动视口外的场景,它到达边缘时停止.
让第二个仍然可以容忍.让它保持原样,但如何计算正确移动的场景的正确偏移移动鼠标,无论它被捕获的位置?
我已经阅读了关于它们的文档,但我仍然不清楚实际的区别是什么。ItemCoordinateCache 的文档说明:
为项目的逻辑(本地)坐标系启用缓存。
而对于 QGraphicsItem::DeviceCoordinateCache:
缓存在设备坐标的绘制设备级别启用。此模式适用于可以移动但不能旋转、缩放或剪切的项目。
这并没有为我澄清事情。好的,所以 DeviceCoordinateCache 不能用于旋转、缩放或剪切,它使用“设备坐标”。
但这究竟是什么意思?有一个离屏缓冲区,我假设对于本地坐标,缓冲区填充了项目的缓存图像,然后像 QGraphicsPixmapItem 一样起作用,并且对它应用了诸如不透明度和变换矩阵之类的任何效果。我认为问题在于这会光栅化项目的图像,因此文档警告说它不会是像素完美的。
但是它如何与 DeviceCoordinateCache 一起工作?项目是先旋转、剪切和缩放,然后在屏幕外像素图上绘制,还是在像素图上呈现相同但不应用任何转换?
编辑:我还尝试将 DeviceCoordinateCache 设置为旋转和缩放的项目,即使我调整它的大小,它也能完美显示,所以我不明白为什么文档说它不应该与缩放、旋转或剪切的项目一起使用。
出于某种原因,我需要将 opencv VideoCapture 包装在一个将在 Qt Quick 中使用的类中。
有两个类,一个是Camera,另一个是CameraView。CameraView 继承自 QQuickPaintedItem。
相机类将定期获取图像。它通过 QObject::startTimer(int interval) 实现。(例如,如果网络摄像头的 fps 为 30,则计时器间隔为 1000 / 30 - 8,8 是时间偏差)。一旦 Camera 获取到图像,它会通过调用 CameraView::Update() 通知 CameraView 重新绘制。
而在 CameraView::paint(QPainter *) 中,CameraView 将从 Camera 类获取图像的副本并通过调用 QPainter::drawImage(...) 绘制该图像。
我在编码过程中遇到了一些问题:
我尝试用 QThread 替换时间事件以定期从相机获取图像。当我在 QThread 中调用 CameraView::Update() 时,CameraView 不会重新绘制。问题是什么?
在我的笔记本电脑中,当我让 CameraView 全屏绘制图像时,我发现一个 Python 程序变慢了。是另一种低成本高效的绘画方式吗?
到目前为止,我有这段代码:
from PyQt4 import QtGui, QtCore
class MyFrame(QtGui.QGraphicsView):
"""
Python PyQt: How can I move my widgets on the window with mouse?
/sf/ask/854937401/
"""
def __init__( self, parent = None ):
super( MyFrame, self ).__init__( parent )
scene = QtGui.QGraphicsScene()
self.setScene( scene )
self.resize( 400, 340 )
# http://pyqt.sourceforge.net/Docs/PyQt4/qpen.html
pencil = QtGui.QPen( QtCore.Qt.black, 1)
pencil.setStyle( QtCore.Qt.SolidLine )
scene.addLine( QtCore.QLineF( 0, -50, 0, 50 ), pencil )
scene.addLine( QtCore.QLineF( -50, 0, 50, 0 ), pencil )
fitInViewButton = QtGui.QPushButton( 'Fit In …
Run Code Online (Sandbox Code Playgroud) QGraphicsItems
我试图用直线( )连接两个QGraphicsLineItem
;通过在第一个对象上单击鼠标中键,然后将鼠标悬停在第二个对象上,释放它后,它应该在它们之间画线。
这是应该连接这些项目的函数。
void GraphicsBlock::connectBlocks(GraphicsBlock *block)
{
GraphicsConnect *connection = new GraphicsConnect(); //Class with QGraphicsLineItem
connection->line->setLine(QLineF(this->pos(), block->pos()));
}
Run Code Online (Sandbox Code Playgroud)
我正在处理一个场景,但在通过鼠标位置查找第二个块时遇到问题。函数mousePressEvent
应该与此代码一起使用:
if(event->button() == Qt::MiddleButton)
Connecting == true;
Run Code Online (Sandbox Code Playgroud)
在那之后mouseReleaseEvent
if(Connecting)
{
//Get object by mouse position here
//this.connectBlocks(..)
Connecting = false;
}
Run Code Online (Sandbox Code Playgroud)
我尝试scene->itemsAt(mouse.x(), mouse.y(), QTransform())
过并且
scene->items(QPointF(mouse.x(),mouse.y())
但它总是返回空列表。
编辑:这就是应用程序应该如何工作,蓝色椭圆被单击端口,现在我应该拖动(仍然按下鼠标)到另一个端口并释放鼠标。
qgraphicsscene ×10
qt ×8
c++ ×5
pyqt ×2
python ×2
autoscroll ×1
caching ×1
mouseevent ×1
mousepress ×1
pyqt4 ×1
qpixmap ×1
qquickitem ×1
tooltip ×1
transform ×1