公有继承QObject和QGrapgicsPathItem后实现鼠标绘制直线图元c++完整代码
时间: 2023-11-27 07:51:06 浏览: 83
以下是实现鼠标绘制直线图元的完整代码:
```
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QGraphicsLineItem>
#include <QMouseEvent>
class LineItem : public QObject, public QGraphicsLineItem
{
Q_OBJECT
public:
LineItem(QGraphicsItem *parent = nullptr)
: QObject(nullptr), QGraphicsLineItem(parent)
{
setPen(QPen(Qt::black, 2));
}
protected:
void mousePressEvent(QGraphicsSceneMouseEvent *event) override
{
if (event->button() == Qt::LeftButton) {
m_startPoint = event->scenePos();
m_endPoint = m_startPoint;
setLine(QLineF(m_startPoint, m_endPoint));
setCursor(Qt::CrossCursor);
event->accept();
} else {
event->ignore();
}
}
void mouseMoveEvent(QGraphicsSceneMouseEvent *event) override
{
if (event->buttons() & Qt::LeftButton) {
m_endPoint = event->scenePos();
setLine(QLineF(m_startPoint, m_endPoint));
event->accept();
} else {
event->ignore();
}
}
void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override
{
if (event->button() == Qt::LeftButton) {
setCursor(Qt::ArrowCursor);
emit lineDrawn(QLineF(m_startPoint, m_endPoint));
event->accept();
} else {
event->ignore();
}
}
signals:
void lineDrawn(const QLineF &line);
private:
QPointF m_startPoint;
QPointF m_endPoint;
};
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QGraphicsScene scene;
QGraphicsView view(&scene);
view.setRenderHint(QPainter::Antialiasing);
view.setDragMode(QGraphicsView::RubberBandDrag);
QObject::connect(&scene, &QGraphicsScene::selectionChanged, [&]() {
for (QGraphicsItem *item : scene.selectedItems()) {
if (LineItem *lineItem = qgraphicsitem_cast<LineItem *>(item)) {
qDebug() << "Selected line:" << lineItem->line();
}
}
});
QObject::connect(&scene, &QGraphicsScene::sceneRectChanged, [&]() {
view.fitInView(scene.sceneRect(), Qt::KeepAspectRatio);
});
QObject::connect(&view, &QGraphicsView::dragEnterEvent, [&](QDragEnterEvent *event) {
for (const QUrl &url : event->mimeData()->urls()) {
if (url.isLocalFile()) {
event->acceptProposedAction();
return;
}
}
event->ignore();
});
QObject::connect(&view, &QGraphicsView::dragMoveEvent, [&](QDragMoveEvent *event) {
event->acceptProposedAction();
});
QObject::connect(&view, &QGraphicsView::dropEvent, [&](QDropEvent *event) {
for (const QUrl &url : event->mimeData()->urls()) {
if (url.isLocalFile()) {
QGraphicsPixmapItem *pixmapItem = new QGraphicsPixmapItem(QPixmap(url.toLocalFile()));
pixmapItem->setPos(event->scenePos());
scene.addItem(pixmapItem);
}
}
event->acceptProposedAction();
});
QObject::connect(&view, &QGraphicsView::doubleClicked, [&](QMouseEvent *event) {
if (event->button() == Qt::LeftButton) {
LineItem *lineItem = new LineItem(nullptr);
scene.addItem(lineItem);
QObject::connect(lineItem, &LineItem::lineDrawn, [&](const QLineF &line) {
qDebug() << "New line:" << line;
});
lineItem->setPos(event->scenePos());
}
});
view.show();
return app.exec();
}
```
这个代码使用了Qt的图形界面框架,使用了QGraphicsScene、QGraphicsView、QGraphicsLineItem等类来实现。具体实现细节可以参考注释和Qt文档。
阅读全文