[Solved]Get value of checkbox in a qtableview
-
bq. However, it seems that you want to simply try to modify the rendering of checked items to give them a background color. That can be done more efficiently using a QItemDelegate.
I want to check the state of the checkbox and it is in the 5 th column.
if the state is true/checked I want to change color of that row.And i also want to se the total rows in the model.
Is it possible.I'd like some simple exampels if there are some around.
-
For the background, use a [[Doc:QStyledItemDelegate]] subclass, as Andre suggested. Override the paint() method, check the row/column and data and put the changed background into the options and call the base class implementation in the end:
@
void MyItemDelegate::paint( QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index )
{
QStyleOptionViewItemV4 newOption(option);
if(index.row() == 5) {
if(index.data(Qt::CheckStateRole) == Qt::Checked) {
newOption.backgroundBrush = QBrush(Qt::yellow);
}
}
QStyledItemDelegate::paint(painter, newOption, index);
}
@(Brain to terminal, code is not tested)
You get the total number of rows using model->rowCount().
-
Thanks I'll try
-
Yes i worked but i want to loop thru the datamodel and check all checkboxes.
I thought i did that with my foreach but sometimes i got the right rowcount and the loop dosent work and sometimes i go thru the loop but only on one row. -
The loop is executed by the view, it calls the delegate with the needed indexes.
-
I'll explain this in a new way.
After i put all my values in the model()(QStandardItemModel)
I put the model in the tableview.When this is done I try to loop thru the datamodel and tries to check if the
@item->setFlags(Qt::ItemIsEnabled|Qt::ItemIsUserCheckable);
@
is checked.If so I want to change color of that row.
thats why I want to loop thru this.
I'm sure that this is a simple task but I'm stuck now
-
OK, so you want to color a complete row based on a set of conditions. Sounds like a job for a proxy model to me.
Something like:
@
//hightlightproxy.h
//in Qt 4.8, you should use QIdentityProxyModel (I think it is called, please check).#if QT_VERSION >= 0x040800
#include <QIndentityProxy>
class HighLightProxy : public QIdentityProxyModel
{
typedef baseClass QIdentityProxyModel;
#else
#include <QSortFilterProxyModel>
class HighLightProxy : public QSortFilterProxyModel
{
typedef baseClass QSortFilterProxyModel;
#endif
Q_OBJECTpublic:
HighLightProxy(QObject* parent = 0);void setRow(int row); void setCheckColumn(column); void setHighlightBrush(const QBrush& brush); virtual QVariant data(const QModelIndex & index, int role = Qt::DisplayRole ) const;
private:
int m_row;
int m_checkColumn;
QBrush m_brush;
}
@@
//hightlightproxy.cpp
HighLightProxy::HighLightProxy(QObject* parent) :
baseClass(parent),
m_row(-1),
m_checkColumn(-1)
{}QVariant HighLightProxy::data(const QModelIndex & index, int role ) const
{
if (!index.isValid())
return QVariant();//lets assume you are not talking about a tree-type model here, and ignore parents
if (index.row() == m_row && role == Qt::BackGroundRole) {
//check the checkstate
if (m_checkColumn > -1) {
QModelIndex sIndex = sourceModel()->index(m_row, m_checkColumn);
if (sIndex.data(Qt::CheckStateRole).toInt() == Qt::Checked) {
return m_highlightBrush;
}
}
}
return baseClass::data(index, role);
}//implementation of setters declared in header is trivial and thus not shown.
@Note: written in forum editor, not complete, nor tested in a compiler.
-
I'll try this but I'm stuck with how to loop thru the tableview or datamodel to.
@foreach(datamodel,???)
{
code.....
]
@
A simpel loop but Not in my head for the moment.pls help
-
With the approach above, you don't need to loop through the view or the model.
-
Thanks for the code and its OK
No problems after a while.