Sorting - qtTableWidget - data items

301 views
Skip to first unread message

Bert Mariani

unread,
Sep 25, 2016, 9:22:55 AM9/25/16
to The Ring Programming Language
Hello ALL

1. How do you code  a "click change event"  in a qtTableWidget when clicking on a  Header Cell

2. See image of the  behaviour of qtSort when clicking on a CheckBox in this example. 
    The CheckBox moves from the top cell to the bottom cell.
     That Data Items are sorted 

3. I want to remove the CheckBoxes and use only the Header Cell when clicked to generate the Fun to sort the data.
----------------------------------------

The Header cells  are marked with  
      xxx-F  for False.  Sort A-Z
      xxx-T  for True.   Sort  Z-A

The code as written uses the CheckBoxes in Row 1. to generate an event which calls the Func  pBox1 that does a qtSort.
The strange behaviour is that the CheckBoxes end up in Row 7.
The Cell Items are sorted

    cBox1A = new qCombobox(Table2) { for i in cBox1List addItem(i,0) next setcurrentIndexChangedEvent($ObjectName+".pBox1A()") }

    func pBox1A
        See "func pBox-1A-2: " + cBox1A.currentText() +" Index: "+  cBox1A.currentIndex() +nl
        Table2.sortByColumn(0, False)
    return


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



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

Load "guilib.ring"

New qapp 
    $ObjectName   = "oSecondWindow"   ### $ObjectName use inside Class SecondWindow as($ObjectName+".pBox2X()")
    oSecondWindow = new SecondWindow  ### Class called SecondWindow
    
        See nl +nl +"===== ObjectName Second: ====== " +nl +nl
        See $ObjectName
        See nl +nl +"===== oSecondWindow: ========= " +nl +nl
        See oSecondWindow
        See nl +nl +"===================" +nl +nl
    
    exec()
}



###-----------------------------------------------
### Widget TABLE 2

Class SecondWindow

    win2 = new qwidget() 
    {       
        #symbolz = "Inside Win-2 symbolz"  ### shows OBJECT: symbolz: GOOGL  --- R6C0 = NULL instead of GOOGL --- Column 0 ALL = NULL
    
        setGeometry(30,40,700,400)
        setwindowtitle("Display qwidget - Win2 Table2 XYZW")
        
            Table2 = new qTableWidget(win2) 
            {
                #symbolz = "Inside Table-2 symbolz" ### shows OBJECT: symbolz: GOOGL R6C0 = NULL instead of GOOGL --- Column 0 ALL = NULL
                
                setrowcount(8)
                setcolumncount(6)
                
                setGeometry(20,20,800,600)
                setselectionbehavior(QAbstractItemView_SelectRows)
                setSelectionMode(QAbstractItemView_SelectItems)         
                
                SetColumnWidth(0,60)    # 
                SetColumnWidth(1,100)   # 
                SetColumnWidth(2,75)    #

                mycols = new qstringlist() 
                    {   append("ABLE-F") 
                        append("BAKER-T")
                        append("CHARLIE-F")
                        append("DOG-T") 
                        append("EASY-F")
                        append("FOX-T")
                    }
                    
                setHorizontalHeaderLabels(mycols)
                setAlternatingRowColors(true)
                    
                setStyleSheet("QHeaderView::section { background-color: aqua }")
                   
                horizontalHeader().setStyleSheet("color: blue")
                verticalHeader().setStyleSheet("color: red")  
                               
                cBox1List =[" ", "X", "Y", "Z", "W"]
                
                cBox1A = new qCombobox(Table2) { for i in cBox1List addItem(i,0) next setcurrentIndexChangedEvent($ObjectName+".pBox1A()") }   ### $ObjectName global
                cBox1B = new qCombobox(Table2) { for i in cBox1List addItem(i,0) next setcurrentIndexChangedEvent($ObjectName+".pBox1B()") }
                cBox1C = new qCombobox(Table2) { for i in cBox1List addItem(i,0) next setcurrentIndexChangedEvent($ObjectName+".pBox1C()") }

                cBox1D = new qCombobox(Table2) { for i in cBox1List addItem(i,0) next setcurrentIndexChangedEvent($ObjectName+".pBox1D()") }   ### $ObjectName global
                cBox1E = new qCombobox(Table2) { for i in cBox1List addItem(i,0) next setcurrentIndexChangedEvent($ObjectName+".pBox1E()") }
                cBox1F = new qCombobox(Table2) 
                        { for i in cBox1List 
                            addItem(i,0) 
                          next 
                          setcurrentIndexChangedEvent($ObjectName+".pBox1F()") 
                        }
                
                
                setCellWidget(0, 0, cBox1A)
                setCellWidget(0, 1, cBox1B)
                setCellWidget(0, 2, cBox1C)   
     
                setCellWidget(0, 3, cBox1D)
                setCellWidget(0, 4, cBox1E)
                setCellWidget(0, 5, cBox1F)   
                
                ###---------------------------
                ### Fill in Widget Table
                companyList = []
                
                aList = 
                [
                  [ "SPY",   "SP500",    "Index",  0, 109.24, 107.21 ],
                  [ "SDYL",  "SPDivs",   "ETF",    1, 152.33, 119.85 ],
                  [ "FB",    "Facebook", "Tech",   2, 143.34, 114.02 ],
                  [ "AMZN",  "Amazon",   "Cloud",  3, 154.80, 117.39 ],
                  [ "NVDA",  "Nvidia",   "Chips",  4, 287.49, 141.35 ],
                  [ "GOOGL", "Google",   "Search", 5, 126.70, 107.83 ]
                ]

                for x = 1 to 6
                    Add(companyList,  new Company { Symbolz=aList[x][1]  Namez=aList[x][2]  Groupz=aList[x][3]  Positionz=aList[x][4] Changez=aList[x][5]  Ratioz=aList[x][6]  } )
                next      

                ###------------------------------
                ### Put data in Widget Cells
                

                
                for x = 1 to len(companyList)               
                    
                    item1 = new qtablewidgetitem(companyList[x].Symbolz)
                    item2 = new qtablewidgetitem(companyList[x].Namez)
                    item3 = new qtablewidgetitem(companyList[x].Groupz)
                    item4 = new qtablewidgetitem("" + companyList[x].Positionz)
                    item5 = new qtablewidgetitem("" + companyList[x].Changez)
                    item6 = new qtablewidgetitem("" + companyList[x].Ratioz)
                    
                    setitem(x, 0, item1) 
                    setitem(x, 1, item2) 
                    setitem(x, 2, item3) 
                    setitem(x, 3, item4) 
                    setitem(x, 4, item5) 
                    setitem(x, 5, item6) 
    
                next
                
      
                setItem(1,3, new qTableWidgetItem("Item13") )
                setItem(3,1, new qTableWidgetItem("Item31") )               
          
            }
            
            ###-----------------------     
        show()
    }
 
