PDA

View Full Version : inserting QCheckBox into the Header



ru_core
12th April 2008, 19:24
Hello everybody
I have the task to insert the QCheckBox item into the vertical header.
I looked through the demos (itemview) where picture (the star) is inserted into the vertical header by reimplementing
method QVariant QAbstractItemModel::headerData ( int section, Qt::Orientation orientation, int role = Qt:: DisplayRole ) const [virtual]


QVariant Model::headerData(int section, Qt::Orientation orientation, int role) const
{
static QIcon services(QPixmap(":/images/services.png"));
if (role == Qt::DisplayRole)
return QString::number(section);
if (role == Qt::DecorationRole)
return qVariantFromValue(services);
return QStandardItemModel::headerData(section, orientation, role);
}
Then I try to reload this method for my own model (CustomModel)


QVariant CustomModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (role == Qt::DisplayRole)
return QString::number(section);
QObject *object = new QCheckBox;
if (role == Qt::UserRole)
return qVariantFromValue(object);
return QStandardItemModel::headerData(section, orientation, role);
}

but there is no effect.
Help me please.

wysota
12th April 2008, 19:30
There will be no effect. QHeaderView is not meant to hold widgets. You need to subclass the header and put it (and handle it) there by yourself. You can use event filters to get away from subclassing but you still have to do things manually.

ru_core
12th April 2008, 19:36
thank you,
can I do it using the
void QTableView::setVerticalHeader ( QHeaderView * header )method or which way?

wysota
12th April 2008, 19:59
If you subclass the header class then yes, you'll have to use setVerticalHeader() to set the subclassed header.

ru_core
12th April 2008, 20:20
There will be no effect. QHeaderView is not meant to hold widgets. You need to subclass the header and put it (and handle it) there by yourself. You can use event filters to get away from subclassing but you still have to do things manually.

so, if I got correct: I need to create widget with the QCheckBox in class customClass (subclassed from QHeaderView) and simply put it by setVerticalHeader() into the view? Correct?

I am new to Qt :)

wysota
12th April 2008, 21:16
Yes, that's correct. And you need to make sure it changes position when sections of the header are resized, etc. It's quite a bit of work, so before you start doing that think if you really need it. Maybe you could just use checkboxes in items themselves, that's much easier to achieve.

ru_core
12th April 2008, 21:31
Yes, that's correct. And you need to make sure it changes position when sections of the header are resized, etc. It's quite a bit of work, so before you start doing that think if you really need it. Maybe you could just use checkboxes in items themselves, that's much easier to achieve.

thank you again for the reply.
at this stage I got confused. So you propose subclass QStandardItem class and just create there checkbox. Then I have and put new object to model by
void QStandardItemModel::setVerticalHeaderItem ( int row, QStandardItem * item ) ?

wysota
12th April 2008, 21:43
I didn't say anything about QStandardItem. If you try to explain what do you need the checkbox for, maybe we'll find a solution that doesn't require writing as much code as subclassing the header does.

ru_core
12th April 2008, 21:52
I didn't say anything about QStandardItem. If you try to explain what do you need the checkbox for, maybe we'll find a solution that doesn't require writing as much code as subclassing the header does.

There is a QTableView object with model. I need to create the functionality to delele some row(s) from model.
So I decided to use the checboxes to indicate which rows have to be deleted. Then by pushing button user can delete the checked rows.

Could it be done other way?

wysota
12th April 2008, 22:00
Why not simply select the rows that are to be deleted using regular selection capabilities?

ru_core
12th April 2008, 22:02
selectig - using "shift" key and then delete'em some way?

wysota
12th April 2008, 22:04
Yes, shift and control.

ru_core
12th April 2008, 22:14
this program is not for every day usage, just like a calculator for students - to calculate some values. Whould it be good idea to propose standard "select-delete" capabilities instead of checkboxes?

wysota
12th April 2008, 22:17
I don't understand what you are asking about... A regular way to select items is to use selection... So you can safely use selection for selecting :)

ru_core
12th April 2008, 22:22
ok, it seems I subtilize to much :) I will follow your sugestion to use standard selection for selecting :)
thank you very much for the help.

vycke
16th April 2008, 18:04
If you're using it to delete rows, why not just make a column with the QTableWidgetItem having the flag Qt::ItemIsUserCheckable?

Or if you want to delete a row at a time, connect the table's (list's) clicked() signal to a delete function? Or even have a column with a "delete" label/button/icon & only have the delete function work if the column is correct?

Vycke

wysota
16th April 2008, 19:26
If you're using it to delete rows, why not just make a column with the QTableWidgetItem having the flag Qt::ItemIsUserCheckable?

Because that's an awful hack that comes from the world wide web where html doesn't allow you to perform real selections.