简体   繁体   English

如何使用图标上显示的文本创建QPushButton?

[英]How to create a QPushButton with the text displayed over the icon?

I am trying to create a QPushButton in my project such that the text shows on top of the custom button image or icon. 我正在尝试在我的项目中创建一个QPushButton ,使文本显示在自定义按钮图像或图标的顶部。 I tried the below methods: 我尝试了以下方法:

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;"
    );

When I try to use the setText here, it shows the icon first and text on its right. 当我尝试在这里使用setText ,它首先显示图标,右边显示文本。 I want the text to appear on top of the icon. 我希望文本显示在图标的顶部。

I also tried the below method I found online: 我也试过我在网上找到的以下方法:

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

This one is not accepting my url path, hence is not displaying the image I need on the button. 这个不接受我的网址路径,因此没有在按钮上显示我需要的图像。

How can I solve this? 我怎么解决这个问题?

When it comes to manipulate button, you may want to do your own class, which will implement QAbstractButton . 当涉及到操作按钮时,你可能想要自己的类,它将实现QAbstractButton Something like this: 像这样的东西:

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;
}

In the .cpp : .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);
}

And now you can create your button with your icon by calling the static method: 现在,您可以通过调用静态方法创建带有图标的按钮:

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

It is just a basic example I gave you, and I am not sure it will work (this is a thing I did some time ago) but you can give it a shot. 这只是我给你的一个基本例子,我不确定它会起作用(这是我前段时间做过的事情),但你可以试一试。 Hope that helps ! 希望有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM