QT5.14.2自带Examples:Tool Tips

概述

该示例演示如何为应用程序的widget提供静态和动态工具提示。用户可以使用提供的工具按钮创建新的形状项,并使用鼠标移动这些项。当光标指向形状项或某个按钮时,将提供工具提示。
QT5.14.2自带Examples:Tool Tips_第1张图片

静态:通过调用QWidget::setToolTip();本示例中的按钮的工具提示采用静态方式。
动态:这种方法使用鼠标跟踪和事件处理来确定在任意时间点,光标下的widget,并显示它们的工具提示;本示例中shapeItem对象的工具提示形采用动态方式。

实现步骤

这个例子包括两个类:

  1. ShapeItem 是一个自定义的widget,表示单个形状项。
  2. SortingBox继承自QWidget,是应用程序的主要widget。

ShapeItem类

ShapeItem类很简单,只是对四个私有成员变量的赋值和取值。

类定义

#ifndef SHAPEITEM_H
#define SHAPEITEM_H

#include 
#include 
#include 

class ShapeItem
{
public:
    void setPath(const QPainterPath &path);
    void setToolTip(const QString &toolTip);
    void setPosition(const QPoint &position);
    void setColor(const QColor &color);

    QPainterPath path() const;
    QPoint position() const;
    QColor color() const;
    QString toolTip() const;

private:
    QPainterPath myPath;
    QPoint myPosition;
    QColor myColor;
    QString myToolTip;
};

#endif

类实现

#include "shapeitem.h"

QPainterPath ShapeItem::path() const
{
    return myPath;
}

QPoint ShapeItem::position() const
{
    return myPosition;
}

QColor ShapeItem::color() const
{
    return myColor;
}

QString ShapeItem::toolTip() const
{
    return myToolTip;
}

void ShapeItem::setPath(const QPainterPath &path)
{
    myPath = path;
}

void ShapeItem::setToolTip(const QString &toolTip)
{
    myToolTip = toolTip;
}

void ShapeItem::setPosition(const QPoint &position)
{
    myPosition = position;
}

void ShapeItem::setColor(const QColor &color)
{
    myColor = color;
}


SortingBox类

类定义

#ifndef SORTINGBOX_H
#define SORTINGBOX_H

#include "shapeitem.h"

#include 

QT_BEGIN_NAMESPACE
class QAction;
class QPoint;
class QToolButton;
QT_END_NAMESPACE

class SortingBox : public QWidget
{
    Q_OBJECT

public:
    SortingBox(QWidget *parent = nullptr);

protected:
    bool event(QEvent *event) override;//提供工具提示
    void resizeEvent(QResizeEvent *event) override;//确保在用户调整主窗口的大小时始终显示
    void paintEvent(QPaintEvent *event) override;//在SortingBox窗口中显示形状项
	//鼠标事件处理程序被重新实现,以使用户能够移动图形项。
    void mousePressEvent(QMouseEvent *event) override;
    void mouseMoveEvent(QMouseEvent *event) override;
    void mouseReleaseEvent(QMouseEvent *event) override;

//此外,我们需要三个专用插槽,使用户能够创建新的形状项目。
private slots:
    void createNewCircle();
    void createNewSquare();
    void createNewTriangle();

private:
//在构造窗口时使用initialItemPosition()、initialItemColor()和createToolButton()
//在用户调整应用程序主小部件的大小时使用updateButtonGeometry()
    int updateButtonGeometry(QToolButton *button, int x, int y);
    void createShapeItem(const QPainterPath &path, const QString &toolTip,
                         const QPoint &pos, const QColor &color);
    int itemAt(const QPoint &pos);
    void moveItemTo(const QPoint &pos);
    QPoint initialItemPosition(const QPainterPath &path);
    QPoint randomItemPosition();
    QColor initialItemColor();
    QColor randomItemColor();
    QToolButton *createToolButton(const QString &toolTip, const QIcon &icon,
                                  const char *member);

    QVector<ShapeItem> shapeItems;
    QPainterPath circlePath;
    QPainterPath squarePath;
    QPainterPath trianglePath;

    QPoint previousPosition;
    ShapeItem *itemInMotion;

