QPushButton不要在按钮上使用文字位移

时间:2018-02-07 15:59:26

标签: c++ qt qstylesheet

我正在创建自己的QPushButton并设置样式。我注意到的是,如果按下按钮并检查按钮,文本会稍微移位(这并不奇怪,因为这使它看起来像一个按钮)。我不希望这种情况发生。我已经尝试查看样式表属性,我可以更改以抑制此行为,但没有运气。有没有办法实现这个目标?

这是两个州的按钮。如果在图像查看器中依次叠加或查看,则可以看到文本位移。

enter image description here

enter image description here

以下是相关代码:

#include "ModeButton.h"

// The RGB colour codes that we use to help create our dynamic stylesheets. Longer term the colour codes may be moved to a global colour file
const QString ModeButton::m_rgbModeButtonEnabledBorder     = QString("rgb(102, 102, 102)");
const QString ModeButton::m_rgbModeButtonEnabledBackground = QString("rgb(153, 153, 153)");
const QString ModeButton::m_rgbModeButtonEnabledText       = QString("rgb(102, 102, 102)");
const QString ModeButton::m_rgbModeButtonDisabledBorder    = QString("rgb(82, 82, 82)");
const QString ModeButton::m_rgbModeButtonDisabledText      = QString("rgb(82, 82, 82)");
const QString ModeButton::m_rgbModeButtonCheckedText       = QString("rgb(0, 0, 0)");
const QString ModeButton::m_rgbModeButtonCheckedBackGround = QString("rgb(255, 153, 51)");

// We dynamically create our stylesheets so that we can separate out the colour from the rest of the styling
const QString ModeButton::m_styleSheetEnabledTemplate  = QString("QPushButton { color: TEXT_RGB; border: 0px solid BORDER_RGB; border-radius: 7px; background-color: BACKGROUND_RGB; }");
const QString ModeButton::m_styleSheetDisabledTemplate = QString("QPushButton:disabled { border: 0px solid BORDER_RGB; color: TEXT_RGB; }");
const QString ModeButton::m_styleSheetCheckedTemplate  = QString("QPushButton:checked { color: TEXT_RGB; border: 0px solid BORDER_RGB; border-radius: 7px; background-color: BACKGROUND_RGB; }");

ModeButton::ModeButton(OperatingModeButtonType a_OperatingModeButtonType, ChannelContainerWidget* ParentContainer, bool DoubleChanneled, int RowSpan)
  : QPushButton(GetOperatingModeButtonLabel(a_OperatingModeButtonType), (QWidget*)ParentContainer),
  m_OperatingModeButtonType(a_OperatingModeButtonType),
  m_DoubleChanneled(DoubleChanneled),
  m_RowSpan(RowSpan)
{
    ModifyStyle();
    setCheckable(true);
    setFixedWidth(-1);
}

//-----------------------------------------------------------------------------

QString ModeButton::GetEnabledStyleSheetString()
{
    QString styleSheetString = m_styleSheetEnabledTemplate;

    styleSheetString.replace("TEXT_RGB", m_rgbModeButtonEnabledText);
    styleSheetString.replace("BORDER_RGB", m_rgbModeButtonEnabledBorder);
    styleSheetString.replace("BACKGROUND_RGB", m_rgbModeButtonEnabledBackground);

    return styleSheetString;
}

//-----------------------------------------------------------------------------

QString ModeButton::GetDisabledStyleSheetString()
{
    QString styleSheetString = m_styleSheetDisabledTemplate;

    styleSheetString.replace("TEXT_RGB", m_rgbModeButtonDisabledText);
    styleSheetString.replace("BORDER_RGB", m_rgbModeButtonDisabledBorder);

    return styleSheetString;
}

//-----------------------------------------------------------------------------

QString ModeButton::GetCheckedStyleSheetString()
{
    QString styleSheetString = m_styleSheetCheckedTemplate;

    styleSheetString.replace("TEXT_RGB", m_rgbModeButtonCheckedText);
    styleSheetString.replace("BORDER_RGB", m_rgbModeButtonEnabledBorder);
    styleSheetString.replace("BACKGROUND_RGB", m_rgbModeButtonCheckedBackGround);

    return styleSheetString;
}

//-----------------------------------------------------------------------------

void ModeButton::ModifyStyle()
{
    setStyleSheet(GetEnabledStyleSheetString() + GetDisabledStyleSheetString() + GetCheckedStyleSheetString());
}

///////////////////////////////////////////////////////////////////////////////

1 个答案:

答案 0 :(得分:0)

将m_styleSheetEnabledTemplate&和m_styleSheetCheckedTemplate的填充属性设置为0可解决我的问题。我相信默认的填充值是不同的,这是实际实现文本位移的方式。感谢@ JohannesSchaub-litb关于尝试填充属性的建议。