HBQT - Sorting QTableView() columns by clicking on header buttons

4,414 views
Skip to first unread message

Zoran Sibinovic

unread,
Aug 28, 2012, 7:12:39 AM8/28/12
to harbou...@googlegroups.com
Hi,

I try to implement in ...\hbqt\tests\testbrow.prg the sorting of columns of an opened .dbf database.

I found that adding   tb1:setSortingEnabled(.t.) the columns header display an arrow that change direction by clicking on it,
but, no data sorting happened.

I tried also to add tb1:sortByColumn( 0,0 ) to activate a sorting without clicking on the header, but without success either.

Am I doing something wrong, miss something or the :setSortingEnabled(.t.) is intended for ex. QTreeView or else.

Googling i found lot of conflicting opinions and cases from where QTableview doesn't do the sort of columns as a QT bug till the sorting is applied only to the data of the column that is sorted, not to the entire rows till that numbers are not sorted as they will have to be.

Can someone give some help about the sorting columns implementation in HBQT and not only in QTableView if it differs, or where can be found some explanation.

I looked in hbide the way of sorting the .dbf using the already made database index from where are retrieved the indexes.
That's ok, but that's not the idea.

I have some thoughts, if this can't be done using some qt methods, triggers, connections or else, as an idea, to make, in my case, a temporary .cdx index file that contains all fields orders and than trigger them when the header is pressed.
Large database - more time i know.

