- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这很简单:我想在不禁用项目的情况下模仿被禁用的项目的颜色变化。
有 QTableWidgetItem
和 QStandardItem
项目,我正在使用这样的代码
item->setForeground( enabled ? QApplication::palette().color( QPalette::Text ) : QApplication::palette().color( QPalette::Disabled, QPalette::Text ) );
现在。但是,如果用户使用新调色板调用 QApplication::setPalette( ... )
,则必须手动刷新该项目。我宁愿设置一个 ColorGroup
和 Role
,这样 Qt 就知道如何刷新。有可能吗?
最佳答案
要实现自动化,您必须重写 QStyledItemDelegate 的 initStyleOption() 方法并将假启用与新角色相关联:
#include <QtWidgets>
enum FakeRoles {
FakeEnableRole = Qt::UserRole + 1000
};
class ForegroundDelegate: public QStyledItemDelegate
{
public:
using QStyledItemDelegate::QStyledItemDelegate;
protected:
void initStyleOption(QStyleOptionViewItem *option, const QModelIndex &index) const override{
QStyledItemDelegate::initStyleOption(option, index);
QVariant val = index.data(FakeRoles::FakeEnableRole);
if(val.canConvert<bool>()){
bool enabled = val.value<bool>();
option->palette.setBrush(QPalette::Text,
QApplication::palette().color(enabled ? QPalette::Active:
QPalette::Disabled, QPalette::Text));
}
}
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QTableWidget w(4, 4);
ForegroundDelegate *delegate = new ForegroundDelegate(&w);
w.setItemDelegate(delegate);
for(int i=0; i< w.rowCount(); ++i)
for (int j=0; j< w.columnCount(); ++j) {
QTableWidgetItem *it = new QTableWidgetItem(QString("%1-%2").arg(i).arg(j));
w.setItem(i, j, it);
bool enabled = QRandomGenerator::global()->bounded(0, 2) == 0;
it->setData(FakeRoles::FakeEnableRole, enabled);
}
w.show();
QTimer::singleShot(1000, [](){
QPalette pal = QApplication::palette();
pal.setColor(QPalette::Active, QPalette::Text, QColor("salmon"));
pal.setColor(QPalette::Disabled, QPalette::Text, QColor("cyan"));
QApplication::setPalette(pal);
});
return a.exec();
}
关于c++ - 模仿基于 QApplication::palette() 的颜色样式行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55617335/
这个问题在这里已经有了答案: How to test your code on a machine with big-endian architecture? (3 个答案) 关闭 4 年前。 我想
我正在使用 Mockito 对业务对象进行单元测试。业务对象使用通常从数据库获取数据的 DAO。为了测试业务对象,我意识到使用单独的内存 DAO(将数据保存在 HashMap 中)比编写所有 when
如何实现: if X in (1,2,3) then 而不是: if x=1 or x=2 or x=3 then 换句话说,如何才能最好地模仿 Excel 的 VBA 中的 IN 运算符? 最佳答案
我正在使用带有 Jabber-net 的 ejabberd 2。我想问你是否可以在使用管理员登录时冒充用户。意思是我希望能够使用管理员帐户登录并发送消息 From: 'User A', To: 'Us
当查询输入到 Excel 中的 Bloomberg BDH 函数中时,它会自动填充选定的单元格,并使用请求的数据填充下面的单元格。 我正在尝试创建一个执行类似操作的函数(尽管使用不同的数据源)。 BD
在 es6 中保留模拟静态属性的类的属性的最佳方法是什么? 使用原型(prototype)链中创建的属性安全吗? class Employee { constructor(name, creato
我有一个 HTML 文本输入 用户输入字符串。由于它是文本输入而不是文本区域,因此用户无法输入换行符。 用户可以在文本输入中输入 Markdown 元素,以便在稍后重新显示数据时格式化数据。然而,他们
我有一个快速的问题:我正在使用 Spyder,变量浏览器选项卡中有一个巧妙的功能,我可以单击标题并对列进行排序。但是,当我尝试使用时无法重现相同的排序顺序: df.sort_values() 在 py
这是一个有趣的挑战。我正在阅读 TypeScript github 中的这个老问题 support Extension Methods用法与C#类似。提出了两种主要方法,一种是添加语法糖来扩展经常令人
在 Java 中这是有效的: class Class1 { T t; ... } //Inside other class that has no relation to Class1 private
我有另一个对象,我希望它的颜色与 UITextField 的默认占位符文本颜色相同。 我知道我可以简单地创建一个具有相同颜色的新 UIColor,但如果 Apple 更改默认的 UITextField
我正在尝试为我的 ListView 项目构建一个类似于 Gmail 应用程序标签列表的布局,其中标签文本在左侧,计数在右侧。除了长文本,我的大部分工作都有效。我所得到的结果导致文本与计数重叠。 这就是
我用一副纸牌创建了一个圆圈,用户可以旋转它来选择一张纸牌。平移结束后,它会捕捉到指定的角度,并带有漂亮的减速动画。将来会有某种指示表明 45 度的卡是所选的卡。我想指出选择随着触觉反馈而改变,就像在
我有一个简单的链表类型和一个 Clone 的实现它: #[deriving(Show)] enum List { Cons(int, Box), Nil, } impl Clone f
所以我创建了自己的自定义 UIView,它看起来像一个警报,现在我想添加显示和隐藏动画。 我想为 AlertController 模仿 Apple 的默认动画。解雇是一个简单的淡入淡出动画,但是我不确
我正在尝试让我的 Javascript 生活变得更轻松一些(至少对于我的工作环境而言)。我试图将所有 Javascript 对象创建方法移至“类”中。我有以下代码(用于创建超链接): function
您将如何模仿 iPhone 的键盘输入。因此,当您单击一个时,会显示 1,然后显示 2,然后显示 12... 依此类推,并显示 ( ) -。我不想使用实际的电话应用程序,因为我正在创建一个虚假的拨号器
你好,我有一个问题,我需要一些指导/帮助来创建一个菜单,如果屏幕太小而无法显示原始菜单,该菜单将替换为一个按钮。我知道 Bootstrap 会为您做这件事,但由于实现限制,我无法使用该库。因此,我查看
我正在使用 UIBarButtonItems 来触发特殊操作,但我还想在屏幕底部添加另一个按钮,其尺寸与在 NavigationBar 中创建的按钮的尺寸相同,我该怎么做? 我可以在 Interfac
我可以原谅可能重复的问题,但我没有找到解决问题的方法。 Controller 加载时模拟按钮点击。它运行良好,直到我将 ng-click 更改为 on-tap( ionic 应用程序)。现在根本不起作
我是一名优秀的程序员,十分优秀!