###-----------------------

    ###------------------------------------
    ### TABLE 2 Methods
    
    func pBox1A
        See "func pBox-1A-2: " + cBox1A.currentText() +" Index: "+  cBox1A.currentIndex() +nl
        Table2.sortByColumn(0, False)
    return
            
    func pBox1B
        See "func pBox-1B-2: " + cBox1B.currentText() +" Index: "+  cBox1B.currentIndex() +nl
        Table2.sortByColumn(1, True)
    return
    
    func pBox1C
        See "func pBox-1C-2: " + cBox1C.currentText() +" Index: "+  cBox1C.currentIndex() +nl
        
            Table2.item(5,3).setText("Changed03")
            See "  Item-R4-C1: " + Table2.item(4,1).text() +nl
            See "  Item-R5-C2: " + Table2.item(5,2).text() +nl
        
        Table2.sortByColumn(2, False)
    return
    
    func pBox1D
        See "func pBox-1D-2: " + cBox1D.currentText() +" Index: "+  cBox1D.currentIndex() +nl
        Table2.sortByColumn(3, True)
    return
            
    func pBox1E
        See "func pBox-1E-2: " + cBox1E.currentText() +" Index: "+  cBox1E.currentIndex() +nl
        Table2.sortByColumn(4, False)
    return  
    
    func pBox1F
        See "func pBox-1F-2: " + cBox1F.currentText() +" Index: "+  cBox1F.currentIndex() +nl
        Table2.sortByColumn(5, True)
    return      
    
    
###--------------------------
 
Class Company 
   Symbolz   
   Namez 
   Groupz
   Positionz 
   Changez
   Ratioz
   
  

   
######################################


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


AA-Table-17c2-SortOneClassOneWindow.ring

CalmoSoft

unread,
Sep 25, 2016, 12:19:02 PM9/25/16
to The Ring Programming Language
Hello Bert,

If you want to sort a column by clicking on a header cell add the next line to the code:

setSortingEnabled(true)

Code:

                setSortingEnabled(true)

Greetings,
Gal Zsolt
(~ CalmoSoft ~)
AA-Table-17c2-SortOneClassOneWindow.ring

Bert Mariani

unread,
Sep 25, 2016, 7:44:29 PM9/25/16
to The Ring Programming Language
Hello Gal

Thank you for the suggestion.
When clicking in the Header Column -  It alternates the  SORT  up / down and displays a little indicator  ^ v  in the header column.

Question:
  How do you call a  Function  to handle the event after the Header Column is Clicked 
  How do you know which Header Column number was Clicked 

--------------------------------------------------------

### If  Header Column is Clicked.  Call this Function.  Get the Number of the column clicked on

    func pHeaderFn
        See "Func pHeaderFn called "    +nl
        See "Header column X clicked: " +nl
    return


-------------------------

AA-Table-17c3-SortOneClassOneWindow.ring

CalmoSoft

unread,
Sep 26, 2016, 6:51:40 AM9/26/16
to ring...@googlegroups.com
Hello Bert,

I have deleted the empty first row.

Code:

Load "guilib.ring"

New qapp 
    $ObjectName   = "oSecondWindow"   ### $ObjectName use inside Class SecondWindow as($ObjectName+".pBox2X()")
    oSecondWindow = new SecondWindow  ### Class called SecondWindow
     
    exec()
}


###-----------------------------------------------
### Widget TABLE 2