    QToolButton *newCircleButton;
    QToolButton *newSquareButton;
    QToolButton *newTriangleButton;
};
#endif

类实现

#include "sortingbox.h"

#include 
#include 
#include 
#include 
#include 
#include 
#include 

SortingBox::SortingBox(QWidget *parent)
    : QWidget(parent)
{
	//如果启用了鼠标跟踪,即使没有按下按钮,窗口也会接收鼠标移动事件。
    setMouseTracking(true);
    setBackgroundRole(QPalette::Base);
    //指向正在被拖动的Item的指针
    itemInMotion = nullptr;
    //创造三个按钮,将会在 resizeEvent 里设置位置。
    newCircleButton = createToolButton(tr("New Circle"),
                                       QIcon(":/images/circle.png"),
                                       SLOT(createNewCircle()));

    newSquareButton = createToolButton(tr("New Square"),
                                       QIcon(":/images/square.png"),
                                       SLOT(createNewSquare()));

    newTriangleButton = createToolButton(tr("New Triangle"),
                                         QIcon(":/images/triangle.png"),
                                         SLOT(createNewTriangle()));

    circlePath.addEllipse(QRect(0, 0, 100, 100));
    squarePath.addRect(QRect(0, 0, 100, 100));

    qreal x = trianglePath.currentPosition().x();
    qreal y = trianglePath.currentPosition().y();
    trianglePath.moveTo(x + 120 / 2, y);
    trianglePath.lineTo(0, 100);
    trianglePath.lineTo(120, 100);
    trianglePath.lineTo(x + 120 / 2, y);

    setWindowTitle(tr("Tool Tips"));
    resize(500, 300);
    //创建3个Item,放入shapeItems变量中,通过paintEvent绘制。
    createShapeItem(circlePath, tr("Circle"), initialItemPosition(circlePath),
                    initialItemColor());
    createShapeItem(squarePath, tr("Square"), initialItemPosition(squarePath),
                    initialItemColor());
    createShapeItem(trianglePath, tr("Triangle"),
                    initialItemPosition(trianglePath), initialItemColor());
}

bool SortingBox::event(QEvent *event)
{
    if (event->type() == QEvent::ToolTip) {
        QHelpEvent *helpEvent = static_cast<QHelpEvent *>(event);
        int index = itemAt(helpEvent->pos());
        //如果ToolTip事件的位置找到了图形则显示toolTip,否则忽略事件
        if (index != -1) {
            QToolTip::showText(helpEvent->globalPos(), shapeItems[index].toolTip());
        } else {
            QToolTip::hideText();
            event->ignore();
        }

        return true;
    }
    return QWidget::event(event);
}

void SortingBox::resizeEvent(QResizeEvent * /* event */)
{
    int margin = style()->pixelMetric(QStyle::PM_DefaultTopLevelMargin);
    int x = width() - margin;
    int y = height() - margin;

    y = updateButtonGeometry(newCircleButton, x, y);
    y = updateButtonGeometry(newSquareButton, x, y);
    updateButtonGeometry(newTriangleButton, x, y);
}

void SortingBox::paintEvent(QPaintEvent * /* event */)
{
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);
    for (const ShapeItem &shapeItem : qAsConst(shapeItems)) {
        painter.translate(shapeItem.position());
        painter.setBrush(shapeItem.color());
        painter.drawPath(shapeItem.path());
        painter.translate(-shapeItem.position());
    }
}

//需要移动物体,必须先点击一次。
void SortingBox::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton) {
        int index = itemAt(event->pos());
        if (index != -1) {
            itemInMotion = &shapeItems[index];
            previousPosition = event->pos();
            //调整vector中的位置,在本例中可以忽略
            shapeItems.move(index, shapeItems.size() - 1);
           update();
        }
    }
}

void SortingBox::mouseMoveEvent(QMouseEvent *event)
{
    if ((event->buttons() & Qt::LeftButton) && itemInMotion)
        moveItemTo(event->pos());
}

void SortingBox::moveItemTo(const QPoint &pos)
{
    QPoint offset = pos - previousPosition;
    itemInMotion->setPosition(itemInMotion->position() + offset);
    previousPosition = pos;
    update();
}
void SortingBox::mouseReleaseEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton && itemInMotion) {
        moveItemTo(event->pos());
        itemInMotion = nullptr;
    }
}

