【问题标题】:Drawing extra item in QGraphicsItem during hover event在悬停事件期间在 QGraphicsItem 中绘制额外的项目
【发布时间】:2015-10-31 18:07:47
【问题描述】:

我想在QGraphicsView 上创建坐标点。当鼠标悬停在该点上时,将显示坐标。

我用QGraphicsEllipseItem绘制坐标。为了启用悬停事件,我需要重新实现QGraphicsEllipseItem。但是,由于QGraphicsEllipseItem 在构造时的大小是固定的,因此悬停文本无法正确显示。我该如何处理?

这是我的代码:

主窗口:

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    view = new QGraphicsView(this);
    view->setRenderHint(QPainter::Antialiasing);

    scene = new QGraphicsScene(this);
    view->fitInView(scene->sceneRect(), Qt::KeepAspectRatio);
    view->setScene(scene);
    setCentralWidget(view);

    for (int y = 0; y < 900; y += 100)
        for(int x = 0; x < 1400; x += 100)
            drawPoint(x, y);
}

void MainWindow::drawPoint(int x, int y)
{
    CoordinatePoint* point = new CoordinatePoint();
    point->setRect(QRect(x, y, 3, 3));
    point->setPen(QPen(Qt::red, 3, Qt::SolidLine));
    point->setBrush(Qt::red);
    scene->addItem(point);
}

重新实现QGraphicsEllipseItem:

CoordinatePoint::CoordinatePoint(QGraphicsItem* parent)
    :QGraphicsEllipseItem(parent)
{
    setAcceptHoverEvents(true);
}


void CoordinatePoint::hoverEnterEvent(QGraphicsSceneHoverEvent* event)
{
    hover = true;
    mx = event->pos().x();
    my = event->pos().y();
    update();
}

void CoordinatePoint::hoverLeaveEvent(QGraphicsSceneHoverEvent* event)
{
    hover = false;
    update();
}

void CoordinatePoint::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
{
    QGraphicsEllipseItem::paint(painter, option, widget);
    if (hover)
    {
        painter->save();
        painter->setPen(Qt::black);
        painter->setBrush(Qt::black);
        painter->drawText(mx + 2, my + 2,
                          "(" + QString::number(mx) + "," +
                          QString::number(my) + ")");
        painter->restore();
    }
}

【问题讨论】:

    标签: qt hover qgraphicsitem


    【解决方案1】:

    我认为为文本使用单独的子项将使您的生活更轻松:

    #include <QtWidgets>
    
    class CoordinatePoint : public QGraphicsEllipseItem
    {
    public:
        CoordinatePoint(QGraphicsItem* parent = Q_NULLPTR) :
            QGraphicsEllipseItem(parent),
            coordinateText(Q_NULLPTR)
        {
            setAcceptHoverEvents(true);
        }
    
        void hoverEnterEvent(QGraphicsSceneHoverEvent*)
        {
            if (!coordinateText) {
                coordinateText = new QGraphicsTextItem(this);
                coordinateText->setPlainText("(" + QString::number(x())
                    + "," + QString::number(y()) + ")");
                coordinateText->setX(2);
                coordinateText->setY(2);
            }
    
            coordinateText->setVisible(true);
        }
    
        void hoverLeaveEvent(QGraphicsSceneHoverEvent*)
        {
            if (coordinateText) {
                coordinateText->setVisible(false);
            }
        }
    
    private:
        QGraphicsTextItem *coordinateText;
    };
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
    
        QMainWindow window;
    
        QGraphicsView *view = new QGraphicsView(&window);
        view->setRenderHint(QPainter::Antialiasing);
    
        QGraphicsScene *scene = new QGraphicsScene(&window);
        view->fitInView(scene->sceneRect(), Qt::KeepAspectRatio);
        view->setScene(scene);
        window.setCentralWidget(view);
    
        for (int y = 0; y < 900; y += 100) {
            for(int x = 0; x < 1400; x += 100) {
                CoordinatePoint* point = new CoordinatePoint();
                point->setRect(QRect(0, 0, 3, 3));
                point->setX(x);
                point->setY(y);
                point->setPen(QPen(Qt::red, 3, Qt::SolidLine));
                point->setBrush(Qt::red);
                scene->addItem(point);
            }
        }
    
        window.show();
    
        return a.exec();
    }
    

    如果每个坐标都有一个额外的QGraphicsTextItem 让您担心,您可以构建其中一个并在它们之间共享,当每个坐标悬停时重新设置它。这应该可以正常工作,因为一次只能悬停一个坐标。

    如果您尝试将文本绘制为椭圆项目的一部分,您必须:

    • 增加项目的大小,使其足以容纳文本,这意味着覆盖boundingRect()
    • 仅在该区域的特定部分内绘制椭圆。
    • hoverEnterEvent() 中,检查鼠标光标是否在椭圆区域内。
    • 可能还有一堆其他的东西。

    【讨论】:

    • 谢谢,它有效。我原本以为如果我创建一个QGraphicsTextItem,其父级是另一个QGraphicsitem,则大小受QGraphicsitem 的限制。事实证明这是错误的。有个小问题:QGraphicsTextItem *coordinateText需要初始化为null_ptr,否则程序崩溃。
    • 它已初始化:coordinateText(Q_NULLPTR) - 你会崩溃吗?
    • 哦,我没有注意到你在构造函数中这样做
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-06
    • 2011-10-31
    • 1970-01-01
    • 2016-02-29
    • 2021-09-08
    • 2012-11-13
    • 2013-08-31
    相关资源
    最近更新 更多