c++ - 如何创建一个在图标上显示文本的 QPushButton?

标签 c++ qt qpushbutton

我正在尝试在我的项目中创建一个 QPushButton,以便文本显示在自定义按钮图像或图标的顶部。 我尝试了以下方法:

imagePath = path;
QPixmap pixmap(imagePath);
QIcon ButtonIcon(pixmap);
button->setIcon(ButtonIcon);
button->setIconSize(pixmap.rect().size());
button->setGeometry(0,0,height,width);
button->setStyleSheet(
    "background-color: gray;"
    "border: 1px solid black;"
    "border-radius: "+QString::number(radius)+"px;"
    "color: lightGray; "
    "font-size: 25px;"
    );

当我在这里尝试使用 setText 时,它首先显示图标,然后在右侧显示文本。我希望文本显示在图标的顶部。

我也尝试了下面网上找到的方法:

imagePath = path;
button->setGeometry(0,0,height,width);
button->setStyleSheet("background-image: url(:/images/images/2adjacentTracksButton.png));"
                      "background-position: center center");

这个不接受我的 url 路径,因此没有在按钮上显示我需要的图像。

我该如何解决这个问题?

最佳答案

当涉及到操作按钮时,您可能想要创建自己的类,该类将实现QAbstractButton。像这样:

class MyButton : public QAbstractButton
{
    Q_OBJECT

public:
    static MyButton* createButton(QIcon icon, QWidget *parent);
    ~MyButton();

    void setText(QString);
    void setIcon(eIcon);
    void setOrientation(Qt::Orientation);

protected : 
    MyButton(QWidget *parent);

    // here, you can reimplement event like mousePressEvent or paintEvent

private :
    QBoxLayout*  m_ButtonLayout;
    QLabel*      m_IconLabel;
    QIcon        m_Icon;
    QLabel*      m_TextLabel;
}

.cpp 中:

MyButton::MyButton(QWidget *parent)
    : QAbstractButton(parent)
{    
    m_ButtonLayout = new QBoxLayout(QBoxLayout::LeftToRight, this);
    m_ButtonLayout->setAlignment(Qt::AlignCenter);
    m_ButtonLayout->setContentsMargins(0, 0, 0, 0);
    m_ButtonLayout->setSpacing(1);

    m_IconLabel = new QLabel(this);
    m_IconLabel->setAlignment(Qt::AlignCenter);
    m_ButtonLayout->addWidget(m_IconLabel);

    m_TextLabel = new QLabel(this);
    m_TextLabel->setAlignment(Qt::AlignCenter);
    m_ButtonLayout->addWidget(m_TextLabel);
    //m_TextLabel->hide();
}

MyButton* MyButton::createButton(QIcon icon, QWidget *parent)
{
    MyButton* pButton = new MyButton(parent);
    pButton->setIcon(icon);

    return pButton;
}

void MyButton::setText(QString text)
{
    m_TextLabel->setVisible(!text.isEmpty());
    m_TextLabel->setText(text);
    QAbstractButton::setText(text);
}

void MyButton::setIcon(QIcon icon)
{
    m_Icon = icon;
    m_IconLabel->setVisible(true);
}

void MyButton::setOrientation(Qt::Orientation orientation)
{
    if (orientation == Qt::Horizontal)
        m_ButtonLayout->setDirection(QBoxLayout::LeftToRight);
    else
        m_ButtonLayout->setDirection(QBoxLayout::TopToBottom);
}

现在您可以通过调用静态方法来创建带有图标的按钮:

MyButton* button = MyButton::createButton(myButtonIcon, this);

这只是我给你的一个基本示例,我不确定它是否有效(这是我前段时间做过的事情),但你可以试一试。希望对您有所帮助!

关于c++ - 如何创建一个在图标上显示文本的 QPushButton?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36781704/

相关文章:

c++ - 多线程变化需要更长的时间

c++ - 为什么交换运营商的提议被否决了?

c++ - 从 LabVIEW 调用的 DLL 写入 MATLAB 文件

qt - 在 mingw 中,qmake copy_dir 总是出错

c++ - 我们什么时候使用 QMutexLocker 重新锁定和解锁?

qt - PySide : How to get the clicked QPushButton object in the QPushButton clicked slot?

c++ - QPushButton 和 QToolButton 的区别

c++ - Visual Studio 没有正确执行代码

c++ - Qt C++ 从 QJsonValue 到 QByteArray 的转换

python - QPushButton,禁用所有其他按钮,除非再次按下