我有一个QList用于存储QgraphicsScene上的一些项目,如:
QList<QGraphicsItem*> lineList;
Run Code Online (Sandbox Code Playgroud)
当我想用它时:
lineList[itemIndex++]=scene->createItemGroup(groupItems);
Run Code Online (Sandbox Code Playgroud)
我遇到了运行时错误.我好奇为什么?
顺便说一下我对linelist.append()的了解
谢谢.
嘿,想要在按下并移动鼠标按钮时拖动这条贝塞尔曲线.
我这样做了:
void MainWindow::mouseMoveEvent(QMouseEvent *e)
{
qDebug()<<"in mouse move - outside if";
if((e->buttons() & Qt::RightButton) && isStart && enableDrag)
{
qDebug()<<"mouse dragging happening";
xc2=e->pos().x();
yc2=e->pos().y();
drawDragBezier(xc2,yc2);
}
}
Run Code Online (Sandbox Code Playgroud)
当我按下右键并开始在整个主窗口中移动鼠标时,这开始拖动.但是我只想在我按下鼠标按钮并在QGraphicsScene中移动鼠标时开始拖动.
怎么解决这个?
编辑:
void mySubClass1::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
if(event->button() == Qt::LeftButton)
{
qDebug()<<"in musubclass mouse press event: "<<event->pos().x()<<" "
<<event- >pos().y();
if(shape().contains(event->pos()))
{
currPosX=event->pos().x();
currPosY=event->pos().y();
qDebug()<<"currPosX currPosY: "<<currPosX<<" "<<currPosY;
}
}
}
Run Code Online (Sandbox Code Playgroud)
主窗口类是:
{
myGPath=new mySubClass1();
myScene=new QGraphicsScene;
myScene->addItem(myGPath);
ui->graphicsView->setScene(myScene);
QPointF *startPoint=new QPointF(50,50);
myPaintPath=new QPainterPath(*startPoint);
myPaintPath->quadTo(100,25,200,200);
myGPath->setPath(*myPaintPath);
}
Run Code Online (Sandbox Code Playgroud)
这是正确的方法吗?
我创建了自己的类(视图和场景)来显示我添加到其中的图像和对象,甚至在我的视图中实现了放大/缩小功能,但现在我必须添加新功能,我什至不知道如何开始寻找它。
不幸的是 - 我什至不知道如何寻找一些基本的东西,因为“移动”和类似的东西是指拖动对象。
编辑 1
void CustomGraphicView::mouseMoveEvent(QMouseEvent *event)
{
if(event->buttons() == Qt::MidButton)
{
setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
translate(event->x(),event->y());
}
}
Run Code Online (Sandbox Code Playgroud)
试过这个 - 但它正在反向工作。
我正在寻找如何放大 QGraphicsView 但在光标位置上。目前我可以缩放,但缩放的位置不一致。
def wheelEvent(self, event):
'''Wheel event to zoom
'''
# Run default event
QtWidgets.QGraphicsView.wheelEvent(self, event)
# Define zoom factor
factor = 1.1
if event.delta() < 0:
factor = 0.9
self.scale(factor, factor)
Run Code Online (Sandbox Code Playgroud)
我见过使用self.mapToScene()
但没有成功
我有几个不同的类派生自QGraphicsItem
或其子(如QGraphicsRectItem
).我现在需要复制这些类的选定对象,而不知道我确切地复制了哪一个.
因为QGraphicsScene::selectedItems()
我决定使用它返回所选项目的列表,但是我不能复制它,QGraphicsItem
因为它是一个抽象类.为了解决这个问题,我试图使用malloc
和复制对象memcpy
.
MainWindow.cpp
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
scene = new QGraphicsScene();
item = new QGraphicsRectItem(50,50,50,50);
item->setFlag(QGraphicsItem::ItemIsSelectable);
scene->addItem(item);
item->setSelected(true);
ui->graphicsView->setScene(scene);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
for(QGraphicsItem *item : scene->selectedItems())
{
QGraphicsItem *copiedItem=(QGraphicsItem *)malloc(sizeof(*item));
memcpy(copiedItem, item, sizeof(*copiedItem));
copiedItem->moveBy(50, 50);
scene->addItem(copiedItem);
qDebug() << item;
qDebug() << copiedItem;
}
}
Run Code Online (Sandbox Code Playgroud)MainWindow.h
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT …
Run Code Online (Sandbox Code Playgroud)我有一个QGraphicsView
子类,其中包含一个子QGraphicsScene
类QWidget
。
将QGraphicsScene
具有在(0,0)顶端让拐角处的实际绘制矩形(让现场真正具有QRectF(-x,-y,w,h)
价值无关的问题)。
在启动时,实际上是在showEvent
小部件的中,我将场景的大小设置为适合自定义大小的矩形。这也使它居中。
我想要的是,在开始时将场景的(0,0)点与视图的左上角匹配。
与其他具有相同标题的问题不同,我实际上需要移动视图,而不是场景中的项目。
图片1:实际开始,图片2:所需
我在尝试:
void MyWidget::showEvent(QShowEvent *event)
{
static bool startUp = true;
if(startUp)
{
QSizeF sz = m_scene->getActualSize();
ui->graphicsView->fitInView(QRectF(0, 0, sz.width(), sz.height()), Qt::KeepAspectRatio);
QPointF zero = ui->graphicsView->mapFromScene(QPointF(0,0));
ui->graphicsView->translate(-zero.x(), -zero.y()); // or positive
startUp = false;
}
QWidget::showEvent(event);
}
Run Code Online (Sandbox Code Playgroud)
注意:我使用fitInView
来获得所需的缩放-它也使所需的矩形聚焦-但在视图中居中。
我也试过
void GraphicsView::alignToZero()
{
QPointF zero = this->mapFromScene(QPointF(0,0));
this->scrollContentsBy(-zero.x(), -zero.y());
}
void MyWidget::showEvent(QShowEvent *event)
{
static bool startUp = true;
if(startUp)
{
QSizeF …
Run Code Online (Sandbox Code Playgroud)