[Qt-jambi-interest] Model/View problem: How to keep the model as simple as possible

Gunnar Sletta gunnar at trolltech.com
Tue Aug 12 10:33:36 CEST 2008


Ying Ma wrote:
> 
> Research & Development
> ------------------------------------------------------------------------
> 
> 
> Dear sir,
> 
>     I am trying to implement a model/view structure for a system which 
> contains a large
> number of data. when I tried to add all the data to the model, the model 
> became too
> huge to perform well. The views attached to this model became slower and 
> slower when
> more and more data was added to the model.
> 
>     So how can I solve this problem? How to make the model as simple as 
> possible to keep
> it works well with the views?
> 
>     Best regards,

Hi,

One way to speed up, is to set the table cells to be fixed size. This is 
done by setting the QHeaderView's resize mode to Fixed and setting a 
default size for rows and columns. Below is an example with 100 columns 
and 1M rows. It runs pretty smooth on my machine at least.

best regards,
Gunnar

import com.trolltech.qt.gui.*;
import com.trolltech.qt.core.*;

public class ItemModel extends QAbstractTableModel {

     public ItemModel() {
     }

     public int rowCount(QModelIndex parent) { return 1000000; }
     public int columnCount(QModelIndex parent) { return 100; }

     public Object data(QModelIndex index, int role) {
         if (role == Qt.ItemDataRole.DisplayRole) {
             return "col=" + (index.column() + 1)  + ", row=" + 
(index.row() + 1);
	}
         return null;
     }


     private int rows = 4;
     private QTimer timer;

     public static void main(String args[]) {
         QApplication.initialize(args);
         QTableView view = new QTableView();

         view.verticalHeader().setResizeMode(QHeaderView.ResizeMode.Fixed);
         view.verticalHeader().setDefaultSectionSize(20);

 
view.horizontalHeader().setResizeMode(QHeaderView.ResizeMode.Fixed);
         view.horizontalHeader().setDefaultSectionSize(100);

         ItemModel model = new ItemModel();
         view.setModel(model);

         view.show();

         QApplication.exec();
     }
}



More information about the Qt-jambi-interest mailing list