PDA

View Full Version : About setIndexWidget



aekilic
11th December 2009, 11:46
Dear All

I create a table view like


QSqlQuery q;
q.exec("crate view .....");

QSqlTableModel *model = new QSqlTableModel ;
model ->setTable("tum_hareket_fisleri");
model ->select();

tableView->setModel(model );
for (int i = 0; i < model ->rowCount(); ++i) {
QCheckBox *cbox = new QCheckBox(tableView);
tableView->setIndexWidget(model ->index(i, 16), cbox);
}


But I am not able to get a check box in my table any idea?

aekilic
11th December 2009, 19:40
any one? any idea?

wysota
11th December 2009, 20:37
How many columns does your sql model have?

aekilic
11th December 2009, 20:46
I got 15

I try to



tableView->insertColumn(16);



But notting changes

wysota
11th December 2009, 22:07
I got 15
So trying to insert something into the 17th column probably shouldn't have any effect, should it?


I try to



tableView->insertColumn(16);



But notting changes

You can't add columns to sql models just like that.

I think what you really want is to use Qt::CheckStateRole. That's only a teaser - how to use it is left as an exercise to you.

aekilic
11th December 2009, 23:00
I did it with

Qt::CheckStateRole

But the problem is check box is not editable...

wysota
12th December 2009, 08:47
Because you didn't implement setData() and flags() for your model.

aekilic
12th December 2009, 09:13
I do it like


class MyModel : public QSqlQueryModel {
Q_OBJECT
public:
MyModel(QObject *parent = 0);
Qt::ItemFlags flags(const QModelIndex &index) const;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
........
};

Qt::ItemFlags MyModel::flags(const QModelIndex &index) const {
Qt::ItemFlags flags = QSqlQueryModel::flags(index);
if (index.column() == aColWithCheckbox)
flags |= Qt::ItemIsUserCheckable;
else
flags |= Qt::ItemIsEditable;
return flags;
}

QVariant MyModel::data(const QModelIndex &index, int role) const {
QVariant value = QSqlQueryModel::data(index, role);
if (role == Qt::CheckStateRole && index.column() == aColWithCheckbox)
return (QSqlQueryModel::data(index).toInt() != 0) ? Qt::Checked : Qt::Unchecked;
else
return value;
}

But as I have told you I could not check the qcheckbox

aekilic
12th December 2009, 11:56
do I missed anything?

wysota
12th December 2009, 12:05
You didn't implement setData().

aekilic
12th December 2009, 12:45
I have writen the setData didnt paste

my code is exactly like this



Qt::ItemFlags CustomModel::flags(const QModelIndex &index) const {
Qt::ItemFlags flags = QSqlTableModel::flags(index);
if (index.column() == 16)
flags |= Qt::ItemIsUserCheckable | Qt::ItemIsEditable;
return flags;
}

QVariant CustomModel::data(const QModelIndex &index, int role) const
{
if (role == Qt::CheckStateRole && index.column() == 16)
return (QSqlQueryModel::data(index).toInt() != 0) ? Qt::Checked : Qt::Unchecked;

return value;
}
bool CustomModel::setData(const QModelIndex &index, const QVariant &value, int role) {
if(role==Qt::CheckStateRole)
return QSqlTableModel::setData(index,value.toBool(),Qt::E ditRole);
return QSqlTableModel::setData(index,value);
}


The check box on the table is still not editable, I can not check the qcheckbox...

wysota
12th December 2009, 19:44
One time you are using QSqlQueryModel as your base class and the other time you are using QSqlTableModel. So which one is it?

aekilic
14th December 2009, 23:17
Sorry

QSqlTableModel it should be, working right now sorry for the delay and time, but right now I had a problem I could not check more than 2 rows?

wysota
14th December 2009, 23:46
Did you solve the previous problem? What was it?

aekilic
15th December 2009, 07:04
I was not able to check the check box, but I could do it right now, but I could not check more than 2 check box, When I press the 3rd check box, the 1st check is removed.

wysota
15th December 2009, 08:06
Please show us the current code.

aekilic
15th December 2009, 08:12
Qt::ItemFlags CustomModel::flags(const QModelIndex &index) const {
Qt::ItemFlags flags = QSqlTableModel::flags(index);
if (index.column() == 16)
flags |= Qt::ItemIsUserCheckable | Qt::ItemIsEditable;
return flags;
}

QVariant CustomModel::data(const QModelIndex &index, int role) const
{
QVariant value = QSqlTableModel::data(index, role);
if (index.column() == 16) {
if (role == Qt::CheckStateRole)
return (QSqlTableModel::data(index).toInt() != 0) ? Qt::Checked : Qt::Unchecked;
}
return value;
}
bool CustomModel::setData(const QModelIndex &index, const QVariant &value, int role) {
if(role==Qt::CheckStateRole)
return QSqlTableModel::setData(index,value.toBool(),Qt::E ditRole);
return QSqlTableModel::setData(index,value,Qt::EditRole);
}


any thing you could suggest

aekilic
15th December 2009, 13:31
is this possible, when I try to click for the 3rd check box, can the model be requested again? Because that is the only reason I could think.

if it is like that, the only think I would like to do is check of the the rows on the table and send a signal take the id of the checked rows, but in this case I could not check more than 2 items

wysota
15th December 2009, 14:01
Do you want to save the changes you make using checkboxes to the SQL database?

aekilic
15th December 2009, 15:01
no I dont want to

wysota
15th December 2009, 16:08
So why are you pushing them to the model? Use a proxy model or something... But certainly don't call QSqlTableModel::setData() and QSqlTableModel::data() when using the check state role.