我的项目在基于 Qt 5.1.1(MSVC 2010,32 位)的 QT Creator 2.8.1 中,并使用工具包编译:QT 5.1.1 MSVC2010 32 位。
问题是 webview 在 Win 7 (x64) 或 Win 8 下显示出一些奇怪的行为。第一个复选框始终具有焦点。如果我将鼠标悬停在第一个复选框上,第二个复选框就会“突出显示”。此外,我无法选中第一个复选框,如果我单击它,第二个复选框将被选中,而第一个复选框将保持未选中状态。
在 Win XP 下使用相同的 exe 或为 Linux 或 Mac 重新编译的项目不会发生错误。
HTML:
<p><input id="loginA" tabindex="3" type="checkbox" name="a" /> Test 1<br/></p>
<p><input id="loginB" tabindex="4" type="checkbox" name="b" /> Test 2<br/></p>
C++/QT5 中的 QWebview 部分:
QVariant result = this->webv->page()->mainFrame()->evaluateJavaScript(jscontent);
如果我通过 Web 检查器的控制台设置 checked 属性,它会显示与所述相同的行为,并且只有第二个被检查。
$(":checkbox").attr("checked", true)
[<input id="loginA" tabindex="3" type="checkbox" name="a" checked="checked">,
<input id="loginB" tabindex="4" type="checkbox" name="b" checked="checked">]
有什么建议吗?非常感谢您的帮助。
最佳答案
这是完整的解决方法:
创建一个扩展 QProxyStyle 的 CustomStyle 类:
#ifndef CUSTOMSTYLE_H
#define CUSTOMSTYLE_H
#include <QProxyStyle>
#include <QStyleOption>
class CustomStyle : public QProxyStyle
{
void drawControl(ControlElement element, const QStyleOption* option, QPainter* painter, const QWidget* widget = 0) const
{
if (element == QStyle::CE_CheckBox ||
element == QStyle::CE_RadioButton) {
option->styleObject->setProperty("_q_no_animation", true);
}
QProxyStyle::drawControl(element, option, painter, widget);
}
};
#endif //CUSTOMSTYLE_H
将 CustomStyle 设置为您的应用程序或 webview 组件:
// like this
QApplication::setStyle(new CustomStyle());
// or like this
ui->webview->setStyle(new CustomStyle());
请注意,您的 QWebView 组件不得设置其“styleSheet”属性。它必须是空的。否则它会以某种方式覆盖我们的自定义样式,并且解决方法将不起作用。
关于windows - QT 5.1.1 : Checkbox in QWebview shows strange behavior under Win 7 (x64)/Win 8,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19941258/