Class SecondWindow

    win2 = new qwidget() 
    {           
        setGeometry(30,40,700,400)
        setwindowtitle("Display qwidget - Win2 Table2 XYZ")
        
            Table2 = new qTableWidget(win2) 
            {
                setrowcount(8)
                setcolumncount(6)
                
                setGeometry(20,20,800,600)
                setselectionbehavior(QAbstractItemView_SelectRows)
                setSelectionMode(QAbstractItemView_SelectItems)         
                
                SetColumnWidth(0,60)    # 
                SetColumnWidth(1,100)   # 
                SetColumnWidth(2,75)    #

                mycols = new qstringlist() 
                    {   append("ABLE") 
                        append("BAKER")
                        append("CHARLIE")
                        append("DOG") 
                        append("EASY")
                        append("FOX")
                    }
                    
                setHorizontalHeaderLabels(mycols)
                setAlternatingRowColors(true)

                setStyleSheet("QHeaderView::section { background-color: aqua }")
                   
                horizontalHeader().setStyleSheet("color: blue")
                verticalHeader().setStyleSheet("color: red")  

                ###----------------------------------------------------------------------
                ### SORT Columns by Header Click, alternate sort every time it is clicked
                
                setSortingEnabled(true)     
                
                            
                ###---------------------------
                ### Fill in Widget Table
                companyList = []
                
                aList = 
                [
                  [ "SPY",   "SP500",    "Index",  0, 109.24, 107.21 ],
                  [ "SDYL",  "SPDivs",   "ETF",    1, 152.33, 119.85 ],
                  [ "FB",    "Facebook", "Tech",   2, 143.34, 114.02 ],
                  [ "AMZN",  "Amazon",   "Cloud",  3, 154.80, 117.39 ],
                  [ "NVDA",  "Nvidia",   "Chips",  4, 287.49, 141.35 ],
                  [ "GOOGL", "Google",   "Search", 5, 126.70, 107.83 ]
                ]

                for x = 1 to 6
                    Add(companyList,  new Company { Symbolz=aList[x][1]  Namez=aList[x][2]  Groupz=aList[x][3]  Positionz=aList[x][4] Changez=aList[x][5]  Ratioz=aList[x][6]  } )
                next      

                ###------------------------------
                ### Put data in Widget Cells
                

                
                for x = 1 to len(companyList)                                
                    item1 = new qtablewidgetitem(companyList[x].Symbolz)
                    item2 = new qtablewidgetitem(companyList[x].Namez)
                    item3 = new qtablewidgetitem(companyList[x].Groupz)
                    item4 = new qtablewidgetitem("" + companyList[x].Positionz)
                    item5 = new qtablewidgetitem("" + companyList[x].Changez)
                    item6 = new qtablewidgetitem("" + companyList[x].Ratioz)
                    
                    setitem(x-1, 0, item1) 
                    setitem(x-1, 1, item2) 
                    setitem(x-1, 2, item3) 
                    setitem(x-1, 3, item4) 
                    setitem(x-1, 4, item5) 
                    setitem(x-1, 5, item6) 
                next
                
                setItem(0,3, new qTableWidgetItem("Item03") )
                setItem(2,1, new qTableWidgetItem("Item21") )     
        
            }
            
            ###--------------------------     
        show()
    }
 
###--------------------------------------

    ###------------------
    ### TABLE 2 Methods
    
    func pHeaderFn
        See "Func pHeaderFn called "  +nl
        See "Header X clicked: " +nl
    return
            
###---------------------
 
Class Company 
   Symbolz   
   Namez 
   Groupz
   Positionz 
   Changez
   Ratioz
   
###---------------------



AA-Table-17c4-SortOneClassOneWindow.ring

Bert Mariani

unread,
Sep 26, 2016, 12:22:45 PM9/26/16
to The Ring Programming Language
Hello  Gal et ALL

When Clicking a  Header Column #,   Qt  Sorts the table widget.  Good.

How do I code a call to a Function  to do other things after the Header Column is Clicked
Also need to know which Header Column Number was clicked

This is the sample Function that I want to call.

    func pHeaderFn
        See "Func pHeaderFn called "    +nl
        See "Header column X clicked: " +nl



On Sunday, September 25, 2016 at 9:22:55 AM UTC-4, Bert Mariani wrote:
Hello ALL

1. How do you code  a "click change event"  in a qtTableWidget when clicking on a  Header Cell

2. See image of the  behaviour of qtSort when clicking on a CheckBox in this example. 
    The CheckBox moves from the top cell to the bottom cell.
     That Data Items are sorted 

3. I want to remove the CheckBoxes and use only the Header Cell when clicked to generate the Func to sort the data.

Mahmoud Fayed

unread,
Sep 26, 2016, 8:23:45 PM9/26/16
to ring...@googlegroups.com
Hello Bert and Gal

I have already updated RingQt to support QHeaderView class events


Then add the next code 

oMyHeader = new QHeaderView(1,win2)
oMyHeader.setSectionsClickable(true)
oMyHeader.setSectionClickedEvent($ObjectName+".pheaderfn()")
sethorizontalHeader(oMyHeader)
setHorizontalHeaderLabels(mycols)
horizontalHeader().setStyleSheet("color: blue")

The method code will be 
    func pHeaderFn
        See "Func pHeaderFn called "  +nl
        # The method geteventparameters() defined by RingQt to return a list of event parameters
        # The sectionClicked event contains one parameter : int logicalIndex
        x = self.oMyHeader.geteventparameters()[1]   
        See "Header "+x+" clicked: " +nl   

Attached the source code file after adding the previous code

Greetings,
Mahmoud
gridheaderevent.ring

Bert Mariani

unread,
Sep 26, 2016, 10:51:39 PM9/26/16
to The Ring Programming Language
Hello Mahmoud

Thank you for updating RingQt to support QHeaderView class events.
And showing how to code a Header clicked event to call a Function 

The attached code will alternately Sort   up / down  when a Header is Clicked.
The called Func prints out the Top Row of the sorted column.

===============
OUTPUT

Func pHeaderFn called
Header 0 clicked:
Column: 0 TopRow: AMZN

Func pHeaderFn called
Header 0 clicked:
Column: 0 TopRow: SPY

Func pHeaderFn called
Header 1 clicked:
Column: 1 TopRow: Amazon

Func pHeaderFn called
Header 1 clicked:
Column: 1 TopRow: SPDivs

Func pHeaderFn called
Header 2 clicked:
Column: 2 TopRow: Chips

Func pHeaderFn called
Header 2 clicked:
Column: 2 TopRow: Tech

Func pHeaderFn called
Header 3 clicked:
Column: 3 TopRow: 1

