[Qt-interest] Questions about table view and item delegates
Christian Gagneraud
cgagneraud at techworks.ie
Fri Jan 22 18:03:40 CET 2010
On 01/22/2010 12:30 PM, 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!
This one is fixed. Actually I was using a QTableWidget and the example
uses a QTableView, the QTableWidget already contains a model, so I was
overwriting it, now the code works:
QTableWidget* widget = w.findChild<QTableWidget*>("tableSchedule");
int nrows = widget->rowCount();
int ncols = widget->columnCount();
QAbstractItemModel* model = widget->model();
QTableView* view = widget;
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));
}
}
[...]
> 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.
I managed to solve problem #1 while working on this one, by tracing
the dataChanged() signal, i realized that something was really wrong...
Thanks,
Chris
>
>
> Regards,
> Chris
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest
More information about the Qt-interest-old
mailing list