[Interest] QQuickView focus problem

Glenn Ramsey gr at componic.co.nz
Mon Nov 18 23:57:57 CET 2013


Hi,

I am having a problem getting a QML form with a text input to work properly when 
hosted by QQuickView. The problem is that the text input will only get the focus 
once. If the focus goes to another window on the desktop then clicking in the 
form does not return the focus to it. The issue is not present when the Qml is 
viewed in qmlscene via Qt Creator. This is on OSX 10.8.5 with Qt 5.1.1 built 
with macx-clang-32.

Example code is below. Is there something that I am omitting that is causing 
this behaviour?

Glenn

focus.qml:

import QtQuick 2.1
import QtQuick.Controls 1.0

Rectangle {

     id: main
     objectName: "main"
     width: 640
     height: 480

     signal closed

     onFocusChanged: {
         console.log("focus")
     }

     TextField {
         id: text_field1
         objectName: "text_field1"
         x: 93
         y: 299
         width: 471
         height: 22
         placeholderText: "Text Field"
         text: activeFocus ? "I have active focus!" : "I do not have active focus"

         onFocusChanged: {
             console.log("focus text_field1")
         }
     }

     Button {
         id: button1
         x: 488
         y: 397
         text: "Close"
         onClicked: {
             closed()
         }
     }
}

focus.cpp:

#include "focus.h"

#include <QCoreApplication>
#include <QWidget>
#include <QQmlEngine>
#include <QUrl>
#include <QProcess>
#include <QStringList>
#include <QQuickItem>
#include <iostream>

// Find the Qml libraries
QString qt5_qml_dir()
{
     QString program = "qmake";
     QStringList arguments;
     arguments << "-query" << "QT_INSTALL_QML";

     QProcess process;
     process.setProgram(program);
     process.setArguments(arguments);
     process.start();

     if (!process.waitForFinished())
         return "";

     QString result = process.readAllStandardOutput();
     std::cout << result.toUtf8().constData() << std::endl;
     return result;
}

Focus::Focus(QWidget *parent)
     : QDialog(parent)
{
     view_ = new QQuickView();
     view_->setResizeMode(QQuickView::SizeViewToRootObject);

     // Add Qt Quick library path
     view_->engine()->addImportPath(qt5_qml_dir());

     // The qml file will need to be copied into Contents/MacOS/
     view_->setSource(QUrl(QCoreApplication::instance()->applicationDirPath() + 
"/focus.qml"));

     QWidget *w = QWidget::createWindowContainer(view_, this);
     w->setMinimumSize(view_->size());
     w->setMaximumSize(view_->size());

     QQuickItem *root = view_->rootObject();
     connect(root, SIGNAL(closed()), this, SLOT(onClosed()));
}

void Focus::onClosed()
{
     accept();
}

main.cpp:

#include <QApplication>

#include "focus.h"

int main(int argc, char **argv)
{
     QApplication app (argc, argv);

     Focus f;
     f.show();
     return app.exec();
}



More information about the Interest mailing list