Func pHeaderFn called
Header 3 clicked:
Column: 3 TopRow: Item03

Func pHeaderFn called
Header 4 clicked:
Column: 4 TopRow: 109.24

Func pHeaderFn called
Header 4 clicked:
Column: 4 TopRow: 287.49

Func pHeaderFn called
Header 5 clicked:
Column: 5 TopRow: 107.21

Func pHeaderFn called
Header 5 clicked:
Column: 5 TopRow: 141.35


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




On Sunday, September 25, 2016 at 9:22:55 AM UTC-4, Bert Mariani wrote:
gridheaderevent-2.ring

Mahmoud Fayed

unread,
Sep 27, 2016, 12:30:23 AM9/27/16
to The Ring Programming Language
Hello Bert

You are welcome, Keep up the good work :D

Greetings,
Mahmoud

CalmoSoft

unread,
Sep 27, 2016, 9:04:13 AM9/27/16
to ring...@googlegroups.com
Hello Bert, Mahmoud,

Now the program shows in the result window if the sorting type is ascending or descending.

I added the next lines:

header[x+1] += 1
if header[x+1] % 2 = 0 sorttype = "descending" else sorttype = "ascending" ok  
See "Header " + x +" clicked: " + sorttype + nl

Code:

Load "guilib.ring"

New qapp 
    toggle = False 
    header = list(6)

    $ObjectName   = "oSecondWindow"   ### $ObjectName use inside Class SecondWindow as($ObjectName+".pBox2X()")
    oSecondWindow = new SecondWindow  ### Class called SecondWindow
     
    exec()
}


###-----------------------------------------------
### Widget TABLE 2

Class SecondWindow

    win2 = new qwidget() 
    {           
        setGeometry(30,40,700,400)
        setwindowtitle("Display qwidget - Win2 Table2 XYZ")
        
            Table2 = new qTableWidget(win2) 
            {
                setrowcount(8)
                setcolumncount(6)
                
                setGeometry(20,20,800,600)
                setselectionbehavior(QAbstractItemView_SelectRows)
                setSelectionMode(QAbstractItemView_SelectItems)         
                
                SetColumnWidth(0,60)     
                SetColumnWidth(1,100)   
                SetColumnWidth(2,75)    

                mycols = new qstringlist() 
                    {   append("ABLE") 
                        append("BAKER")
                        append("CHARLIE")
                        append("DOG") 
                        append("EASY")
                        append("FOX")
                    }
                    
                ###----------------------------------
                ### Set Headers and Colors

                setHorizontalHeaderLabels(mycols)                              ### List
                   horizontalHeaderItem(4).setText("NbrChange")                ### Text
                setHorizontalHeaderItem(5, new QTableWidgetItem("NbrRatio"))   ### Item
                
                setAlternatingRowColors(true)
                setStyleSheet("QHeaderView::section { background-color: aqua }")                
                verticalHeader().setStyleSheet("color: red")  
                horizontalHeader().setStyleSheet("color: blue")

                ###---------------------------------------------------
                ### Enable Call to Func when Header Column is Clicked

                oMyHeader = new QHeaderView(1,win2)
                oMyHeader.setSectionsClickable(true)
                oMyHeader.setSectionClickedEvent($ObjectName+".pheaderfn()")
                sethorizontalHeader(oMyHeader)
                  
                            
                ###---------------------------
                ### Fill in Widget Table
                companyList = []
                
                aList = 
                [
                  [ "SPY",   "SP500",    "Index",  0, 109.24, 107.21 ],
                  [ "SDYL",  "SPDivs",   "ETF",    1, 152.33, 119.85 ],
                  [ "FB",    "Facebook", "Tech",   2, 143.34, 114.02 ],
                  [ "AMZN",  "Amazon",   "Cloud",  3, 154.80, 117.39 ],
                  [ "NVDA",  "Nvidia",   "Chips",  4, 287.49, 141.35 ],
                  [ "GOOGL", "Google",   "Search", 5, 126.70, 107.83 ]
                ]

                for x = 1 to 6
                    Add(companyList,  new Company { Symbolz=aList[x][1]  Namez=aList[x][2]  Groupz=aList[x][3]  Positionz=aList[x][4] Changez=aList[x][5]  Ratioz=aList[x][6]  } )
                next      

                ###------------------------------
                ### Put data in Widget Cells
                             
                for x = 1 to len(companyList)                                
                    item1 = new qtablewidgetitem(companyList[x].Symbolz)
                    item2 = new qtablewidgetitem(companyList[x].Namez)
                    item3 = new qtablewidgetitem(companyList[x].Groupz)
                    item4 = new qtablewidgetitem("" + companyList[x].Positionz)
                    item5 = new qtablewidgetitem("" + companyList[x].Changez)
                    item6 = new qtablewidgetitem("" + companyList[x].Ratioz)
                    
                    setitem(x-1, 0, item1) 
                    setitem(x-1, 1, item2) 
                    setitem(x-1, 2, item3) 
                    setitem(x-1, 3, item4) 
                    setitem(x-1, 4, item5) 
                    setitem(x-1, 5, item6) 
                next
                
                setItem(0,3, new qTableWidgetItem("Item03") )
                setItem(2,1, new qTableWidgetItem("Item21") )    
                

                ###----------------------------------------------------------------------
                ### SORT Columns by Header Click, Alternate Sort every time it is Clicked
                
                setSortingEnabled(true)     
     
            }
            
            ###--------------------------     
        show()
    }
 
