Manuel wrote:
> Hi Gunnar,
> 
> thanks for your reply.
> I got it working with beginInsertRows() and endInsertRows() but i think, 
> that i don't implemented it correctly :-)
> Is there any example available?
> 
> Here is what i have done. Please correct me, if my way of thinking is 
> wrong:
> 
> I have an Array, ArrayList or whatever i want to show in the table. For 
> example a list of File-Objects.
> I want to list these files in the table (filename, file size etc.). So 
> my TableModel has to know the ArrayList.
> I transfer this ArrayList to the TableModel via a parameter in the 
> contructor.
> Is this the best way to give the TableModel access to the ArrayList?
> 
> Now my program manipulates the ArrayList in the background. For example 
> i add or delete files.
> Should i now call the beginInsertRows() and endInsertRows()-functions 
> manually to get the GUI to be updated?
> Or is there a possibility, that the TableModel recognizes itself, that 
> the ArrayList has changed.
> Actually i fire an event when the ArrayList changes and call an 
> update-Function which call the beginInsertRows() and endInsertRows() 
> functions.

Calling begin/end after the change is not enough. You need to do

beginInsertRows()
change array
endInsertRows()

with the proper arguments for the new rows to be committed correctly.

Below you'll find a modified version of your code where I have a 
doStuff() funtion that is called after 3 seconds which adds another row 
to the program.

best regards,
Gunnar

package mcut.gui;

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

public class TableModelMyFile extends QAbstractTableModel {
     ArrayList<String> myFiles;

     public TableModelMyFile(ArrayList<String> myFiles) {
         this.myFiles = myFiles;
     }

     public int columnCount(QModelIndex i) { return 5; }
     public int rowCount(QModelIndex i) { return myFiles.size(); }

     public Object data(final QModelIndex index, int role) {
         switch (role) {
         case Qt.ItemDataRole.DisplayRole:
             return myFiles.get(index.row()) + ":" + index.column();
         default:
             break;
         }
         return null;
     }


     public void doStuff() {
         beginInsertRows(null, 3, 3);
         myFiles.add("Four");
         endInsertRows();
     }


     public static void main(String args[]) {
         QApplication.initialize(args);

         QTableView view = new QTableView();

         ArrayList<String> list = new ArrayList<String>();
         list.add("One");
         list.add("Two");
         list.add("Three");

         final TableModelMyFile model = new TableModelMyFile(list);
         view.setModel(model);

         view.show();

         QApplication.invokeLater(3000, new Runnable() {
                 public void run() {
                     model.doStuff();
                 }
             });

         QApplication.exec();
     }
}

_______________________________________________
Qt-jambi-interest mailing list
[email protected]
http://lists.trolltech.com/mailman/listinfo/qt-jambi-interest

Reply via email to