如何忽略QPushButton
的文字?
答案 0 :(得分:0)
YourPushButton->setText("");
答案 1 :(得分:0)
如果你的意思是添加省略号。下次谷歌更好: ElidedButton Class Reference
答案 2 :(得分:0)
基于Qt示例的标签,我想出了以下解决方案。它主要基于Qt示例,我只是根据自己的要求对其进行了简化:
标题:
/**
* Push button with elided text
* \remark based on http://doc.qt.io/qt-5/qtwidgets-widgets-elidedlabel-example.html
*/
class CElidedPushButton : public QPushButton
{
Q_OBJECT
Q_PROPERTY(QString text READ text WRITE setText)
Q_PROPERTY(bool isElided READ isElided)
public:
//! Constructor
explicit CElidedPushButton(QWidget *parent = nullptr);
//! Constructor
explicit CElidedPushButton(const QString &text, QWidget *parent = nullptr);
//! Text
void setText(const QString &text);
//! Text
const QString &text() const { return m_content; }
//! Elided?
bool isElided() const { return m_elided; }
protected:
//! \copydoc QPushButton::paintEvent
void paintEvent(QPaintEvent *event) override;
private:
bool m_elided = false;
QString m_content;
};
CPP:
CElidedPushButton::CElidedPushButton(QWidget *parent) : QPushButton(parent)
{ }
CElidedPushButton::CElidedPushButton(const QString &text, QWidget *parent)
: QPushButton(parent), m_elided(false), m_content(text)
{
this->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
}
void CElidedPushButton::setText(const QString &newText)
{
m_content = newText;
this->update();
}
void CElidedPushButton::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
const QFontMetrics fontMetrics = painter.fontMetrics();
const int usableWidth = qRound(0.9 * this->width());
const QString elidedText = fontMetrics.elidedText(m_content, Qt::ElideRight, usableWidth);
m_elided = (elidedText != m_content);
QPushButton::setText(elidedText);
QPushButton::paintEvent(event);
}