tags:

views:

46

answers:

2

I have a model containing in one column floats (ex: 22.7). Now, I want that in QTableView, it will be visualized together with the unit (MB): 22.7 MB. The reason I am doing so is because I want that sorting is based on floats, but the visualization is as I said with units.

I created a Model, a Filter and a View. But it does not work. Here is a piece of my code:

QStandardItemModel* model = new QStandardItemModel(this);
QSortFilterProxyModel *filterModel = new QSortFilterProxyModel(0);
filterModel->setSourceModel(model);

QStandardItem* vSItem6 = new QStandardItem();
vSItem6->setData(22.7, Qt::DisplayRole); 
model->setItem(1, 7, vSItem6);
QModelIndex index = model->index(1, 7, QModelIndex());
QString itext = model->data(index, Qt::DisplayRole).toString(); 
filterModel->setData(index, itext + " MB", Qt::DisplayRole);


mUi.tableView->setModel(filterModel);
mUi.tableView->setSortingEnabled(true);
mUi.tableView->show();

Everything seems to be fine, but in QTableView, only the float number is visualized (without the unit MB). Can anybody please help me? Thanks

A: 

I know your question was about QTableView, but it might be easier if it is the only thing you want to display (I don't know your objectives) to use a QTableWidget with a QDoubleSpinBox since it has a method that allows you to set a suffix to the value that is displayed.

In your case, it would be:

QDoubleSpinBox* spin = new QDoubleSpinBox();
spin->setSuffix("MB");

Note that the range is set by default from 0.0 to 99.99 so if you want to set other values you should change the range before.

Hope this helps.

Live
Thanks for the reply, but this is not what I am searching for.
Tom
I am using QTableView instead of QTableWidget because of the Model View Controller.
Tom
It would be possible to add another column besides with the unit, but it is clearly not the best solution.
Live
You could look into setData and specify a DisplayRole (with the MB appended manually after) and a custom role you create to keep your value in a double format.
Live
Yes, this is true, but it looks stupid. Besides, I have many columns with units... I am surprised that there is nothing in the qt documentation that could help. I mean, everybody is writing how beatyfull model view controller approach is, that you have one model and many views, but nobody gives a useful example of how to do it...
Tom
+1  A: 

Have a look at setItemDelegate or setItemDelegateForColumn. You can derive your delegate class from QStyledItemDelegate, and in the overridden paint method you can paint your numbers including the units.

hmuelner
Thanks, I will try. But I still don't understand why to use model view controller widgets if even simple things don't work. I heard somewhere that proxy model should help, but there is no example around...
Tom
@Tom: The delegate classes are helpers to fix how individual items are drawn. Since you want to change how the item is drawn, the delegate class makes a lot of sense.
Caleb Huitt - cjhuitt