一、Qt Widgets 问题交流
1.
二、Qt Quick 问题交流
1.Control 组件(如 Button 等)的 hoverEnabled 属性
Control2 中 Control 组件(如 Button 等)的 hoverEnabled 属性受到祖先节点该属性设置的影响,如果没有显式设置他就会往上找带这个属性的节点。比如 Button 正好放了个 MouseArea 作为父节点,此时 MouseArea 默认 hoverEnabled = false 就会导致 Button 没有 hover 触发。
源码如下:
bool QQuickControlPrivate::calcHoverEnabled(const QQuickItem *item)
{
const QQuickItem *p = item;
while (p) {
// QQuickPopupItem accepts hover events to avoid leaking them through.
// Don't inherit that to the children of the popup, but fallback to the
// environment variable or style hint.
if (qobject_cast(p))
break;
if (const QQuickControl *control = qobject_cast(p))
return control->isHoverEnabled();
QVariant v = p->property("hoverEnabled");
if (v.isValid() && v.userType() == QMetaType::Bool)
return v.toBool();
p = p->parentItem();
}
bool ok = false;
int env = qEnvironmentVariableIntValue("QT_QUICK_CONTROLS_HOVER_ENABLED", &ok);
if (ok)
return env != 0;
// TODO: QQuickApplicationWindow::isHoverEnabled()
return QGuiApplication::styleHints()->useHoverEffects();
}
2.ListView positionViewAtEnd() 没有滚动到底部
参照 Qt Bug:https://bugreports.qt.io/browse/QTBUG-28474
有时调用该函数没法滚动到底部,还有一点点距离,解决方法是等到 ListView 的 contentHeight 更改后再调用 positionViewAtEnd()。
通过 glGetString(GL_VERSION) 可以获取到 OpenGL 的版本号,但调用这个函数前需要先初始化上下文环境。
Qt 也封装了这个函数,在 QOpenGLFunctions 类中,而构造 QOpenGLContext 上下文还需要一个 QSurface(QWindow 或者 QOffscreenSurface),而 Surface 又需要 QScreen ,QScreen 指针可以通过 QGuiApplication 获取。最终代码如下:
#include
#include
#include
#include
#include
#include
#include
#include
#include
bool parseOpenGLVersion(const QByteArray &versionString, int &major, int &minor)
{
bool majorOk = false;
bool minorOk = false;
QList parts = versionString.split(' ');
if (versionString.startsWith(QByteArrayLiteral("OpenGL ES"))) {
if (parts.size() >= 3) {
QList versionParts = parts.at(2).split('.');
if (versionParts.size() >= 2) {
major = versionParts.at(0).toInt(&majorOk);
minor = versionParts.at(1).toInt(&minorOk);
// Nexus 6 has "OpenGL ES 3.0V@95.0 (GIT@I86da836d38)"
if (!minorOk)
if (int idx = versionParts.at(1).indexOf('V'))
minor = versionParts.at(1).left(idx).toInt(&minorOk);
} else {
qWarning("Unrecognized OpenGL ES version");
}
} else {
// If < 3 parts to the name, it is an unrecognised OpenGL ES
qWarning("Unrecognised OpenGL ES version");
}
} else {
// Not OpenGL ES, but regular OpenGL, the version numbers are first in the string
QList versionParts = parts.at(0).split('.');
if (versionParts.size() >= 2) {
major = versionParts.at(0).toInt(&majorOk);
minor = versionParts.at(1).toInt(&minorOk);
} else {
qWarning("Unrecognized OpenGL version");
}
}
if (!majorOk || !minorOk)
qWarning("Unrecognized OpenGL version");
return (majorOk && minorOk);
}
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_UseDesktopOpenGL);
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
#endif
QGuiApplication app(argc, argv);
int major=0;
int minor=0;
//一般需要先构造一个QGuiApplication有些函数才能调用
QScreen *screen=app.primaryScreen();
if(screen){
QOffscreenSurface surface(screen);
surface.create();
QOpenGLContext context;
context.create();
context.makeCurrent(&surface);
const GLubyte *glstr=context.functions()->glGetString(GL_VERSION);
if(glstr){
QByteArray bytestr= QByteArray(reinterpret_cast(glstr));
qDebug()
关注
打赏
最近更新
- 深拷贝和浅拷贝的区别(重点)
- 【Vue】走进Vue框架世界
- 【云服务器】项目部署—搭建网站—vue电商后台管理系统
- 【React介绍】 一文带你深入React
- 【React】React组件实例的三大属性之state,props,refs(你学废了吗)
- 【脚手架VueCLI】从零开始,创建一个VUE项目
- 【React】深入理解React组件生命周期----图文详解(含代码)
- 【React】DOM的Diffing算法是什么?以及DOM中key的作用----经典面试题
- 【React】1_使用React脚手架创建项目步骤--------详解(含项目结构说明)
- 【React】2_如何使用react脚手架写一个简单的页面?