56 lines
1.7 KiB
C++
56 lines
1.7 KiB
C++
|
#include "myiconbutton.h"
|
||
|
#include <QFontMetrics>
|
||
|
#include <QHBoxLayout>
|
||
|
#include <QStyleOptionButton>
|
||
|
#include <QPainter>
|
||
|
|
||
|
MyIconButton::MyIconButton(QWidget *parent) :
|
||
|
QPushButton(parent)
|
||
|
{
|
||
|
m_iconButton = new QPushButton(this);
|
||
|
m_iconButton->setCheckable(false);
|
||
|
m_iconButton->setFixedSize(QSize(24, 24));
|
||
|
m_iconButton->setFocusPolicy(Qt::NoFocus);
|
||
|
m_iconButton->setFlat(true);
|
||
|
// m_iconButton->setStyleSheet("QPushButton:checked{border: none;}"
|
||
|
// "QPushButton:!checked{border: none;}");
|
||
|
|
||
|
m_textLabel = new QLabel(this);
|
||
|
QSizePolicy textLabelPolicy = m_textLabel->sizePolicy();
|
||
|
textLabelPolicy.setHorizontalPolicy(QSizePolicy::Fixed);
|
||
|
textLabelPolicy.setVerticalPolicy(QSizePolicy::Fixed);
|
||
|
m_textLabel->setSizePolicy(textLabelPolicy);
|
||
|
m_textLabel->setScaledContents(true);
|
||
|
|
||
|
QHBoxLayout *hLayout = new QHBoxLayout();
|
||
|
hLayout->setContentsMargins(8, 0, 0, 0);
|
||
|
hLayout->addWidget(m_iconButton, Qt::AlignCenter);
|
||
|
hLayout->addWidget(m_textLabel);
|
||
|
hLayout->addStretch();
|
||
|
setLayout(hLayout);
|
||
|
|
||
|
connect(m_iconButton, &QPushButton::clicked, this, &QPushButton::click);
|
||
|
}
|
||
|
|
||
|
MyIconButton::~MyIconButton()
|
||
|
{}
|
||
|
|
||
|
void MyIconButton::setThemeIcon(const QString &themeIconName)
|
||
|
{
|
||
|
m_themeIconName = themeIconName;
|
||
|
m_iconButton->setIcon(QIcon::fromTheme(themeIconName));
|
||
|
}
|
||
|
|
||
|
void MyIconButton::setDesplayText(const QString &text)
|
||
|
{
|
||
|
m_textLabel->setFixedWidth(this->width() - 40);
|
||
|
QFontMetrics fontMetrics(m_textLabel->font());
|
||
|
int fontSize = fontMetrics.width(text);
|
||
|
if (fontSize > m_textLabel->width()) {
|
||
|
m_textLabel->setText(fontMetrics.elidedText(text, Qt::ElideRight, m_textLabel->width()));
|
||
|
} else {
|
||
|
m_textLabel->setText(text);
|
||
|
}
|
||
|
}
|
||
|
|