以下是一个继承自QLabel的箭头控件类的示例代码:

#include <QLabel>
#include <QPainter>

class ArrowLabel : public QLabel
{
public:
    ArrowLabel(QWidget *parent = nullptr) : QLabel(parent), m_arrowDirection(ArrowDirection::Up), m_arrowAngle(30)
    {
    }

    void setArrowDirection(ArrowDirection direction)
    {
        m_arrowDirection = direction;
        update();
    }

    void setArrowAngle(int angle)
    {
        m_arrowAngle = angle;
        update();
    }

protected:
    void paintEvent(QPaintEvent *event) override
    {
        QLabel::paintEvent(event);

        QPainter painter(this);
        painter.setRenderHint(QPainter::Antialiasing);

        // 获取箭头矩形区域
        QRect arrowRect = rect();
        arrowRect.setHeight(arrowRect.height() / 2);

        // 根据箭头方向旋转画布
        switch (m_arrowDirection)
        {
        case ArrowDirection::Up:
            painter.translate(width() / 2, height() / 2);
            break;
        case ArrowDirection::Down:
            painter.translate(width() / 2, height() / 2);
            painter.rotate(180);
            break;
        case ArrowDirection::Left:
            painter.translate(width() / 2, height() / 2);
            painter.rotate(-90);
            break;
        case ArrowDirection::Right:
            painter.translate(width() / 2, height() / 2);
            painter.rotate(90);
            break;
        }

        // 画箭头
        QPainterPath path;
        path.moveTo(-arrowRect.width() / 2, -arrowRect.height() / 2);
        path.lineTo(arrowRect.width() / 2, -arrowRect.height() / 2);
        path.lineTo(0, arrowRect.height() / 2);
        path.closeSubpath();

        painter.fillPath(path, Qt::black);

        // 旋转箭头角度
        if (m_arrowAngle != 0)
        {
            painter.rotate(m_arrowAngle);
            painter.drawPath(path);
        }
    }

private:
    enum class ArrowDirection
    {
        Up,
        Down,
        Left,
        Right
    };

    ArrowDirection m_arrowDirection;
    int m_arrowAngle;
};

在上面的代码中,我们创建了一个名为ArrowLabel的类,继承自QLabel,并添加了setArrowDirection()setArrowAngle()函数,用于设置箭头的方向和倾斜角度。

paintEvent()函数中,我们使用QPainter来绘制箭头。首先,我们根据箭头的方向旋转画布。然后,我们使用QPainterPath来绘制箭头形状,并使用fillPath()函数填充箭头的颜色。最后,如果箭头角度不为0,我们再次旋转画布,并使用drawPath()函数绘制旋转后的箭头。

为了使用ArrowLabel控件,你可以在Qt Designer中将QLabel控件提升为ArrowLabel,或者在代码中使用ArrowLabel的实例。

以下是一个使用ArrowLabel控件的示例:

#include <QApplication>
#include <QHBoxLayout>
#include <QMainWindow>

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    QMainWindow window;
    QWidget *centralWidget = new QWidget(&window);
    QHBoxLayout *layout = new QHBoxLayout(centralWidget);

    ArrowLabel *arrowLabel = new ArrowLabel(centralWidget);
    arrowLabel->setArrowDirection(ArrowLabel::ArrowDirection::Right);
    arrowLabel->setArrowAngle(45);
    layout->addWidget(arrowLabel);

    window.setCentralWidget(centralWidget);
    window.show();

    return app.exec();
}

在上面的示例中,我们创建了一个QMainWindow窗口,并在其中添加了一个ArrowLabel控件。我们设置了箭头的方向为右侧,并将箭头的倾斜角度设置为45度。最后,我们将centralWidget设置为窗口的中央部件,并显示窗口。

你可以根据需要自由控制箭头的方向和倾斜角度,通过调用setArrowDirection()setArrowAngle()函数来实现

QT C++写一个继承于qlabel的箭头控件类以便于我可以在布局管理器中将qlabel提升为箭头控件并给出接口可以自由控制箭头指向及倾斜角度

原文地址: https://www.cveoy.top/t/topic/hNRN 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录