如何省略 a 的文本QPushButton
?
问问题
1173 次
3 回答
1
基于省略标签的 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;
};
共产党:
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);
}
于 2019-01-23T15:08:51.680 回答
0
如果您的意思是添加省略号。下次谷歌更好: ElidedButton 类参考
于 2012-05-15T14:03:04.117 回答
-1
YourPushButton->setText("");
于 2012-05-15T09:26:00.300 回答