QPushButton 不要在选中的按钮上使用文本位移
QPushButton Don't Use Text Displacement on Button Checked
我正在创建自己的 QPushButton 并设计它的样式。我注意到发生的事情是,如果按下并选中按钮,文本会稍微移位(不足为奇,因为这让它看起来像一个按钮)。我不希望这种情况发生。我试过查看 style-sheet 可以更改的属性以抑制这种行为,但没有成功。有什么办法可以实现吗?
下面是两种状态下的按钮。如果在图像查看器中一个接一个地叠加或查看,您可以看到文本位移。
相关代码如下:
#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());
}
///////////////////////////////////////////////////////////////////////////////
将 m_styleSheetEnabledTemplate 和 m_styleSheetCheckedTemplate 的 padding 属性都设置为 0 解决了我的问题。我相信默认的填充值是不同的,这就是实际实现文本位移的方式。感谢@JohannesSchaub-litb 建议尝试 padding 属性.
我正在创建自己的 QPushButton 并设计它的样式。我注意到发生的事情是,如果按下并选中按钮,文本会稍微移位(不足为奇,因为这让它看起来像一个按钮)。我不希望这种情况发生。我试过查看 style-sheet 可以更改的属性以抑制这种行为,但没有成功。有什么办法可以实现吗?
下面是两种状态下的按钮。如果在图像查看器中一个接一个地叠加或查看,您可以看到文本位移。
相关代码如下:
#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());
}
///////////////////////////////////////////////////////////////////////////////
将 m_styleSheetEnabledTemplate 和 m_styleSheetCheckedTemplate 的 padding 属性都设置为 0 解决了我的问题。我相信默认的填充值是不同的,这就是实际实现文本位移的方式。感谢@JohannesSchaub-litb 建议尝试 padding 属性.