//创建新的图形,只需要通过createShapeItem函数
//将图形放入shapeItems即可
void SortingBox::createNewCircle()
{
    static int count = 1;
    createShapeItem(circlePath, tr("Circle <%1>").arg(++count),
                    randomItemPosition(), randomItemColor());
}

void SortingBox::createNewSquare()
{
    static int count = 1;
    createShapeItem(squarePath, tr("Square <%1>").arg(++count),
                    randomItemPosition(), randomItemColor());
}

void SortingBox::createNewTriangle()
{
    static int count = 1;
    createShapeItem(trianglePath, tr("Triangle <%1>").arg(++count),
                    randomItemPosition(), randomItemColor());
}

int SortingBox::itemAt(const QPoint &pos)
{
    for (int i = shapeItems.size() - 1; i >= 0; --i) {
        const ShapeItem &item = shapeItems[i];
        if (item.path().contains(pos - item.position()))
            return i;
    }
    return -1;
}


int SortingBox::updateButtonGeometry(QToolButton *button, int x, int y)
{
    QSize size = button->sizeHint();
    button->setGeometry(x - size.rwidth(), y - size.rheight(),
                        size.rwidth(), size.rheight());

    return y - size.rheight()
           - style()->pixelMetric(QStyle::PM_DefaultLayoutSpacing);
}

void SortingBox::createShapeItem(const QPainterPath &path,
                                 const QString &toolTip, const QPoint &pos,
                                 const QColor &color)
{
    ShapeItem shapeItem;
    shapeItem.setPath(path);
    shapeItem.setToolTip(toolTip);
    shapeItem.setPosition(pos);
    shapeItem.setColor(color);
    shapeItems.append(shapeItem);
    update();
}

QToolButton *SortingBox::createToolButton(const QString &toolTip,
                                          const QIcon &icon, const char *member)
{
    QToolButton *button = new QToolButton(this);
    button->setToolTip(toolTip);
    button->setIcon(icon);
    button->setIconSize(QSize(32, 32));
    connect(button, SIGNAL(clicked()), this, member);

    return button;
}

//程序启动时会出现三个图形,这里计算三个图形的位置
//y是固定的,x的值与shapeItems当前的大小有关
QPoint SortingBox::initialItemPosition(const QPainterPath &path)
{
    int x;
    //qRound为四色五入取整
    //controlPointRect为包围盒, 比boundingRect速度更快
    //从底部抬高两个模型高度
    int y = (height() - qRound(path.controlPointRect().height()) *2);
    //shapeItems.size为在此之前创建的item数量
    if (shapeItems.size() == 0)
        x = ((3 * width()) / 2 - qRound(path.controlPointRect().width())) / 2;
    else
        x = (width() / shapeItems.size()
             - qRound(path.controlPointRect().width())) / 2;

    return QPoint(x, y);
}

QPoint SortingBox::randomItemPosition()
{
    return QPoint(QRandomGenerator::global()->bounded(width() - 120), QRandomGenerator::global()->bounded(height() - 120));
}

QColor SortingBox::initialItemColor()
{
    return QColor::fromHsv(((shapeItems.size() + 1) * 85) % 256, 255, 190);
}

QColor SortingBox::randomItemColor()
{
    return QColor::fromHsv(QRandomGenerator::global()->bounded(256), 255, 190);
}

按钮位置:
QT5.14.2自带Examples:Tool Tips_第2张图片

初始图形位置:QT5.14.2自带Examples:Tool Tips_第3张图片
显示提示:
QT5.14.2自带Examples:Tool Tips_第4张图片
移动图形:
QT5.14.2自带Examples:Tool Tips_第5张图片

main函数

#include 

#include "sortingbox.h"

int main(int argc, char *argv[])
{
    Q_INIT_RESOURCE(tooltips);

    QApplication app(argc, argv);
    SortingBox sortingBox;
    sortingBox.show();
    return app.exec();
}

你可能感兴趣的:(QT5.14.2自带Examples:Tool Tips)