具有多个图标和文本的复杂按钮
Complex push button with several icons and text
我需要创建一个按钮,其中可能包含两个图标和两个要显示的文本标签(在屏幕截图上 - "Email" 或 "Connection" 等每一行都是一个独立按钮)。
据我了解 - 我无法覆盖 QPushButton 或 QAbstractButton,我必须实现我自己的自定义小部件才能实现这种布局。到目前为止,我正在尝试使用 Qt Designer 制作此类按钮,并使用 QSS 和动态属性更改小部件的样式,但由于某种原因,当我按下按钮时,样式 sheet 没有改变。我的代码:
namespace ui
{
ViewEntryPushButton::ViewEntryPushButton(QWidget* parent)
: QWidget(parent)
, ui(new Ui::ViewEntryPushButton)
{
ui->setupUi(this);
}
ViewEntryPushButton::~ViewEntryPushButton()
{
delete ui;
}
void ViewEntryPushButton::mousePressEvent(QMouseEvent* event)
{
this->setProperty("pressed", true);
this->style()->unpolish(this);
this->style()->polish(this);
this->update();
}
void ViewEntryPushButton::mouseReleaseEvent(QMouseEvent *event)
{
this->setProperty("pressed", false);
this->style()->unpolish(this);
this->style()->polish(this);
this->update();
}
} // namespace ui
还有我的样式sheet,我在Qt Designer对象视图中应用到最高对象:
#ViewEntryPushButton {
background-color: transparent;
padding: 5px;
margin-left: 5px;
}
#ViewEntryPushButton[pressed="true"]{
background-color:rgb(51, 0, 255);
border-width: 3px;
border-color: rgba(255, 255, 255, 20);
border-style: inset;
}
所以我的问题是:
1.我的方法是正确的还是可以子类化QAbstractButton
并像这样实现?
2. 在这种情况下,我对样式 sheets 做错了什么,为什么当我按下鼠标时小部件的样式没有改变?
您是否尝试过在构造函数中使用这样的 setstylesheet 函数更改样式表。好吧,这只是另一种做事方式。
Button.setstylesheet("")
最终在所有其他层之上使用带有 QStackedLayout 和透明按钮的自定义小部件,我认为这是最好的方法。
我在布局的 "low" 层上创建了必要的标签,并在样式表中用 background:transparent;
属性 在它们上面添加了一个按钮。
还应在布局中将 StackingMode 设置为 StackAll
。
我需要创建一个按钮,其中可能包含两个图标和两个要显示的文本标签(在屏幕截图上 - "Email" 或 "Connection" 等每一行都是一个独立按钮)。
据我了解 - 我无法覆盖 QPushButton 或 QAbstractButton,我必须实现我自己的自定义小部件才能实现这种布局。到目前为止,我正在尝试使用 Qt Designer 制作此类按钮,并使用 QSS 和动态属性更改小部件的样式,但由于某种原因,当我按下按钮时,样式 sheet 没有改变。我的代码:
namespace ui
{
ViewEntryPushButton::ViewEntryPushButton(QWidget* parent)
: QWidget(parent)
, ui(new Ui::ViewEntryPushButton)
{
ui->setupUi(this);
}
ViewEntryPushButton::~ViewEntryPushButton()
{
delete ui;
}
void ViewEntryPushButton::mousePressEvent(QMouseEvent* event)
{
this->setProperty("pressed", true);
this->style()->unpolish(this);
this->style()->polish(this);
this->update();
}
void ViewEntryPushButton::mouseReleaseEvent(QMouseEvent *event)
{
this->setProperty("pressed", false);
this->style()->unpolish(this);
this->style()->polish(this);
this->update();
}
} // namespace ui
还有我的样式sheet,我在Qt Designer对象视图中应用到最高对象:
#ViewEntryPushButton {
background-color: transparent;
padding: 5px;
margin-left: 5px;
}
#ViewEntryPushButton[pressed="true"]{
background-color:rgb(51, 0, 255);
border-width: 3px;
border-color: rgba(255, 255, 255, 20);
border-style: inset;
}
所以我的问题是:
1.我的方法是正确的还是可以子类化QAbstractButton
并像这样实现?
2. 在这种情况下,我对样式 sheets 做错了什么,为什么当我按下鼠标时小部件的样式没有改变?
您是否尝试过在构造函数中使用这样的 setstylesheet 函数更改样式表。好吧,这只是另一种做事方式。
Button.setstylesheet("")
最终在所有其他层之上使用带有 QStackedLayout 和透明按钮的自定义小部件,我认为这是最好的方法。
我在布局的 "low" 层上创建了必要的标签,并在样式表中用 background:transparent;
属性 在它们上面添加了一个按钮。
还应在布局中将 StackingMode 设置为 StackAll
。