0

I` ve got such component as QMLDoubleEdit

import QtQuick 1.1
Item{
property double dVal: 0;
Rectangle {

    TextInput {
        id: textDVal
        focus:true
        validator:DoubleValidator{
            bottom:0;
            top:200;
            decimals: 3
        }           
        text: dVal           
    }
}

}

When it used to implement UI in for example

Item{
property double dWesMax:    0;
property double dWesMin:    0;


Rectangle {       
    QMLDoubleEdit {
        id: edtMaxWes
        x: 40
        y: 50
        dVal: dWesMax
    }

    QMLDoubleEdit {
        id: edtMinWes
        x: 260
        y: 50
        dVal: dWesMin
    }
 }
 }

I can set it initial values from my *.cpp code, but i can`t read them after changing:

// that is ok

QDeclarativeContext *context=m_qmlFrmParam->rootContext();
    context->setContextProperty("dWesMax",m_wntModule->getWesMax());

// that is not

double dVal=0;
dVal=(m_dlgRoot->property("dWesMax")).toDouble();

Better explain here: 1) Creating UI

void frmWesParam::prepareElements(){
m_qmlFrmParam=new QDeclarativeView();
m_qmlFrmParam->setSource(QUrl("qrc:/view/wesparams/QMLBgPanel.qml"));
m_qmlFrmParam->setResizeMode(QDeclarativeView::SizeRootObjectToView);
m_dlgRoot=m_qmlFrmParam->rootObject();
QDeclarativeContext *context=m_qmlFrmParam->rootContext();
context->setContextProperty("viewerWidget",this);
}

void frmWesParam::setWesMax(double dVal){
m_dlgRoot->setProperty("dWesMax",QString::number(dVal));
}

2) Showing it (just row of elements QMLDoubleEdit, and two buttons - "Ok","Cancel" onPress, emiting accept or cancel)

frmWesParam *vw=new frmWesParam();
vw->setWesMax(14.3);
vw->show();

3) When widget appears, it contain my value - 14.3. Here user can change it, and push button; 4) On signal accept i got

void frmWesParam::catchAccept(){
if(m_wntModule!=0){
double dVal=0;
dVal=(m_dlgRoot->property("dWesMax")).toDouble();
qDebug()<<dVal;
}     

And here i got only what i sat setWesMax(...) .

AlexBee
  • 368
  • 3
  • 13
  • What's `m_dlgRoot`? Isn't the context property you're setting in C++ different from the one in the QML code? Properties that you set using `setContextProperty` can be read using `contextProperty` member function of the same context. – W.B. Aug 19 '14 at 10:51
  • Ye, this is so, but i change value in QMLDoubleEdit, but can`t read; – AlexBee Aug 19 '14 at 10:59
  • m_qmlFrmParam=new QDeclarativeView(); .... m_dlgRoot=m_qmlFrmParam->rootObject(); – AlexBee Aug 19 '14 at 11:01
  • What do you mean can't read? Are you getting initial value or invalid QVariant? – W.B. Aug 19 '14 at 11:02
  • i`ve got values that i set there by setContext() – AlexBee Aug 19 '14 at 11:07
  • And how are you changing the value? In the QMLDoubleEdit? If so, you're not changing it at all, since your bindings only work in one direction. When you change the value in QMLDoubleEdit, dVal doesn't get updated. – W.B. Aug 19 '14 at 11:15
  • I change value in QMLDoubleEdit, then i`ve got button(qml) that sends – AlexBee Aug 19 '14 at 11:18
  • accept() - this signal i catch and after that try to use dVal; – AlexBee Aug 19 '14 at 11:18
  • I'm sorry, but can't expect help if you provide just shreds of relevant information. Please post full piece code showing what you're trying to achieve and where you're not getting results you're expecting. – W.B. Aug 19 '14 at 11:22
  • i changed my question, with some code to clear up what i do – AlexBee Aug 19 '14 at 11:44

2 Answers2

1

As I said in comments, changing values in TextInput does not change the dWesMax property. You should either read the text property of TextInput in C++, or have QML update dWesMax upon change in TextInput. Here's an example of how you could do it:

QMLDoubleEdit

import QtQuick 1.1
Item{
property double dVal: 0;
property var textChangedCb: null;
Rectangle {

    TextInput {
        id: textDVal
        focus:true
        validator:DoubleValidator{
            bottom:0;
            top:200;
            decimals: 3
        }           
        text: dVal
        onTextChanged: {dVal = text; if (textChangedCb) textChangedCb();}
    }
}

UI code

Item{
property double dWesMax:    0;
property double dWesMin:    0;


Rectangle {       
    QMLDoubleEdit {
        id: edtMaxWes
        x: 40
        y: 50
        dVal: dWesMax
        textChangedCb: {dWesMax = dVal;}
    }

    QMLDoubleEdit {
        id: edtMinWes
        x: 260
        y: 50
        dVal: dWesMin
        textChangedCb: {dWesMin = dVal;}
    }
 }
}

This is not the cleanest way to do it, but it will work without changing your C++ code and QML structure.

W.B.
  • 5,445
  • 19
  • 29
0

i`ve got annother solution based on help by W.B.

UI:

Item{
 property alias dWesMax:    edtMaxWes.dVal;
 property alias dWesMin:    edtMinWes.dVal;


 Rectangle {       
 QMLDoubleEdit {
     id: edtMaxWes
     x: 40
     y: 50
 }

 QMLDoubleEdit {
    id: edtMinWes
    x: 260
    y: 50
 }
}
}

QMLDoubleEdit:

Item{
 property double dVal: 0;

 Rectangle {
    TextInput {
        id: textDVal
        validator:DoubleValidator{
            bottom:0;
            top:200;
            decimals: 3
        }
        onTextChanged: {
            if(text.length>0){
              dVal=parseFloat(text);
            }
        }

    }
}
}
AlexBee
  • 368
  • 3
  • 13