c++自定义继承与QGraphicsItem的类实现传入矩形的参数来画矩形,并可以响应鼠标拖动以及选择改变颜色和用小矩形以大的矩形的顶点为中新显示大矩形顶点,选中矩形顶点内可进行大矩形的缩放实现
时间: 2024-05-28 20:09:03 浏览: 6
可以的,你可以先定义一个继承自QGraphicsItem的类,然后在类中实现paint、boundingRect、mousePressEvent、mouseMoveEvent和mouseReleaseEvent这几个事件函数来实现画矩形、拖动以及选择改变颜色和用小矩形以大的矩形的顶点为中新显示大矩形顶点、选中矩形顶点内可进行大矩形的缩放等功能。你可以在类的构造函数中传入矩形的参数来画矩形。需要注意的是,在进行大矩形的缩放时你可以通过判断选中的矩形顶点来进行缩放的处理。希望能对你有帮助,有什么问题可以再问哦。
相关问题
Qt c++用QGraphicsItem类实现拖动一个矩形图形时 出现一个虚线矩形来代表拖动位置 鼠标松开后矩形移动到拖动位置 虚线矩形消失
要实现这个功能,你需要在继承自QGraphicsItem的自定义图形项中重写mousePressEvent、mouseMoveEvent和mouseReleaseEvent三个事件。
在mousePressEvent中,你需要记录下鼠标点击时的位置和当前图形项的位置,然后将图形项的状态设置为正在被拖动。
在mouseMoveEvent中,你需要根据鼠标移动的距离计算出图形项应该移动的位置,并在图形项上绘制一个虚线矩形,表示拖动位置。
在mouseReleaseEvent中,你需要将图形项的状态设置为已经停止拖动,并将图形项移动到拖动位置,同时让虚线矩形消失。
下面是一个简单的示例代码,可以帮助你理解这个过程:
```
class MyRectItem : public QGraphicsItem
{
public:
MyRectItem(QGraphicsItem *parent = nullptr);
QRectF boundingRect() const override;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override;
void mousePressEvent(QGraphicsSceneMouseEvent *event) override;
void mouseMoveEvent(QGraphicsSceneMouseEvent *event) override;
void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override;
private:
QPointF m_pressPos;
QPointF m_itemPos;
bool m_isDragging;
};
MyRectItem::MyRectItem(QGraphicsItem *parent)
: QGraphicsItem(parent)
, m_isDragging(false)
{
}
QRectF MyRectItem::boundingRect() const
{
return QRectF(0, 0, 50, 50);
}
void MyRectItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
Q_UNUSED(option);
Q_UNUSED(widget);
if (m_isDragging) {
// 绘制虚线矩形
painter->setPen(Qt::DashLine);
painter->drawRect(QRectF(m_itemPos, boundingRect().size()));
}
else {
// 绘制实线矩形
painter->setPen(Qt::SolidLine);
painter->drawRect(boundingRect());
}
}
void MyRectItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
if (event->button() == Qt::LeftButton) {
m_pressPos = event->scenePos();
m_itemPos = pos();
m_isDragging = true;
update();
}
else {
QGraphicsItem::mousePressEvent(event);
}
}
void MyRectItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
if (m_isDragging) {
QPointF delta = event->scenePos() - m_pressPos;
setPos(m_itemPos + delta);
update();
}
else {
QGraphicsItem::mouseMoveEvent(event);
}
}
void MyRectItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
if (event->button() == Qt::LeftButton) {
m_isDragging = false;
update();
}
else {
QGraphicsItem::mouseReleaseEvent(event);
}
}
```
c++ qt继承QObject和QGraphicsItem画十字点、画矩形、画多边形、画直线卡尺,在同一个类中,而且可以通过鼠标滚轮缩放
可以通过以下代码实现:
```c++
#include <QtGui>
class MyGraphicsItem : public QObject, public QGraphicsItem
{
public:
MyGraphicsItem(QGraphicsItem *parent = nullptr) : QObject(parent), QGraphicsItem(parent) {}
QRectF boundingRect() const override
{
return QRectF(-50, -50, 100, 100);
}
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override
{
painter->setRenderHint(QPainter::Antialiasing);
painter->setPen(QPen(Qt::black, 2));
painter->drawLine(-50, 0, 50, 0);
painter->drawLine(0, -50, 0, 50);
painter->drawRect(-25, -25, 50, 50);
QPolygonF polygon;
polygon << QPointF(-30, 30) << QPointF(0, -30) << QPointF(30, 30);
painter->drawPolygon(polygon);
painter->setPen(QPen(Qt::red, 1, Qt::DashLine));
painter->drawLine(-50, 0, 50, 0);
painter->drawLine(0, -50, 0, 50);
}
void mousePressEvent(QGraphicsSceneMouseEvent *event) override
{
if (event->button() == Qt::LeftButton) {
m_lastPos = event->pos();
m_isDragging = true;
}
}
void mouseMoveEvent(QGraphicsSceneMouseEvent *event) override
{
if (m_isDragging) {
QPointF delta = event->pos() - m_lastPos;
setPos(pos() + delta);
m_lastPos = event->pos();
}
}
void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override
{
if (event->button() == Qt::LeftButton) {
m_isDragging = false;
}
}
void wheelEvent(QGraphicsSceneWheelEvent *event) override
{
qreal factor = qPow(1.2, event->delta() / 240.0);
setScale(scale() * factor);
}
private:
QPointF m_lastPos;
bool m_isDragging = false;
};
```
在这个类中,我们继承了 QObject 和 QGraphicsItem,实现了 boundingRect() 和 paint() 函数来绘制图形,以及 mousePressEvent()、mouseMoveEvent()、mouseReleaseEvent() 和 wheelEvent() 函数来处理鼠标事件。在 paint() 函数中,我们绘制了十字点、矩形、多边形和直线卡尺,并使用 setPen() 函数设置了画笔的颜色、宽度和样式。在 mousePressEvent()、mouseMoveEvent() 和 mouseReleaseEvent() 函数中,我们处理了鼠标拖动事件,使图形可以被拖动。在 wheelEvent() 函数中,我们处理了鼠标滚轮事件,使图形可以被缩放。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)