I use also a total commander plugin - cdbfview (it's free) and extremely fast. Among other things I can view the database and sort the columns easily, and what's very interesting i have found no temporary files during the sorting. Bad thing can't edit the fields but in lot of cases useful to get some informations quickly. Haven' found it's source code yet. 

Thanks in advance for every opinion.

Zoran 

 






Giovanni Di Maria

unread,
Aug 28, 2012, 8:46:09 AM8/28/12
to harbou...@googlegroups.com

francesco perillo

unread,
Aug 28, 2012, 9:03:31 AM8/28/12
to harbou...@googlegroups.com

Qtableview uses a model. It is model job to provide sorted data. So as far as i know the only way is to intercept the click on the header, set a new index on dbf (or create it) and then force a refresh.

Zoran Sibinovic

unread,
Aug 28, 2012, 9:19:51 AM8/28/12
to harbou...@googlegroups.com
Hi Giovanni, fperillo,

already read it jus it applay to QTableWidget not to QTableView.

Hi fperillo

>>Qtableview uses a model. It is model job to provide sorted data. So as far as i know the only way is to intercept the click on the header, set a new index on dbf (or create it) and then force a refresh.
 
I think also it have to be something like that, but i haven't found (documentation QT or else) any reference about the necessity to use an generated index file to do that, by tutorial everything seems to indicate that the sorting index is virtual, in witch way, i don't know for now.  

Thanks
Zoran




francesco perillo

unread,
Aug 28, 2012, 9:25:59 AM8/28/12
to harbou...@googlegroups.com

Please specify: are you using qtableview or widget?

Zoran Sibinovic

unread,
Aug 28, 2012, 12:32:12 PM8/28/12
to harbou...@googlegroups.com
Hi,

as I already said in the topic first post I use QTableView. All the code is from ...\hbqt\tests\testbrow.prg

Zoran 

Pritpal Bedi

unread,
Aug 28, 2012, 3:08:05 PM8/28/12
to harbou...@googlegroups.com
Hi


as I already said in the topic first post I use QTableView. All the code is from ...\hbqt\tests\testbrow.prg

You cannot sort a .dbf.

To achieve this functionality, then 
load .dbf records in an array and then 
use HbQt classes only. Do not use code 
from testbrow.prg which is based on 
HBQAbstractItemModel().

That way only you can get sorting functionality.

Note: .dbf implementation is based on 
recno() which never gets changed with 
sorting mechanism.

Pritpal Bedi

Zoran Sibinovic

unread,
Aug 29, 2012, 1:30:11 AM8/29/12
to harbou...@googlegroups.com
Hi Pritpal,

thanks for an clear answer.

Can you suggest which example in ...\hbqt\tests\..., if any, can be used or what class/method is more appropriate to use, maybe QTableWidget?.

Thanks again
Zoran 



Pritpal Bedi

unread,
Aug 29, 2012, 12:10:02 PM8/29/12
to harbou...@googlegroups.com
Hi


thanks for an clear answer.

Can you suggest which example in ...\hbqt\tests\..., if any, can be used or what class/method is more appropriate to use, maybe QTableWidget?.

There is no test written to this effect.
Please study Qt's QTableView() documentation.
It will give you insight how to implement.

But, pardon my ignorance, why would you like 
to have a sorted table in the view? Then how you 
would relate a record in the view to table's recono().
I am curious why a sorting is needed, if any, 
for a .dbf. It is logical to browse it indexed.

Pritpal Bedi 

Zoran Sibinovic

unread,
Aug 29, 2012, 1:02:42 PM8/29/12
to harbou...@googlegroups.com
Hi Pritpal,

the reason is to make an dbf utility, simple for now, just to open, look in sort by fileds and change some data in fields if needed.

I found also, that even if you open a database with an index the view is in natural order, so...

I found one solution that works, but not finished for posting it here.

The idea was to make an index with the first fieldname, then fill an array with the record numbers but skipping through the ordered database.
Then in the my_select i added -> if the current row() is 0 then delete the index tag, empty the array, make the new index based on the newly chosen fieldname and then refill the array with new values. Also added and changed some code in my_browse and my_save ex. dbgoto(y+1) with dbgoto(aIndex[y+1]).

The things works and by pressing the headers buttons the database is sorted in the view, I have to say very fast. the catch is for now how to make an alternate ascending/descending view. When you click twice or more times on the same header button the connection to my_select is evaluated only the first time, because something is changed, if is not, the second click, my_select don't evaluate.

The problem that i faced now is how to connect the header buttons to some function, not to select them as my_select, just to make some evaluation on every click.

Thanks
Zoran
 





 

Zoran Sibinovic

unread,
Aug 31, 2012, 3:24:41 PM8/31/12
to harbou...@googlegroups.com
Hi to all,

I almost finished an example of opening, sorting the fields in QTableView, by clicking on the headers in ascending/descending order and the reflection of the view if changed the data in the current sorted column.

The working explanation is at the top of the .prg
Put the testbrow1.prg in ..\contrib\hbqt\tests\ 
end build it with hbmk2 testbrow1

If there was some problems try   hbmk2 testbrow1 -w0 

I think that some code can be optimized. But thats all for now.

Expecting opinions

Thanks
Zoran





 
testbrow1.prg

Zoran Sibinovic

unread,
Sep 3, 2012, 2:49:44 PM9/3/12
to harbou...@googlegroups.com
Hi to all,

any opinion about the above sample?

Zoran

apolinar

unread,
Sep 7, 2012, 1:28:07 PM9/7/12
to Harbour Users
Zoran
I've tried your sample on ubuntu 10.04 and works.
Thank you

regards
Apolinar


apolinar

unread,
Sep 8, 2012, 4:12:02 AM9/8/12
to Harbour Users
Hi Zoran
You could insert

tb1:resizeColumnsToContents ()

before
oWnd:Show()
QApplication():exec()

regards
Apolinar

Zoran Sibinovic

unread,
Sep 8, 2012, 6:21:57 AM9/8/12
to harbou...@googlegroups.com
Hi apolinar

thanks for the test and for your proposal and notes.

This is, for now only the first step in this demo to create something most powerful.
In intend also expand the possibilities by adding more features, ex. open more bases in different tabs, and, if have success, makee also relations between them.
but this will be the future.

In the meanwhile, because i don't know all the hbxbp classes, their methods, parameters and what they can do, if anyone knows some easiest way to achieve some better features, is very welcome to post.   

Thanks 
Zoran




 
Reply all
Reply to author
Forward
0 new messages