PDA

View Full Version : QTableView - model / view pattern - layout, edit



vlastagf
1st August 2009, 16:40
Hi folks!

I've started using Qt couple of days ago and have some issues I'm not able to solve anyhow and I would really appreciate someone's help.

I'm using mode / view pattern with QTableView, showing and editing my data works well, but:

1. How do I add (or delete) a row in the QTableView visual interface? I did it with some buttons and signals to my model but is there any other possibility?

2. My QTableView is expanding dynamically and I want the row to use all possible space but not more so I'll have to scroll left or right. I tried returning different values from data(..) and headerData(..) functions to Qt::SizeHintRole arguments but with no effect.

3. I'm quite familiar with model / view pattern but how can help me a delegate?

Thanks for any response or tip.

georgep
1st August 2009, 17:39
Have you seen this http://wiki.qtcentre.org/index.php?title=QAbstractItemModel ?

vlastagf
1st August 2009, 18:27
Thank for your quick response.

I went through this text and the result is this.

ad 1: I implemented insertRows(..) function but how to make the QTableView to call it while running?

ad 2: I fixed returning Qt::SizeHintRole however it's never called in function data(..) and returning proper values in headerData(..) doesn't make sense because it resize only the header columns not the geometry of data columns at all.

vlastagf
1st August 2009, 21:28
So I probably partly solved #1 by overriding QTableView to my own class and reimplementing some event handlers.

But I still need to know how to tell rows / columns to use all available space but not more -(

vlastagf
1st August 2009, 22:42
So I solved almost everything by overriding the QTableView class.

To add or delete a row I overrode the keyPressEvent(..) and implemented insertRows(..) in the model.

To dynamically resize columns by percents I did following.

in the model->data(..)
{
...
case Qt::SizeHintRole:
switch(index.column())
{
case 0:
return QVariant(40);
break;

case 1:
return QVariant(40);
break;

case 2:
//in the last column is used the rest of available space, 20+- in this case
return QVariant(0);
}
...
}

the overrode resizeEvent(..)
{
int count = model()->columnCount();
int total = 0;

for (int i = 0; i < count - 1; i++)
{
const QModelIndex index = model()->index(0, i);
QVariant hint = model()->data(index, Qt::SizeHintRole);
int size = (width() / 100) * hint.toInt();
horizontalHeader()->resizeSection(i, size);
total = total + size;
}
//used magical constant 5 for the last section
horizontalHeader()->resizeSection(count - 1, width() - total - 5);
}

I used Qt::SizeHintRole cause it's never used by QTableView itself.