[Solved]Get value of checkbox in a qtableview
-
wrote on 20 Oct 2011, 06:54 last edited by
Hello
I need some help to get the checked value in a tableview.My code looks like this
@ QModelIndexList selectedList= ui->testTable->selectionModel()->selectedRows(5);//colum som läggs till listan
QModelIndex index = ui->testTable->currentIndex();QBrush cyan; cyan.setColor(Qt::cyan); cyan.setStyle(Qt::SolidPattern); foreach (index,selectedList ) { if (dataModel->data(index)) dataModel->setData(index, cyan, Qt::BackgroundRole); }
@
But i need to loop thrue the hole datamodel and check every box.
A hint someone
-
wrote on 20 Oct 2011, 07:36 last edited by
Hi,
from your code, it's not clear what you want to achieve. It does not fir to the question.
Inside the code, you iterate all selected cells in column 5 and set the background color. Where comes the checked state into the game? -
wrote on 20 Oct 2011, 07:54 last edited by
If you want to query the checked state of a model, simply use the correct data role Qt::CheckStateRole, which returns a QVariant containing a Qt::CheckState.
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 [[doc:QItemDelegate]].
-
wrote on 20 Oct 2011, 08:06 last edited by
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.
-
wrote on 20 Oct 2011, 08:27 last edited by
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().
-
wrote on 20 Oct 2011, 08:40 last edited by
Thanks I'll try
-
wrote on 20 Oct 2011, 08:57 last edited by
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. -
wrote on 20 Oct 2011, 09:04 last edited by
The loop is executed by the view, it calls the delegate with the needed indexes.
-
wrote on 20 Oct 2011, 09:18 last edited by
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
-
wrote on 20 Oct 2011, 09:27 last edited by
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.
-
wrote on 20 Oct 2011, 09:37 last edited by
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
-
wrote on 20 Oct 2011, 10:16 last edited by
With the approach above, you don't need to loop through the view or the model.
-
wrote on 20 Oct 2011, 10:44 last edited by
Thanks for the code and its OK
No problems after a while.
1/13