String into Slider Value (amatour here)



  • Hey guys,

    not sure I posted on the right place but got this problem. Maybe some of you know how to do this:)

    I am getting a value into ValueMain (from other sources) in the .cpp file.

    setTextLabel1(ValueMain);
    

    This value need to be assigned to QString for output. (in the same .cpp file)

    void Controlview::SetPageID(QString pageID)
    {
        m_pageID = pageID;
    }
    
    QString Controlview::GetPageID()
    {
        return m_pageID;
    }
    
    QString Controlview::textLabel1()const
    {
        return m_textlabel1;
    }
    
    void Controlview::setTextLabel1(const QString &textLabel1)
    {
        m_textlabel1 = textLabel1;
    }
    

    I would like to use this valueMain not just to output the value on text but to make the slider / progressBar to "act" in the main.qml file:

    import QtQuick 2.0
    
    Rectangle {
        width: 480
        height: 272
        z: -15
        color:"#060C10";
        border.width: 1;
        border.color: "#000000";
    
        Controlview {
            id: view
    
            onPageID:
            {
                text1.text = textLabel1;
            }
        }
    
        Rectangle {
            id: pageid
            width: 480
            height: 270
            color: "#060C10"
    
            property color color1: "#25333D"
            property color color2: "#00C70E" 
            property color color3: "#25333D"
            property color color4: "black" 
            property color color5: "black" 
            property color color6: "#00AC0C" 
    
            //-------------------- Slider Start --------------------
    
            Rectangle {
                x: 35
                y: 50
                width: 320
                height: 15
                border.color: "black"
                border.width: 0
    
                radius: 6
                gradient: Gradient {
                    GradientStop { position: 0.0; color: "#767E84" }
                    GradientStop { position: 1.0; color: "#767E84" }
                }
    
                Item {
                    id: slider
                    width: 266
                    height: 15
    
                    property real value: //Would love to have the ValueMain here (maybe converted from textLabel1 to an Integer or anything like that.
                    property real maximum: 100
                    property real minimum: 1
                    property int xMax: width - handle.width; onXMaxChanged: updatePos(); onminimumChanged: updatePos();
    
                    function updatePos() {
                        if (maximum > minimum) {
                            var pos = (value - minimum) * slider.xMax / (maximum -minimum);
                            pos = Math.min(pos, width - handle.width);
                            pos = Math.max(pos, 0);
                            handle.x = 250 - pos;
                        } else {
                            handle.x = 2;
                        }
                    }
    
                    Rectangle {
                        anchors.fill: parent
                        border.color: "black"
                        border.width: 0
                        radius: 6               
                        }
                    }
    
                    Rectangle {
                        id: handle
                        smooth: true
                        border.color: "black"
                        border.width: 0
                        width: 16
                        height: slider.height
                        radius: 6                  
    
                        Rectangle {
                            id: handle1
                            smooth: true
                            x: 0
                            y: -47
                            border.color: "black"
                            border.width: 0
                            width: 60
                            height: 45
                            radius: 6
    
                            Rectangle {
                                x: 45
                                y: 0
                                width: 77
                                height: 15
                                opacity: 1
                                border.color: "black"
                                border.width: 0
                                radius: 6
                            }
    
                            Text {
                                id: text1 // here I am getting the value as numbers, works perfect.
                                color: pageid.color4
                                font.bold: true
                                font.pixelSize: 28
                                anchors.verticalCenter: parent.verticalCenter
                                anchors.top: handle1.bottom
                                anchors.topMargin: - 29
                                anchors.horizontalCenter: parent.horizontalCenter
                            }
    
                            Text {
                                text: "Bar"
                                color: pageid.color5
                                font.pixelSize: 10
                                anchors.verticalCenter: parent.verticalCenter
                                anchors.top: handle1.bottom
                                anchors.topMargin: - 43
                                anchors.right:handle1.right
                                anchors.rightMargin: - 40
                            }
                        }
                    }
    
                    Rectangle {
                        id: info
                        smooth: true
                        x: 312
                        y: -30
                        border.color: "black"
                        border.width: 0
                        width: 60
                        height: 45
                        radius: 6
    
                    Text {
                        id: text2 
                        color: pageid.color4
                        font.bold: true
                        font.pixelSize: 28
                        anchors.verticalCenter: parent.verticalCenter
                        anchors.top: info.bottom
                        anchors.topMargin: - 46
                        anchors.horizontalCenter: parent.horizontalCenter
                    }
    
                    Text {
                        text: "Cm"
                        color: pageid.color5
                        font.pixelSize: 10
                        anchors.verticalCenter: parent.verticalCenter
                        anchors.top: info.bottom
                        anchors.topMargin: - 13
                        anchors.right: info.right
                        anchors.rightMargin: 6
                    }
                }
            }
    
                Rectangle {
                    x: 120
                    y: 15
                    width: 210
                    height: 15
                    radius: 6
                    gradient: Gradient {
                        GradientStop { position: 0.0; color: pageid.color1 }
                        GradientStop { position: 1.0; color: pageid.color1 }
                    }
                }
    
            }
    

    Hopefully you understand what I would like to do. I am sorry about the messy explonation and code. I am an absolute begginer.

    Thank you for your help and have a great day:)

    /Alv



  • @Alvery You can directly assign

    property real value: textLabel1
    

    if textLabel1 has real value as QString



  • @Yashpal said in String into Slider Value (amatour here):

    property real value: textLabel1

    Thank you for your fast replay:)

    I have tried it, got this message:

    ReferenceError: textLabel1 is not defined
    

    Not sure why:(



  • @Alvery You missed to reference textLabel1 by parent id 'view'



  • Thank you again Yashpal:)

    I did the following in the slider:

    property real value: view.textLabel1
    

    I am not getting any error message but unfortunately my Slider wont move. If I set a constant value like 80 instead of view.textLabel1, it would move instantly.

    What am I doing worng?

    I am sorry about my amatour questions. It would mean a lot if we could find a solution.

    Thank you again for your time!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.