Danny Sporea wrote:
In the table, the 2 rows appear as "10" and "10.32".
Is there any way to format "10" to appear as "10.00" ?
I don't want to set it up as a String, since using
Double creates an edit spin box in that cell that
allows only numeric values, as opposed to the String
that is just a free text entry box...

Hi, Danny.

The simplest way I think would be to return a String for the display role of the model. As long as the edit mode still returns a double, the correct editor will be used.

I've attached a short example of how this can be achieved.

-- Eskil


import com.trolltech.qt.core.QModelIndex;
import com.trolltech.qt.core.Qt;
import com.trolltech.qt.gui.QApplication;
import com.trolltech.qt.gui.QStandardItemModel;
import com.trolltech.qt.gui.QTableView;

public class CustomDisplayRole extends QTableView {
	
	public CustomDisplayRole() {
		QStandardItemModel sim = new QStandardItemModel() {
			@Override
			public Object data(QModelIndex index, int role) {
				Object data = super.data(index, role);
				if (role == Qt.ItemDataRole.DisplayRole && data instanceof Double)
					data = String.format("%1$.2f", data);			
				
				return data;
			}
		};
		
		sim.setColumnCount(5);
		sim.setRowCount(5);
		
		QModelIndex index1 = sim.index(0, 0);
		QModelIndex index2 = sim.index(1, 0);		
		
		sim.setData( index1, new Double( 10.0 ) );
		sim.setData( index2, new Double( 10.32 ) );

		setModel(sim);
	}

	public static void main(String args[]) {
		QApplication.initialize(args);
		
		CustomDisplayRole sim = new CustomDisplayRole();
		sim.show();
		
		QApplication.exec();
	}
	
}
_______________________________________________
Qt-jambi-interest mailing list
Qt-jambi-interest@trolltech.com
http://lists.trolltech.com/mailman/listinfo/qt-jambi-interest

Reply via email to