Skip to content
  • 0 Votes
    3 Posts
    2k Views
    T

    Yes. mAppEngine is instance of QQmlApplicationEngine. I would like to assign one model to one ListView(in case of many tabs created). I tried this:

    SmsModel *model = new SmsModel(); model->createDummyData(); QObject* pRoot = mAppEngine->rootObjects()[0]; QObject* m_pTabView= pRoot->findChildren<QObject*>("conversationTabView").first(); if (m_pTabView) { QVariant returnedValue; QVariant title = phoneNumber; QQmlContext *context = new QQmlContext(mAppEngine, mAppEngine); context->setContextProperty(QString("myModel"), model); mAppEngine->rootContext()->setContextProperty("mmm", model); QQmlComponent *component = new QQmlComponent(mAppEngine, QUrl("qrc:/ChatView.qml"), this); QObject *object = component->create(context); QQmlEngine::setObjectOwnership(object, QQmlEngine::CppOwnership); object->setProperty("active", QVariant(true)); component->setParent(m_pTabView); QMetaObject::invokeMethod(m_pTabView, "addTab", Q_RETURN_ARG(QVariant, returnedValue), Q_ARG(QVariant, title), Q_ARG(QVariant, QVariant::fromValue(component))); object->setProperty("anchors.fill", "parent"); }

    and in QML:

    Rectangle { ... ListView { model: myModel } }

    I get this error "ReferenceError: myModel is not defined". When I do

    mAppEngine->rootContext()->setContextProperty("myModel", model);

    I have data in GUI, but when I create two tabs, then both ListViews share the same model(I want one model <-> one ListView).

  • 0 Votes
    10 Posts
    7k Views
    A

    Below is an answer.

    void MyWidget::changeEvent(QEvent *event)
    {
    if (e->type() == QEvent::LanguageChange) {
    titleLabel->setText(tr("Document Title"));
    ...
    okPushButton->setText(tr("&OK"));
    } else
    QWidget::changeEvent(event);
    }

    code within if (e->type() == QEvent::LanguageChange) { ... )
    should contain ALL the code which has to be called when language changed.
    Basically every line of the code with tr() has to be called there.

    In there you can put call to retranslateUi,
    but retranslateUi is just a function where QtDesigner put strings created in there.
    this might be or might not be sufficient for your widget.
    Finally there are people who do not use designer.
    That is why you could not find retranslateUi mentioned anywhere.
    Keyword is tr().

    For example you may fill tree widget outside of designer.
    If you used text which has to be internationalized there, you need to make sure the same code is executed as a reaction on language changed.

    Common logic suggests to put all code which requires initialization in a single function or slot.
    Such function or slot should be called when QEvent::LanguageChange arrives.
    Call this function myRetranslateUi if you want.

    You do not have to subclass every widget (as suggested by example) though.
    Alternative is to install event filter.

  • 0 Votes
    4 Posts
    3k Views
    T

    @nickaein

    My only advice is to make things as clear and understandable as possible. ColumnLayout and RowLayout are just convenience forms of GridLayout, and personally I think it's confusing to know exactly what each attached property Layout.somethingdoes :).

  • 2 Votes
    2 Posts
    3k Views
    P

    Still looking for a solution to this, any ideas?

  • 0 Votes
    2 Posts
    2k Views
    ?

    Hi chocis,

    Yes Yes, otherwise container will contain a bunch of undefined objects. The objects are destroyed immediately when destroy() is called and their memory is also freed immediately (the javascript garbage collector is not involved in this case). The objects are instantiated immediately when createObject() is called, but they are rendered the first time after returning from the onClicked javascript code block. Don't know. You need to measure. If your targeted hardware supports multiple threads: Create the objects outside the GUI thread. Either use incubateObject() in QML or do it in C++ with QThread. In addition: Create invisible objects when the CPU / GUI thread is idle and set them visible as soon as you need them. Don't know. You need to measure. Send signals from QML to other (C++) threads to terminate them. Don't know. Like you already stated, if the component you want to instantiate isn't in a separate qml file then Loader can't be used. Try incubateObject().

    Cheers!
    Wieland