[Qt-interest] Questions about table view and item delegates

Andre Somers andre at familiesomers.nl
Fri Jan 22 15:40:12 CET 2010


Christian Gagneraud wrote:
> Hi all,
>
> I'm using a table view with spinbox delegate, I've based my code on 
> http://doc.trolltech.com/4.6/itemviews-spinboxdelegate.html.
>
> I'm using SDK 2009.01 on Fedora 12.
>
> I've re-used "as is" spinboxdelegate.{cpp,h}, and my main() looks like 
> this:
>
> int main(int argc, char *argv[])
> {
>      QApplication a(argc, argv);
>      MainWindow w;
>
>      /* Set-up the schedule table using spinbox delegate */
>      QTableWidget* widget = w.findChild<QTableWidget*>("tableSchedule");
>      int nrows = widget->rowCount();
>      int ncols = widget->columnCount();
>      QStandardItemModel model(nrows, ncols);
>      QTableView* view = widget;
>      view->setModel(&model);
>      SpinBoxDelegate delegate;
>      view->setItemDelegate(&delegate);
>      for (int row = 0; row < nrows; ++row) {
>               for (int column = 0; column < ncols; ++column) {
>                   QModelIndex index = model.index(row, column, 
> QModelIndex());
>                   model.setData(index, QVariant(0));
>               }
>           }
>
>      w.show();
>      return a.exec();
> }
>
> The only noticeable difference (for me) between the example and my 
> (simple) app is that the table view is embedded within a form designed 
> with QtDesigner/QtCreator. As a consequence I use Mainwindow.show() 
> instead of TableView.show() and I got the pointer to my QTableView via 
> findChild().
>
> Problem 1:
> When I insert data into the model, they are not shown up in the view. 
> For example, I'm expected to get my table filled with zeros, but 
> instead I got an empty table. If i execute the unmodified example, it 
> works as expected!
>   
I'd say: compare your code with the example, and see what you do 
different. Sounds like standard debugging to me...

(...)
> Problem 3:
> When the user select several cells, or a whole column or row, I would 
> like that changing the value of one of these cells change the value of 
> all the selected cells. Again I didn't find any documentation on how 
> to do this. Any help appreciated on that matter too.
>   
You'll have to implement that yourself. It's not that hard. You could 
connect to your model's dataChanged() signal, see if your item is inside 
a selection in your view, and if so, call setData on all the other cells 
in the selection with the new value. I never tried it, but it should be 
only a few lines of code.
See the documentation on QAbstractModelItem and QItemSelectionModel.

André





More information about the Qt-interest-old mailing list