QListWidget --- How do you get the Item Value ?

1,496 views
Skip to first unread message

Bert Mariani

unread,
May 24, 2016, 3:25:10 PM5/24/16
to The Ring Programming Language

Hi Mahmoud


 In your example on:   http://ring-lang.net/   

   Desktop and Mobile development using RingQt Using the QListWidget Class

 How do you get the item value returned in func pWork

 I want to extract the word "three" rather than the currentRow "2" when select button is pressed.

 You can see what I tried below.


=========================

Load "guilib.ring"


New qApp {


win1 = new qWidget() {


setGeometry(100,100,400,400)


list1 = new qlistwidget(win1) {

setGeometry(150,100,200,200)

alist = ["one","two","three","four","five"]    # <<<  Item positions and values 

for x in alist additem(x) next

setcurrentrow(3,2)

win1.setwindowtitle("Items Count : " + count() )

}


btn1 = new qpushbutton(win1) {

setGeometry(10,200,100,30)

settext("selected item")

setclickevent("pWork()")

}


btn2 = new qpushbutton(win1) {

setGeometry(10,240,100,30)

settext("Delete item")

setclickevent("pWork2()")

}


show()

}


exec()

}


/*

From QListWidget Doc  http://doc.qt.io/qt-4.8/qlistwidget.html

   QListWidgetItem * currentItem() const

   int               currentRow() const

   QListWidgetItem * item(int row) const

*/


func pWork

  btn1.settext(string(list1.currentrow()))     


  See "1Row " + list1.currentRow() +nl        # <<< Returns Row number selected


### These do NOT work !!!

  #See " 2Item " + list1.currentItem() +nl 

  #See " 3Item " + list1.item() +nl

  #See " 4Item " + list1.item(2) +nl


  #See " 5Item " + string(list1.currentItem() ) +nl

  #See " 6Item " + string( list1.item() ) +nl

  #See " 7Item " + string(list1.item(2) ) +nl



func pWork2

list1 {

takeitem(currentrow())

}

Mahmoud Fayed

unread,
May 24, 2016, 5:04:35 PM5/24/16
to The Ring Programming Language
Hello Bert

To do that you need the QListWidgetItem class.
I have already added the class to RingQt

Just download the last binary update

Now the next sample demonstrates how to do this

Load "guilib.ring"


New qApp {

        win1 = new qWidget() {

                setGeometry(100,100,400,400)

                list1 = new qlistwidget(win1) {

                        setGeometry(150,100,200,200)

                       alist = ["one","two","three","four","five"]

                       for x in alist additem(x) next

                       setcurrentrow(3,2)

                       win1.setwindowtitle("Items Count : " + count() )

               }

              btn1 = new qpushbutton(win1) {

                        setGeometry(10,200,100,30)

                       settext("selected item")

                       setclickevent("pWork()")

               }

               btn2 = new qpushbutton(win1) {

                        setGeometry(10,240,100,30)

                       settext("Delete item")

                       setclickevent("pWork2()")

               }

               show()

        }

       exec()

}

func pWork

        if list1.currentrow() > 0

        btn1.settext( list1.item(list1.currentrow()).text() ) ok


func pWork2

        list1 {

               takeitem(currentrow())

       }




From the previous example 
the list.item() method will get the QListWidgetItem object using the item row 
Then using the text() method from the QListWidgetItem we get the value

References : 

Greetings,
Mahmoud

Bert Mariani

unread,
May 24, 2016, 7:57:47 PM5/24/16
to The Ring Programming Language
Thanks Mahmoud

It required the  last binary update to work.

There is a small error in the sample code. (Qt  uses  "Index 0" ) 
Can Ring-Lang take care of this so that it reports as  -- Index 1 --  (hate those index 0's )

This is the output from the extra statements I added.
However the Button  label itself never shows the value "one" 

     Row 4   Item five
     Row 3   Item four
     Row 2   Item three
     Row 1   Item two
     Row 0   Item one   <<<<<


func pWork
        
See "Row " + list1.currentRow()                                        # <<< added to debug
See "   Item " + list1.item( list1.currentrow() ).text() +nl     # <<< added to debug
        if list1.currentrow() > 0                                                              # <<< Error. the "Row 0 = one"
btn1.settext( list1.item(list1.currentrow()).text() ) 
ok

Mahmoud Fayed

unread,
May 25, 2016, 7:18:21 AM5/25/16
to The Ring Programming Language
Hello Bert

Thanks for reporting, Very soon i will update RingQt to increment the index when we get it
and decrements the index when we set it.

I will let you know about this update once i do it after finishing my current task (Ring for Mac OS X)

Greetings,
Mahmoud
Reply all
Reply to author
Forward
0 new messages