###--------------------------------------

    ###------------------
    ### TABLE 2 Methods
    
    func pHeaderFn
        See "Func pHeaderFn called "  +nl

        # The method geteventparameters() defined by RingQt to return a list of event parameters
        # The sectionClicked event contains one parameter : int logicalIndex    

        x = self.oMyHeader.geteventparameters()[1] 
        header[x+1] += 1
        if header[x+1] % 2 = 0 sorttype = "descending" else sorttype = "ascending" ok  
        See "Header " + x +" clicked: " + sorttype + nl
        See "Column: " + x +" TopRow: "+ Table2.item(0,x).text() + nl + nl 

    return
            
###---------------------
 
Class Company 
   Symbolz   
   Namez 
   Groupz
   Positionz 
   Changez
   Ratioz
   
###---------------------


Greetings,
gridheaderevent-3.ring

Bert Mariani

unread,
Sep 27, 2016, 12:28:24 PM9/27/16
to The Ring Programming Language
Hello ALL

How do you  set the background color in a  widget table  Item ?

Tried:    Table2.item(6, 4).setBackground(color: yellow )   ???

After Clicking on a Header column, the data gets Sorted.
The Func pHeaderFn  is called

It does some calculations and outputs to -- Item Cell  6,4  to 7,5   -- the SumChg    AvgRatio
I want to color the Cell  Item  background to yellow


----------------------




On Sunday, September 25, 2016 at 9:22:55 AM UTC-4, Bert Mariani wrote:
AA-Table-17c5-SortOneClassOneWindow.ring

Mahmoud Fayed

unread,
Sep 27, 2016, 2:39:23 PM9/27/16
to The Ring Programming Language
Hello Gal

Thanks for the sample update, Keep up the good work :D

Greetings,
Mahmoud

Mahmoud Fayed

unread,
Sep 27, 2016, 2:40:18 PM9/27/16
to The Ring Programming Language
Hello Bert

The next code will set the cell background color.

mycolor = new qcolor() { setrgb(0,0,255,255) }

mybrush = new qbrush() { setstyle(1)  setcolor (mycolor) }

item(3,3).setbackground(mybrush)  


Greetings,
Mahmoud

Bert Mariani

unread,
Sep 27, 2016, 4:27:44 PM9/27/16
to The Ring Programming Language
Hello Mahmound

Thank you for the suggested code:

It is more complex than the sample C code for QT where colors and brushes do not have to be defined and set
###  tableWidget->item(8, 0)->setBackground(Qt::red);     <<< Sample C code 

It generates there errors depending on which line is activated:

    Line 169 Error (R3) : Calling Function without definition !: item
    In method pheaderfn() in file C:/RING/bin/AA-Table-17c5-SortOneClassOneWindow.ring
    called from line 16  In qapp_exec In method exec() in file ring_qt.ring
    called from line 11  in file C:/RING/bin/AA-Table-17c5-SortOneClassOneWindow.ring

    C:/RING/bin/AA-Table-17c5-SortOneClassOneWindow.ring Line (171) Syntax error
    C:/RING/bin/AA-Table-17c5-SortOneClassOneWindow.ring errors count : 1


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

###--------------------
###  Item cell color
###  tableWidget->item(8, 0)->setBackground(Qt::red);     <<< Sample C code 

  mycolor = new qcolor() { setrgb(0,0,255,255) }
  mybrush = new qbrush() { setstyle(1) setcolor(mycolor) }
  item(3,3).setbackground(mybrush)                         <<< Line 169

  Table2.item(6,4).setbackground(mybrush))                <<< Line 171

------------------------


On Sunday, September 25, 2016 at 9:22:55 AM UTC-4, Bert Mariani wrote:
AA-Table-17c5-SortOneClassOneWindow.ring

Mahmoud Fayed

unread,
Sep 27, 2016, 5:48:21 PM9/27/16
to ring...@googlegroups.com
Hello Bert

The setBackground() methods comes with more than one version (different parameters types)
I supported one of these versions in RingQt which accept QBrush 

I will support that other versions (In the beginning of RingQt i used to select one version of the method, later I added support for using many versions and using _2 and _3 and so on in the method name. Just i need to modify the configuration file to add the missing methods).

For the error message

Use Table2.item()
Because the item() method belong to the QTableWidget Class

I used it in my code because the code was inside the braces { } and I have access to the class.

Greetings,
Mahmoud
Message has been deleted

CalmoSoft

unread,
Sep 28, 2016, 4:55:02 AM9/28/16
to ring...@googlegroups.com
Hello Bert, Mahmoud,

I have changed the next lines:

item(3,3).setbackground(mybrush)        
Table2.item(6,4).setbackground(mybrush))

for the lines:

Table2.item(3,3).setbackground(mybrush)        
Table2.item(6,4).setbackground(mybrush)

Code:

Load "guilib.ring"

New qapp 
    toggle = False 
       header = list(6)

    $ObjectName   = "oSecondWindow"   ### $ObjectName use inside Class SecondWindow as($ObjectName+".pBox2X()")
    oSecondWindow = new SecondWindow  ### Class called SecondWindow
     
    exec()
}


###-----------------------------------------------
### Widget TABLE 2

