c++ - 如何使用 C++ 在 Qt 中创建垂直(旋转)按钮

标签 c++ qt user-interface button qpushbutton

我想在 Qt 中创建一个垂直按钮(使用 C++,而不是 Python),文本顺时针或逆时针旋转 90º。标准 QPushButton 似乎不可能。

我该怎么做?

最佳答案

为了在 Qt 中创建一个垂直按钮,您可以将 QPushButton 子类化,以便转置小部件报告的尺寸,还可以修改绘图事件以正确对齐绘制按钮。

这里有一个名为 OrientablePushButton 的类,它可以用作传统 QPushButton 的直接替代品,但也可以通过使用 setOrientation< 来支持垂直方向.

方面:

Vertical button

示例用法:

auto anotherButton = new OrientablePushButton("Hello world world world world", this);
anotherButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Minimum);
anotherButton->setOrientation(OrientablePushButton::VerticalTopToBottom);

头文件:

class OrientablePushButton : public QPushButton
{
    Q_OBJECT
public:
    enum Orientation {
        Horizontal,
        VerticalTopToBottom,
        VerticalBottomToTop
    };

    OrientablePushButton(QWidget * parent = nullptr);
    OrientablePushButton(const QString & text, QWidget *parent = nullptr);
    OrientablePushButton(const QIcon & icon, const QString & text, QWidget *parent = nullptr);

    QSize sizeHint() const;

    OrientablePushButton::Orientation orientation() const;
    void setOrientation(const OrientablePushButton::Orientation &orientation);

protected:
    void paintEvent(QPaintEvent *event);

private:
    Orientation mOrientation = Horizontal;
};

源文件:

#include <QPainter>
#include <QStyleOptionButton>
#include <QDebug>
#include <QStylePainter>

OrientablePushButton::OrientablePushButton(QWidget *parent)
    : QPushButton(parent)
{ }

OrientablePushButton::OrientablePushButton(const QString &text, QWidget *parent)
    : QPushButton(text, parent)
{ }

OrientablePushButton::OrientablePushButton(const QIcon &icon, const QString &text, QWidget *parent)
    : QPushButton(icon, text, parent)
{ }

QSize OrientablePushButton::sizeHint() const
{
    QSize sh = QPushButton::sizeHint();

    if (mOrientation != OrientablePushButton::Horizontal)
    {
        sh.transpose();
    }

    return sh;
}

void OrientablePushButton::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);

    QStylePainter painter(this);
    QStyleOptionButton option;
    initStyleOption(&option);

    if (mOrientation == OrientablePushButton::VerticalTopToBottom)
    {
        painter.rotate(90);
        painter.translate(0, -1 * width());
        option.rect = option.rect.transposed();
    }

    else if (mOrientation == OrientablePushButton::VerticalBottomToTop)
    {
        painter.rotate(-90);
        painter.translate(-1 * height(), 0);
        option.rect = option.rect.transposed();
    }

    painter.drawControl(QStyle::CE_PushButton, option);
}

OrientablePushButton::Orientation OrientablePushButton::orientation() const
{
    return mOrientation;
}

void OrientablePushButton::setOrientation(const OrientablePushButton::Orientation &orientation)
{
    mOrientation = orientation;
}

关于c++ - 如何使用 C++ 在 Qt 中创建垂直(旋转)按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53135674/

相关文章:

c++ - 没有双号

c++ - 动画到数组

qt - QGridLayout 中包含在多个单元格中的小部件未居中

java - GUI JButton,如何将按钮(问题)分配给答案

java - 当管理员和其他普通用户按下相同的登录按钮时,如何让管理员和其他普通用户访问 Java 中的不同 GUI 到 MySQL 连接?

c++ - 打开转义文件时出现意外行为 :///URL in IE

c++ - 标准在哪里定义 volatile 变量可以更改?

c++ - 数据库打不开错误qt c++

c++ - Qt Creator 中的 "collect2: ld returned 1 exit status"

user-interface - STS(Spring 工具套件)2.7.2 @RequestMappings View 不显示