如何使自定义小部件(从“QFrame”派生)包含可检查/可切换的支持?如何使Qt的样式表(CSS)知道我的小部件已被用户选中/切换?
我是否需要在我的自定义小部件中使用某种信号或属性?
我不想使用(即)QPushButton,因为我需要我的按钮小部件来使用Qt的.ui文件。
也就是说,当使用QPushButton时,事情很简单:
QPushButton#btnName:focus:pressed { border-image: url(ThemesPathKey/menu-top-h.png); }
QPushButton#btnName:checked { border-image: url(ThemesPathKey/menu-top-h.png); }
QPushButton#btnName:hover { border-image: url(ThemesPathKey/menu-top-h.png); }我需要类似的东西,我的自定义按钮小部件。
谢谢。
发布于 2014-11-13 14:06:12
您可以对自定义小部件使用hover和focus,但只支持按钮和复选框中的checked。
要替换checked,可以使用自定义属性:
QPushButton#btnName[checked="true"] { border-image: url(ThemesPathKey/menu-top-h.png); }单击小部件时,请切换checked属性,如下所示:
void mousePressEvent(QMouseEvent*) override
{
bool checked = property("checked").toBool();
setProperty("checked", !checked);
style()->polish(this);
}发布于 2014-11-13 14:03:49
对于悬停,可以对任何派生小部件使用相同的方法:
class MyClass : public QFrame
{
<...>
}
MyClass a;
a.setStyleSheet("MyClassa:hover{<...>}"); //this will work
a.setStyleSheet("MyClass#a:hover{<...>}"); //and this will work
a.setStyleSheet("QFrame:hover{<...>}"); //Even this will work, too检查/取消检查要复杂得多。框架没有检查状态,所以您必须自己实现它。
class MyClass : public QFrame
{
public:
<...>
protected:
void mousePressEvent(QMouseEvent * e) override
{
checked_ = !checked_;
if(checked)
setStyleSheet("border:1px solid black;");
else
setStyleSheet("border:0px;");
}
bool checked_;
}或者类似的东西
class MyClass : public QFrame
{
public:
<...>
protected:
void paintEvent(QPaintEvent * e) override
{
if(checked)
{
//draw something
}
else
{
//draw something else
}
}
bool checked_;
}https://stackoverflow.com/questions/26910389
复制相似问题