Class SecondWindow

    win2 = new qwidget() 
    {           
        setGeometry(30,40,700,400)
        setwindowtitle("Display qwidget - Win2 Table2 XYZ")
        
            Table2 = new qTableWidget(win2) 
            {
                setrowcount(8)
                setcolumncount(6)
                
                setGeometry(20,20,800,600)
                setselectionbehavior(QAbstractItemView_SelectRows)
                setSelectionMode(QAbstractItemView_SelectItems)         
                
                SetColumnWidth(0,60)     
                SetColumnWidth(1,100)   
                SetColumnWidth(2,75)    

                mycols = new qstringlist() 
                    {   append("ABLE") 
                        append("BAKER")
                        append("CHARLIE")
                        append("DOG") 
                        append("EASY")
                        append("FOX")
                    }
                    
                ###----------------------------------
                ### Set Headers and Colors

                setHorizontalHeaderLabels(mycols)                              ### List
                   horizontalHeaderItem(4).setText("NbrChange")                ### Text
                setHorizontalHeaderItem(5, new QTableWidgetItem("NbrRatio"))   ### Item
                
                setAlternatingRowColors(true)
                setStyleSheet("QHeaderView::section { background-color: aqua }")                
                verticalHeader().setStyleSheet("color: red")  
                horizontalHeader().setStyleSheet("color: blue")

                ###---------------------------------------------------
                ### Enable Call to Func when Header Column is Clicked

                oMyHeader = new QHeaderView(1,win2)
                oMyHeader.setSectionsClickable(true)
                oMyHeader.setSectionClickedEvent($ObjectName+".pheaderfn()")
                sethorizontalHeader(oMyHeader)
                  
                            
                ###---------------------------
                ### Fill in Widget Table
                companyList = []
                
                aList = 
                [
                  [ "SPY",   "SP500",    "Index",  0, 109.24, 107.21 ],
                  [ "SDYL",  "SPDivs",   "ETF",    1, 152.33, 119.85 ],
                  [ "FB",    "Facebook", "Tech",   2, 143.34, 114.02 ],
                  [ "AMZN",  "Amazon",   "Cloud",  3, 154.80, 117.39 ],
                  [ "NVDA",  "Nvidia",   "Chips",  4, 287.49, 141.35 ],
                  [ "GOOGL", "Google",   "Search", 5, 126.70, 107.83 ]
                ]

                for x = 1 to 6
                    Add(companyList,  new Company { Symbolz=aList[x][1]  Namez=aList[x][2]  Groupz=aList[x][3]  Positionz=aList[x][4] Changez=aList[x][5]  Ratioz=aList[x][6]  } )
                next      

                ###------------------------------
                ### Put data in Widget Cells
                             
                for x = 1 to len(companyList)                                
                    item1 = new qtablewidgetitem(companyList[x].Symbolz)
                    item2 = new qtablewidgetitem(companyList[x].Namez)
                    item3 = new qtablewidgetitem(companyList[x].Groupz)
                    item4 = new qtablewidgetitem("" + companyList[x].Positionz)
                    item5 = new qtablewidgetitem("" + companyList[x].Changez)
                    item6 = new qtablewidgetitem("" + companyList[x].Ratioz)
                    
                    setitem(x-1, 0, item1) 
                    setitem(x-1, 1, item2) 
                    setitem(x-1, 2, item3) 
                    setitem(x-1, 3, item4) 
                    setitem(x-1, 4, item5) 
                    setitem(x-1, 5, item6) 
                next
                
                setItem(0,3, new qTableWidgetItem("Item03") )
                setItem(2,1, new qTableWidgetItem("Item21") )    
                

                ###----------------------------------------------------------------------
                ### SORT Columns by Header Click, Alternate Sort every time it is Clicked
                
                setSortingEnabled(true)     
     
            }
            
            ###--------------------------     
        show()
    }
 
###--------------------------------------

    ###------------------
    ### TABLE 2 Methods
    
    func pHeaderFn
 
        See "Func pHeaderFn called "  +nl

        # The method geteventparameters() defined by RingQt to return a list of event parameters
        # The sectionClicked event contains one parameter : int logicalIndex    

        x = self.oMyHeader.geteventparameters()[1] 
        header[x+1] += 1
        if header[x+1] % 2 = 0 sorttype = "descending" else sorttype = "ascending" ok 
        
        See "Header " + x +" clicked: " + sorttype + nl
        See "Column: " + x +" TopRow: "+ Table2.item(0,x).text() + nl + nl 
        
        ###-----------------------------
        ### Manual Sort
        #
        #   Table2.sortByColumn(x, False)   ### False ascending, True decending
        
        ###----------------------
        ### Sum of Changes column
        
        Table2.setitem(6, 4, new qtablewidgetitem("SumChg"))
        Sum = 0
            for y = 0 to 5
                Sum += Table2.item(y,4).text()
            next    
        Table2.setitem(7, 4, new qtablewidgetitem("" + Sum))

        ###-------------------------
        ### Average of Ratio column
        Table2.setitem(6, 5, new qtablewidgetitem("AvgRatio"))
        Sum = 0
            for y = 0 to 5
                Sum += Table2.item(y,5).text()
            next
        Avg = Sum / y
        Table2.setitem(7, 5, new qtablewidgetitem("" + Avg))        
        
        ###--------------------
        ###  Item cell color
        ###  tableWidget->item(8, 0)->setBackground(Qt::red);

        mycolor = new qcolor() { setrgb(0,125,255,255) }
        mybrush = new qbrush() { setstyle(1) setcolor(mycolor) }
        Table2.item(3,3).setbackground(mybrush)        
        Table2.item(6,4).setbackground(mybrush)

    return
            
###---------------------
 
Class Company 
      Symbolz   
      Namez 
      Groupz
      Positionz 
      Changez
      Ratioz
   
###---------------------


Output:



AA-Table-17c6-SortOneClassOneWindow.ring
Message has been deleted

Bert Mariani

unread,
Sep 28, 2016, 10:00:03 AM9/28/16
to The Ring Programming Language
Thank you very much Gal and Mahmoud
For helping with the code sample.

--------------------------

The biggest problem that I have today is:  How to write proper QT type statements that work with Ring.
Along with Global, Local and Object scope.

I look at the ring_qt.ring file,  But I am never sure of what format the parameters take.
I scan the internet for QT C type examples, but they do not always work.

