我正在尝试一些看起来很容易的东西,但我正在努力解决它。我希望能够在GUI中绘制矩形。我现在在做的是:
我使用Qt Designer创建了一个GUI,其中包含了一个QGraphicsView。
在我的主窗口中,我创建了一个myGraphicsScene(从QGraphicsScene派生的类,但是通过鼠标按下,移动和释放事件被覆盖),我将场景设置为在UI中创建的QGraphicsView。
问题是我在更改场景时无法正确控制视图的刷新和更新。
以下是代码的相关部分:
MainGUIWindow构造函数和初始化:
MainGUIWindow::MainGUIWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainGUIWindow)
{
ui->setupUi(this);
_init();
}
void MainGUIWindow::_init()
{
scene = new myGraphicsScene(ui->frame_drawing);
scene->setSceneRect(QRectF(QPointF(-100, 100), QSizeF(200, 200)));
ui->graphicsView->setScene(scene);
QRect rect(10, 20, 80, 60);
scene->addText("Hello world!");
scene->addRect(rect, QPen(Qt::black), QBrush(Qt::blue));
}
我可以很好地看到HelloWorld文本和这个矩形。但是,当我开始点击事件时,我不再正确地获得更新。
myGraphicsScene类标题:
#ifndef MYGRAPHICSSCENE_H
#define MYGRAPHICSSCENE_H
#include <QGraphicsScene>
class QGraphicsSceneMouseEvent;
class QPointF;
class QColor;
class myGraphicsScene : public QGraphicsScene
{
Q_OBJECT
public:
explicit myGraphicsScene(QObject *parent = 0);
public slots:
signals:
protected:
void mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent) override;
void mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent) override;
void mouseReleaseEvent(QGraphicsSceneMouseEvent *mouseEvent) override;
private:
QPen* pen;
QBrush* brush;
QRect* rect;
QPoint* p1;
QPoint* p2;
bool firstClick;
// bool startedRect;
};
#endif
myGraphicsScene类实现:
#include "myGraphicsScene.h"
#include <QGraphicsSceneMouseEvent>
#include <QRect>
myGraphicsScene::myGraphicsScene(QObject *parent)
: QGraphicsScene(parent)
{
pen = new QPen(Qt::black);
brush = new QBrush(Qt::blue);
rect = 0;
// startedRect = false;
firstClick = true;
}
void myGraphicsScene::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent)
{
if (mouseEvent->button() != Qt::LeftButton)
return;
// rect = new QRect((mouseEvent->scenePos()).toPoint(), (mouseEvent->scenePos()).toPoint());
// addRect(*rect, *pen, *brush);
// startedRect = true;
if(firstClick)
{
p1 = new QPoint((mouseEvent->scenePos()).toPoint());
QRect tmp_rect(*p1, *p1);
addRect(tmp_rect, *pen, *brush);
}
else
{
p2 = new QPoint((mouseEvent->scenePos()).toPoint());
QRect tmp_rect(*p2, *p2);
addRect(tmp_rect, *pen, *brush);
}
QGraphicsScene::mousePressEvent(mouseEvent);
}
void myGraphicsScene::mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent)
{
// if(startedRect)
// {
// rect->moveBottomRight((mouseEvent->scenePos()).toPoint());
// qDebug("Mouse Position: %d, %d", (mouseEvent->scenePos()).toPoint().x(), (mouseEvent->scenePos()).toPoint().y());
// qDebug("Rectangle BottomRight Position: %d, %d", rect->bottomRight().x(), rect->bottomRight().y());
// }
QGraphicsScene::mouseMoveEvent(mouseEvent);
}
void myGraphicsScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *mouseEvent)
{
if (mouseEvent->button() != Qt::LeftButton)
return;
// rect = 0;
// startedRect = false;
if(firstClick)
{
firstClick = false;
}
else
{
rect = new QRect(*p1, *p2);
addRect(*rect, *pen, *brush);
p1 = 0;
p2 = 0;
rect = 0;
firstClick = true;
}
QGraphicsScene::mouseReleaseEvent(mouseEvent);
}
我最初的想法是以拖放方式绘制矩形,但最终最终尝试了两次点击方法,这种方法效果更好但仍然没有完全更新视图。< / p>
我是Qt的新手,所以我不太熟悉它应该如何完成。任何帮助将不胜感激,因为我已经有一段时间在这里停留了一段时间。
谢谢!
答案 0 :(得分:1)
在您的每个mouse*Event()
重载中,请调用QGraphicsScene::update()
方法。它将在视图上安排重绘。
答案 1 :(得分:1)
我刚刚找到解决方案:我必须实际更改添加到场景中的项目,而不是矩形。所以,现在,我的代码看起来像:
#include "myGraphicsScene.h"
#include <QGraphicsSceneMouseEvent>
#include <QRect>
#include <QGraphicsRectItem>
myGraphicsScene::myGraphicsScene(QObject *parent)
: QGraphicsScene(parent)
{
pen = new QPen(Qt::black);
brush = new QBrush(Qt::blue);
tmp_rect = 0;
startedRect = false;
// firstClick = true;
}
void myGraphicsScene::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent)
{
if (mouseEvent->button() != Qt::LeftButton)
return;
// Drag and drop approach
startedRect = true;
p1 = new QPointF(mouseEvent->scenePos());
tmp_rect = new QRectF(*p1, *p1);
// addRect(*tmp_rect, *pen, *brush);
tmp_rect_item = new QGraphicsRectItem(*tmp_rect);
rectangles.push_back(tmp_rect_item);
addItem(rectangles.back());
// Two-clicks approach
// if(firstClick)
// {
// p1 = new QPointF(mouseEvent->scenePos());
// tmp_rect_item = addRect(QRect(p1->toPoint(), p1->toPoint()), *pen, *brush); //save it to remove it after
// }
// else
// {
// p2 = new QPointF(mouseEvent->scenePos());
// // QRect tmp_rect(*p2, *p2);
// // addRect(tmp_rect, *pen, *brush);
// }
update();
QGraphicsScene::mousePressEvent(mouseEvent);
}
void myGraphicsScene::mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent)
{
if(startedRect)
{
tmp_rect_item->setRect(QRectF(*p1, mouseEvent->scenePos()));
qDebug("Mouse Position: %d, %d", (mouseEvent->scenePos()).toPoint().x(), (mouseEvent->scenePos()).toPoint().y());
qDebug("Rectangle BottomRight Position: %d, %d", tmp_rect->bottomRight().x(), tmp_rect->bottomRight().y());
update();
}
QGraphicsScene::mouseMoveEvent(mouseEvent);
}
void myGraphicsScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *mouseEvent)
{
if (mouseEvent->button() != Qt::LeftButton)
return;
// Drag and drop approach:
tmp_rect = 0;
startedRect = false;
// Two-clicks approach
// if(firstClick)
// {
// firstClick = false;
// }
// else
// {
// removeItem(tmp_rect_item);
// tmp_rect_item = new QGraphicsRectItem(QRectF(*p1, *p2));
// // *tmp_rect, *pen, *brush);
// rectangles.push_back(tmp_rect_item);
// addItem(rectangles.back());
// p1 = 0;
// p2 = 0;
// tmp_rect = 0;
// firstClick = true;
// }
update();
QGraphicsScene::mouseReleaseEvent(mouseEvent);
}
答案 2 :(得分:0)
我发现另一个有用的提示是,当我扩展视图时,我将鼠标事件处理程序置于&#34; public&#34;而不是&#34;受保护&#34;。我做了改变,呼叫是一致的。