[Qt-interest] model->insertColumns() is inserting row instead

Tony Rietwyk tony.rietwyk at rightsoft.com.au
Fri Dec 19 01:02:42 CET 2008


Neel wrote: 

> I am just playing with Qt and for that I made a 
> MarkSheetModel that inherits 
> from QAbstractTableModel. and I am using a Standerd QTableView for 
> representation purpouse.
>  This is My MarkSheetTableModel Header.
> 
> class MarkSheetTableModel : public QAbstractTableModel{
>         public:
>                 MarkSheetTableModel(QObject* parent=0);
>                 ~MarkSheetTableModel();
>         private:
>                 QList<QString> subjects;//Headers Come from Here
>                 QHash<QString, QList<double>* > 
> students;//each student and 
> their corresponding marks make a row
>         public:
>                 QList<double>& addStudent(const QString& stdName);
>                 QList<double>& student(const QString& stdName);
>         public:
>                 int rowCount(const QModelIndex& parent) const;
>                 int columnCount(const QModelIndex& parent) const;
>                 QVariant data(const QModelIndex& index, int 
> role) const;
>                 QVariant headerData(int section, 
> Qt::Orientation orientation, 
> int role) const;
> };
> 
> everythings goes fine.
>  
>  Now I want two Extra Column Total and Avarage, as these two 
> does not hold a 
> concreate data I don't want these two to come from the Model 
> (These two should 
> be 
> representation oriented columns).so I searched a lot to 
> insert a Column on 
> View (at 
> least a psudo Column) But I didn't get one.
>  What I got is insertColumn() method on Model (although Idont 
> think it 
> shouldbe added to column but I've no other way I think) .
>  
>  Here goes my MainWindow's Constructor's code.
> 
> MainWindow::MainWindow(QWidget *parent, Qt::WFlags flags): 
> QMainWindow(parent, 
> flags), vHeadersBrush(QColor(0, 0, 255, 127)), 
> hHeadersBrush(QColor(0, 255, 
> 0, 127)), mAutoRecalculateStatus(true){
>         ui.setupUi(this);
>         markSheetTableModel = new MarkSheetTableModel;
>         markSheetTableModel->addStudent("Ram") << 50 << 85 << 
> 86.50 << 75.25;
>         markSheetTableModel->addStudent("Shyam") << 55 << 80 
> << 86 << 85;
>         markSheetTableModel->insertColumns(0, 1);//Inserting 
> one Column I've 
> triedinsertColumns() which gave the same result too
>         markSheetTableModel->addStudent("Total");//Setting 
> Header for that 
> Column.
>         ui.markSheetTableView->setModel(markSheetTableModel);
> }
> 
> But this above code inserts a Row called Total instead
> I am kinda stuck at this point somebody please Help me out.


Hi Neel, 

I'm not sure about the extra row - it might be a bug.  Which version are you
using? 

For the extra columns, there are alternatives: 

- Include them in the model, and just calculate the values in the data
function. Views can then hide those columns if they are not relevant. 

- Use a proxy model and add them there. 

Hope that helps, 

Tony Rietwyk.





More information about the Qt-interest-old mailing list