Example:
     Class QTableWidgetItem

     Func setBackground P1
     return QTableWidgetItem_setBackground(pObject,GetObjectPointerFromRingObject(P1))

     Func background 
     pTempObj = new QBrush
     pTempObj.pObject = QTableWidgetItem_background(pObject)
     return pTempObj

It would be nice to have a one line example after each one to show their proper use.


Greetings
Bert Mariani


On Sunday, September 25, 2016 at 9:22:55 AM UTC-4, Bert Mariani wrote:

Mahmoud Fayed

unread,
Sep 28, 2016, 12:14:57 PM9/28/16
to The Ring Programming Language
Hello Bert

Very nice question.

Ring 1.1 documentation is better and comes with RingQt classes and method prototypes (from Qt documentation)

Check this link until we release Ring 1.1 very soon


Greetings,
Mahmoud

CalmoSoft

unread,
Sep 30, 2016, 6:21:18 AM9/30/16
to ring...@googlegroups.com
Hello Mahmoud,

How can I sort only a range of cells in the QTableWidget?
Thank you very much.

Mahmoud Fayed

unread,
Sep 30, 2016, 2:53:56 PM9/30/16
to The Ring Programming Language
Hello Gal

1 - Don't depend on the UI controls to do the logic
2 - Create a function to divide lists to three parts. the function take nStart and nEnd numbers
Then return 3 lists. The first list from 1 to nStart-1 
The second list from nStart to nEnd
The third list from nEnd+1 to the end of items
Let this function return the three lists in one list like  [ aList1 , aList2 , aList3 ]

3 - Sort aList2 by getting it from the second item

4 - Create a function to merge between lists, get [aList1 , aList2, aList3] and return one list

5 - Create a function to convert from UI control to the List and from the List to the UI control

6 - You can put the new functions in a list for example Class DivideMergeLists for future reuse.

Greetings,
Mahmoud

CalmoSoft

unread,
Oct 1, 2016, 1:00:06 AM10/1/16
to The Ring Programming Language
Hello Mahmoud,

Thank you very much for your help.
I try to do it.

Mahmoud Fayed

unread,
Oct 1, 2016, 2:18:10 AM10/1/16
to The Ring Programming Language
Hello Gal

You are welcome, Keep up the good work :D

Greetings,
Mahmoud

CalmoSoft

unread,
Oct 1, 2016, 6:49:54 AM10/1/16
to ring...@googlegroups.com
Hello Bert, Mahmoud,

Now the next code works properly.

Code:

load "guilib.ring"

new qapp 

    header = list(6)

    $objectname   = "osecondwindow"   ### $objectname use inside class secondwindow as($objectname+".pBox2X()")
    osecondwindow = new secondwindow  ### class called secondwindow
     
    exec()
}

class secondwindow

    win2 = new qwidget() 
    {           
        setgeometry(30,40,700,400)
        setwindowtitle("display qwidget - win2 Table2 xyz")
        
            Table2 = new qtablewidget(win2) 
            {
                setrowcount(8)
                setcolumncount(6)
                
                setgeometry(20,20,800,600)
                setselectionbehavior(qabstractitemview_selectrows)
                setselectionmode(qabstractitemview_selectitems)         

                setcolumnwidth(2,75)    

                mycols = new qstringlist() 
                    {    append("Able") 
                         append("Baker")
                         append("Charlie")
                         append("Dog") 
                         append("Easy")
                         append("Fox")
                    }
                    
                sethorizontalheaderlabels(mycols)                              
                horizontalheaderitem(4).settext("NbrChange")               
                sethorizontalheaderitem(5, new QTableWidgetItem("NbrRatio")) 
                
                setalternatingrowcolors(true)
                setStyleSheet("QHeaderView::section { background-color: aqua }")                  
                verticalheader().setstylesheet("color: red")  
                horizontalheader().setstylesheet("color: blue")

                omyheader = new QHeaderView(1,win2)
                omyheader.setsectionsclickable(true)
                omyheader.setsectionclickedevent($objectname+".pheaderfn()")
                sethorizontalheader(omyheader)
                  
                companyList = []
                
                aList = 
                [
                  [ "SPY",   "SP500",    "Index", 0, 109.24, 107.21 ],
                  [ "SDYL",  "SPDivs",   "ETF", 1, 152.33, 119.85 ],
                  [ "FB",    "Facebook", "Tech", 2, 143.34, 114.02 ],
                  [ "AMZN",  "Amazon",   "Cloud", 3, 154.80, 117.39 ],
                  [ "NVDA",  "Nvidia",   "Chips", 4, 287.49, 141.35 ],
                  [ "GOOGL", "Google",   "Search", 5, 126.70, 107.83 ]
                ]
                
                for x = 1 to 6
                    add(companyList,  new company { Symbolz=aList[x][1]  Namez=aList[x][2]  Groupz=aList[x][3]  Positionz=aList[x][4] Changez=aList[x][5]  Ratioz=aList[x][6]  } )
                next      

                for x = 1 to len(companyList)                                
                    item1 = new QTableWidgetItem(companyList[x].Symbolz)
                    item2 = new QTableWidgetItem(companyList[x].Namez)
                    item3 = new QTableWidgetItem(companyList[x].Groupz)
                    item4 = new QTableWidgetItem("" + companyList[x].Positionz)
                    item5 = new QTableWidgetItem("" + companyList[x].Changez)
                    item6 = new QTableWidgetItem("" + companyList[x].Ratioz)
                    
                    setitem(x-1, 0, item1) 
                    setitem(x-1, 1, item2) 
                    setitem(x-1, 2, item3) 
                    setitem(x-1, 3, item4) 
                    setitem(x-1, 4, item5) 
                    setitem(x-1, 5, item6) 
                 next

                 #setsortingenabled(true)      
            }
        show()
    }
 
