if (QLineF(QPointF(event->x(), event->y()), m_lastPos).length() < QApplication::startDragDistance())出现错误:QGraphicsSceneMouseEvent中没有成员下x()
时间: 2024-05-26 18:17:56 浏览: 11
这个错误的原因是QGraphicsSceneMouseEvent中没有x()和y()成员函数。你可以使用pos()函数来获取事件发生的位置,代码如下:
if (QLineF(QPointF(event->pos()), m_lastPos).length() < QApplication::startDragDistance())
这里将event->pos()传递给QPointF构造函数,获取事件的位置。
相关问题
#include <QtWidgets>class MyItem : public QGraphicsObject{public: MyItem(int pinCount, QColor color, QSizeF size, QGraphicsItem* parent = nullptr) : QGraphicsObject(parent) , m_pinCount(pinCount) , m_color(color) , m_size(size) {} QRectF boundingRect() const override { return QRectF(QPointF(0, 0), m_size); } QPainterPath shape() const override { QPainterPath path; path.addRect(QRectF(QPointF(0, 0), m_size)); return path; } void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) override { Q_UNUSED(option) Q_UNUSED(widget) painter->setPen(Qt::NoPen); painter->setBrush(m_color); painter->drawRect(boundingRect()); painter->setPen(Qt::black); painter->setFont(QFont("Arial", 10)); painter->drawText(boundingRect(), Qt::AlignCenter, QString("%1 pins").arg(m_pinCount)); } void mousePressEvent(QGraphicsSceneMouseEvent* event) override { if (event->button() == Qt::LeftButton) { setCursor(Qt::ClosedHandCursor); m_lastPos = event->scenePos(); } } void mouseMoveEvent(QGraphicsSceneMouseEvent* event) override { if (QLineF(event->screenPos(), m_lastPos).length() < QApplication::startDragDistance()) return; QGraphicsObject::mouseMoveEvent(event); setPos(pos() + event->scenePos() - m_lastPos); m_lastPos = event->scenePos(); } void mouseReleaseEvent(QGraphicsSceneMouseEvent* event) override { if (event->button() == Qt::LeftButton) { setCursor(Qt::ArrowCursor); } }private: int m_pinCount; QColor m_color; QSizeF m_size; QPointF m_lastPos;};int main(int argc, char* argv[]){ QApplication app(argc, argv); QGraphicsScene scene; scene.setSceneRect(0, 0, 800, 600); MyItem* item1 = new MyItem(8, Qt::red, QSizeF(80, 60)); item1->setPos(100, 100); scene.addItem(item1); MyItem* item2 = new MyItem(16, Qt::green, QSizeF(120, 80)); item2->setPos(300, 200); scene.addItem(item2); QGraphicsView view(&scene); view.setRenderHint(QPainter::Antialiasing); view.setDragMode(QGraphicsView::RubberBandDrag); view.setViewportUpdateMode(QGraphicsView::FullViewportUpdate); view.show(); return app.exec();}在这个代码中,如何使用TextOut去给矩形框中添加文本
这个代码是使用Qt的绘图框架来实现的,所以不能直接使用TextOut函数。可以在MyItem类的paint函数中使用QPainter的函数来绘制文本,如下所示:
```
void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) override
{
Q_UNUSED(option)
Q_UNUSED(widget)
painter->setPen(Qt::NoPen);
painter->setBrush(m_color);
painter->drawRect(boundingRect());
painter->setPen(Qt::black);
painter->setFont(QFont("Arial", 10));
painter->drawText(boundingRect(), Qt::AlignCenter, QString("%1 pins").arg(m_pinCount));
}
```
这段代码中,我们使用了QPainter的setFont函数来设置字体,使用drawText函数来绘制文本。其中,boundingRect()函数返回了该图形项的边界矩形,Qt::AlignCenter表示文本将会居中对齐。这样,每个矩形框中就会显示相应的文本。
if(!m_bEnabled || !m_bPressed || !m_element) { QQuickPaintedItem::mousePressEvent(event); } else { //qDebug() << "mouse move"; m_element->m_lines.append(QLineF(m_lastPoint, event->localPos())); m_lastPoint = event->localPos(); update(); }
这是 PaintedItem 类中的一个操作,用于处理鼠标移动事件。在函数中首先通过判断 m_bEnabled、m_bPressed 和 m_element 这几个标志位,来判断是否可以处理当前的鼠标事件。如果不能处理,则调用 QQuickPaintedItem 类的 mousePressEvent() 函数继续处理事件。如果可以处理,则将当前的鼠标位置和上一个位置构成一条线段,并添加到当前的 ElementGroup 对象的 m_lines 数组中,表示正在绘制一条新的线条。然后更新 m_lastPoint 的值为当前的鼠标位置,以便下一次调用 mouseMoveEvent() 函数时可以计算出新的线段。最后,通过调用 update() 函数来触发重新绘制,以便将新绘制的线条显示出来。
相关推荐
![](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)
![](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)