您当前的位置: 首页 > 

龚建波

暂无认证

  • 5浏览

    0关注

    313博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

《QDebug 2021年4月》

龚建波 发布时间:2021-04-16 00:11:43 ,浏览量:5

一、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()。

三、其他 1.Qt 获取 OpenGL 版本

通过 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()            
关注
打赏
1655829268
查看更多评论
0.0859s