###--------------------------------------
    
    func pheaderfn


        see "func pheaderfn called "  +nl

            for i = 0 to 5

                 for j = 0 to 5

                      aList[i+1][j+1] = Table2.item(i, j).text()

                 next

            next


        sum = 0
            for y = 0 to 5
                sum += Table2.item(y,4).text()
            next    
 
        sum = 0
            for y = 0 to 5
                sum += Table2.item(y,5).text()
            next
        avg = sum / y

        mycolor = new qcolor() { setrgb(0,125,255,255) }
        mybrush = new qbrush() { setstyle(1) setcolor(mycolor) }
        
              nc = self.omyheader.geteventparameters()[1] 
        header[nc+1] += 1
        if header[nc+1] % 2 = 0 sorttype = "descending" else sorttype = "ascending" ok
        if header[nc+1] % 2 = 0 bList = sort(aList, nc+1) bList = reverse(bList) else bList = sort(aList, nc+1) ok

        for n = 1 to len(bList)
            Table2.setitem(n-1, 0, new QTableWidgetItem(bList[n][1]))
            Table2.setitem(n-1, 1, new QTableWidgetItem(bList[n][2]))
            Table2.setitem(n-1, 2, new QTableWidgetItem(bList[n][3])) 
            Table2.setitem(n-1, 3, new QTableWidgetItem(bList[n][4]))
            Table2.setitem(n-1, 4, new QTableWidgetItem(bList[n][5]))
            Table2.setitem(n-1, 5, new QTableWidgetItem(bList[n][6]))
        next

        Table2.setitem(6, 4, new QTableWidgetItem("sumchg"))  
        Table2.setitem(7, 4, new QTableWidgetItem("" + sum)) 
        Table2.setitem(6, 5, new QTableWidgetItem("avgratio"))
        Table2.setitem(7, 5, new QTableWidgetItem("" + avg))

        Table2.item(6,4).setbackground(mybrush) 
        
        see "header " + nc +" clicked: " + sorttype + nl
        see "column: " + nc +" toprow: "+ Table2.item(0,nc).text() + nl + nl 

    return
            
class Company 
         Symbolz   
         Namez 
         Groupz
         Positionz 
         Changez
         Ratioz  


Output:

func pheaderfn called
header
3 clicked: ascending
column
: 3 toprow: 0

AA-Table-17c6-SortOneClassOneWindow.ring

Bert Mariani

unread,
Oct 1, 2016, 12:25:11 PM10/1/16
to The Ring Programming Language
Hello Gal and Mahmoud et ALL

Very nice coding Gal. Thank you.

Question: QWidgetTable
Is there a way to select a range of cells  using the mouse, like in Excel ?
Then call a function to perform some manipulations on those selected cells.





On Sunday, September 25, 2016 at 9:22:55 AM UTC-4, Bert Mariani wrote:

CalmoSoft

unread,
Oct 1, 2016, 1:05:33 PM10/1/16
to The Ring Programming Language
Hello Bert,

Thank you very much for your kind words.
I can not answer your question.

Mahmoud Fayed

unread,
Oct 1, 2016, 7:09:44 PM10/1/16
to The Ring Programming Language
Hello Bert

QTableWidget class contains methods that help in this task like

QList<QTableWidgetItem *>selectedItems()
QList<QTableWidgetSelectionRange>selectedRanges() const

The advantage of the previous method maybe related to better performance (if they cache lists contains the selected items).

But the previous methods are not added to RingQt.

So for now (until we add them), the next example will do what you want to do by scanning all of the QTableWidget items 

Sure, The next solution will be slow for large numbers of items.

Load "guilib.ring"

New qApp {

        win1 = new qMainWindow() {

                setGeometry(100,100,800,600)

                setwindowtitle("Using QTableWidget")

               Table1 = new qTableWidget(win1) {

                        setrowcount(10) setcolumncount(10)

                        setGeometry(10,10,400,400)

                       for x = 1 to 10

                                for y = 1 to 10

                                       item1 = new qtablewidgetitem("10")

                                       setitem(x-1,y-1,item1)

                               next

                       next

               }

        btn1 = new qPushButton(win1) {

                setText("Increase")    

                setGeometry(510,10,100,30)

               setClickEvent("pClick()")

       }

            show()

        }

        exec()

}


func pClick

        for nRow = 0 to Table1.rowcount() - 1

                for nCol = 0 to Table1.columncount() - 1

                       Table1.item(nRow,nCol)  {

                               if isSelected()

                                        setText( "" + ( 10 + text()) )

                                ok

                       }

               next

       next




Greetings,
Mahmoud






Check the next sample

CalmoSoft

unread,
Oct 8, 2016, 5:05:25 AM10/8/16
to ring...@googlegroups.com
Hello Mahmoud,

How can I use selectedItems() and selectedRanges() functions of QTableWidget?
Please send me an example.
Thank you very much.

Mahmoud Fayed

unread,
Oct 8, 2016, 5:55:55 AM10/8/16
to The Ring Programming Language
Hello Gal

I still need to add them, They will be added in Ring 1.2

Greetings,
Mahmoud

CalmoSoft

unread,
Oct 8, 2016, 6:25:56 AM10/8/16
to The Ring Programming Language
Hello Mahmoud,

Thank you very much for your reply.

Mahmoud Fayed

unread,
Oct 8, 2016, 6:58:27 AM10/8/16
to The Ring Programming Language
Hello Gal

You are welcome :D

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