PDA

View Full Version : TextAlignment / no RowCount in QStandardItemModel



SenSej
16th November 2008, 19:54
I set up an QStandardItemModel an show it in an QTableView.
When inserting several rows, first colum shows the row number.
How to disable them?

Also what the second colum (first row with my content) shows the text right alligned.
And i want the colum width set to minimum, regarding content text width.

Haven´t found any hints - can you help me?

wysota
16th November 2008, 21:25
The first "column" is a header - you can hide it. Alignment is set using Qt::TextAlignmentRole on each item. As for "width set to minimum" - define "minimum" :) You can change the resize policy for the horizontal header if you want, maybe it will be sufficient in your case.

SenSej
17th November 2008, 13:44
The first "column" is a header - you can hide it.
Sorry, but did you mean "can" or "can´t"? Because if i can, i wonder again how.



Qt::TextAlignmentRole on each item.
I need to display ints and text. This data is gathered from other objects.
I´ve tried to avoid setting up explicit an QStandardItems for setting it as model data.

How i did it:
int:
model->setData(model->index(row, 0, QModelIndex()), i+1);
text:
model->setData(model->index(row, 0, QModelIndex()), QString::fromStdString(object->getTextAt(j) );

But where to set the Qt::TextAllignmentRole when doing it this way?

BTW: My exxample is very simple. I didn´t set up special classes for the model or it´s items. Didn´t use subclasing in any way. It´s just one method in my mainwindowclass, where i set up a QStandartmodel, gathering some data to show the model in QTableView.
I need to keep it simple - also if it´s not confentional or seems to be stupid - there is no time left.

Next year going to learn QT from the beginning - i swear!

wysota
17th November 2008, 14:32
Sorry, but did you mean "can" or "can´t"? Because if i can, i wonder again how.
"Can" :) By calling.... hide() on it :) Have you seen QTableView docs? They are really nice, you should at least take a brief look at them...


I need to display ints and text. This data is gathered from other objects.
I´ve tried to avoid setting up explicit an QStandardItems for setting it as model data.
So return appropriate alignment from data() based on the contents of the item. data() is a virtual method, you can reimplement it.


But where to set the Qt::TextAllignmentRole when doing it this way?
I take it that you... forgot... to read the docs. See what setData() accepts.


BTW: My exxample is very simple. I didn´t set up special classes for the model or it´s items. Didn´t use subclasing in any way. It´s just one method in my mainwindowclass, where i set up a QStandartmodel, gathering some data to show the model in QTableView.
I need to keep it simple - also if it´s not confentional or seems to be stupid - there is no time left.
You don't need to subclass anything.


Next year going to learn QT from the beginning - i swear!
Ok, but this year start by reading documentation of methods and classes you are using.

SenSej
17th November 2008, 15:45
First of all - Thanks a lot for you help.
Hiding wasn´t a matter of the model, but the view. I didn´t set up any view, so i didn´t recognize this. I´ve hided the vertical Header of the QTableView and now all´s fine.

By using

int intVal = 89;
QStandardItem* itemInt = new QStandardItem();
itemInt->setData(intVal, Qt::AlignRight);
model->setData(model->index(row, 0, QModelIndex()), itemInt);

"true" will be displayed and not 89.

Just wanted to thank you and start now to read. But any hints are stil wellcome.

wysota
17th November 2008, 18:16
By using

int intVal = 89;
QStandardItem* itemInt = new QStandardItem();
itemInt->setData(intVal, Qt::AlignRight);
model->setData(model->index(row, 0, QModelIndex()), itemInt);

"true" will be displayed and not 89.

setData takes a value and a role. A role is something that tells the model, what you want to do with the value you give it. If you want the value to be displayed, you pass Qt::DisplayRole, if you want it to determine what font is used, you pass it Qt::FontRole. If you want to tell what the text alignment should be, you pass it a Qt::TextAlignmentRole. So the proper sequence of calls in your case is:

setData(89, Qt::DisplayRole); // display 89
setData(Qt::AlignRight, Qt::TextAlignmentRole); // aligned to the right

SenSej
18th November 2008, 14:15
setData(89, Qt::DisplayRole); // display 89
setData(Qt::AlignRight, Qt::TextAlignmentRole); // aligned to the right
Thanks - Now i got you right!
But in my doku, the signature of "setData" come with QModelIndex as first param.
When using also the index it works fine.

Pherhaps you´ve got one more hint for me:
My QStandardItemModel got three colums.
The first and second are only to display some items.
I want to use the third colum as input fields.
So i need to disable any editor-action for the first and second, the third schould be marked and differ in graphic from the rest, with enabled editor functionality.

In the doku i´ve found the role-enum, but not which values are suitable for each role.

Is it possible, to set another widget as an item to one field in the model (like an SpinBox or LineEdit)?

wysota
18th November 2008, 17:17
Thanks - Now i got you right!
But in my doku, the signature of "setData" come with QModelIndex as first param.
When using also the index it works fine.
If you call the model directly, then index comes as the first parameter. If you call setData on QStandardItem, it takes only two parameters.


Pherhaps you´ve got one more hint for me:
My QStandardItemModel got three colums.
The first and second are only to display some items.
I want to use the third colum as input fields.
So i need to disable any editor-action for the first and second, the third schould be marked and differ in graphic from the rest, with enabled editor functionality.
Take a look at QAbstractItemModel::flags() and QStandardItem::flags().


Is it possible, to set another widget as an item to one field in the model (like an SpinBox or LineEdit)?

Yes, but you don't want to do that.