[Qt-interest] QTableWidget change columncount on resize

Liviu Lalescu livubuntu at lalescu.ro
Sun Jun 27 14:16:08 CEST 2010


The copying of the whole table contents is time consuming and unnecessary, in 
my opinion.

I think you can use a custom model connected to a QTableView (probably 
subclass QAbstractItemModel to implement the custom model). The model knows 
the column count of the QTableView. You need to reimplement the function 
data() of QAbstractItemModel and maybe other classes. Function data() which 
you reimplement will return 
myItems.at(index.row()*tableView.nColumns()+index.column()).

Please correct me if I am wrong.


On Sunday, June 27, 2010 14:50:09 Teaburon wrote:
> Hello!
> 
> I have subclassed QTableWidget for my current project. The widget holds
> quadratic cells of the same size and each cell contains one character. The
> visible items are stored in a QList<T> myItems. Now I want the table to
> change its columnCount on a resize, so that the cellsize stays the same and
> more (or less) items are visible if the user resizes the table. I therefore
> implemented resizeEvent like this:
> 
> void MyTableWidget::resizeEvent(QResizeEvent *e) {
>     if (columnCount() > 0 ) {
> 
>         int colC = e->size().width() / columnWidth(0);
>         int rowC = myItems.size() / colC;
> 
>         setColumnCount(colC);
>         setRowCount(rowC);
>         row = col = 0;
> 
>         for(int i = 0; i < myItems.size(); ++i){
>             MyItem * c = new MyItem(myItems.at(i));
>             setItem(row,col,c);
>             col++;
>             if (col % colC == 0) {
>                 col = 0;
>                 row++;
>             }
>             myItems.replace(i, *c);
>         }
>     }
>     QWidget::resizeEvent(e);
> }
> 
> This works nearly the way I want, but it is slow, because of the copy
> action and since myItems is very large (>2k entries)... Is there any way
> to achieve this behavior without copying the items like I did?
> Any advice on how to make this faster would be great!
> 
> Greetings Teaburon.



More information about the Qt-interest-old mailing list