Qtableview with Standard model focus problem
-
I have created Qtableview and setted a model
expiry date has a dateEdit delegate
i want to disable selection for some columns such as name , price ,cost , t. price and t. cost
so when the user press tab it will not be selected or focused
how can i do that?
please answer,I really need this . -
@VRonin said in Qtableview with Standard model focus problem:
Qt::ItemIsSelectable
it worked but the item looks disabled
-
QString qname = qry.value(0).toString(); QString qprice = qry.value(1).toString(); QString qtax = qry.value(2).toString(); //int qmin = qry.value(3).toInt(); QString qsale = qry.value(4).toString(); //-->creating the items QStandardItem *iname = new QStandardItem(qname); iname->setFlags(Qt::ItemIsSelectable); iname->setEditable(false); QStandardItem *iPB = new QStandardItem(); iPB->setCheckable(true); iPB->setEditable(false); QStandardItem *iquantity = new QStandardItem("1"); QStandardItem *iexpiry = new QStandardItem("01/18"); QStandardItem *isale = new QStandardItem(qsale); isale->setEditable(false); isale->setFlags(Qt::ItemIsSelectable); QStandardItem *itax = new QStandardItem(qtax); itax->setEditable(false); itax->setFlags(Qt::ItemIsSelectable); QStandardItem *iprice = new QStandardItem(qprice); iprice->setEditable(false); iprice->setFlags(Qt::ItemIsSelectable); QStandardItem *icost = new QStandardItem(); icost->setEditable(false); icost->setFlags(Qt::ItemIsSelectable); QStandardItem *itprice = new QStandardItem(qprice); itprice->setEditable(false); itprice->setFlags(Qt::ItemIsSelectable); QStandardItem *itcost = new QStandardItem(); itcost->setEditable(false); itcost->setFlags(Qt::ItemIsSelectable); //-->fell the model with this items model->setItem(row, 0, iname); model->setItem(row, 1, iPB); model->setItem(row, 2, iquantity); model->setItem(row, 3, iexpiry); model->setItem(row, 4, isale); model->setItem(row, 5, itax); model->setItem(row, 6, iprice); model->setItem(row, 7, icost); model->setItem(row, 8, itprice); model->setItem(row, 9, itcost); //-->end ui->itemsTV->setModel(model);
-
-
To disable it completely you need to do the same thing with the
Qt::ItemIsEnabled
flag. Then implement a custom delegate:class NoGrayDelegate : public QStyledItemDelegeate{ Q_OBJECT Q_DISABLE_COPY(NoGrayDelegate) public: NoGrayDelegate(QObject* parent = Q_NUULPTR) : QStyledItemDelegeate(parent){} void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const Q_DECL_OVERRIDE { Q_ASSERT(index.isValid()); QStyleOptionViewItem opt = option; initStyleOption(&opt, index); opt->state | QStyle::State_Enabled; QStyle *style = option.widget ? option.widget->style() : QApplication::style(); style->drawControl(QStyle::CE_ItemViewItem, &opt, painter, option.widget); } };
and call something like
ui->itemsTV->setItemDelegateForColumn(1, new NoGrayDelegate(this));
for every column you want disabled but with black text -
@VRonin
it has some syntax error but i managed to solve themclass NoGrayDelegate : public QStyledItemDelegate{ Q_OBJECT Q_DISABLE_COPY(NoGrayDelegate) public: NoGrayDelegate(QObject* parent = nullptr) : QStyledItemDelegate(parent){} void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const Q_DECL_OVERRIDE { Q_ASSERT(index.isValid()); QStyleOptionViewItem opt = option; initStyleOption(&opt, index); opt.state | QStyle::State_Enabled; QStyle *style = option.widget ? option.widget->style() : QApplication::style(); style->drawControl(QStyle::CE_ItemViewItem, &opt, painter, option.widget); } };
still the same
i have to press tab 10 times to go the same column but in the other row -
sorry, my code missed a
=
.opt.state | QStyle::State_Enabled;
should beopt.state |= QStyle::State_Enabled;
i have to press tab 10 times to go the same column but in the other row
can you show us the code where you set the flags and set the delegate?
-
still the same
class NoGrayDelegate : public QStyledItemDelegate{ Q_OBJECT Q_DISABLE_COPY(NoGrayDelegate) public: NoGrayDelegate(QObject* parent = nullptr) : QStyledItemDelegate(parent){} void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const Q_DECL_OVERRIDE { Q_ASSERT(index.isValid()); QStyleOptionViewItem opt = option; initStyleOption(&opt, index); opt.state |= QStyle::State_Enabled; QStyle *style = option.widget ? option.widget->style() : QApplication::style(); style->drawControl(QStyle::CE_ItemViewItem, &opt, painter, option.widget); } };
ui->itemsTV->setItemDelegateForColumn(0, new NoGrayDelegate(this)); ui->itemsTV->setItemDelegateForColumn(3, new DateEditDelegate(this)); ui->itemsTV->setItemDelegateForColumn(4, new NoGrayDelegate(this)); ui->itemsTV->setItemDelegateForColumn(5, new NoGrayDelegate(this)); ui->itemsTV->setItemDelegateForColumn(6, new NoGrayDelegate(this)); ui->itemsTV->setItemDelegateForColumn(7, new NoGrayDelegate(this)); ui->itemsTV->setItemDelegateForColumn(8, new NoGrayDelegate(this)); ui->itemsTV->setItemDelegateForColumn(9, new NoGrayDelegate(this));
-
@VRonin ok it worked
now i have another problem in focus
look at the image above
when the page opens it will automaticity set the focus on supplier combobox
[TAB] the user go to no. lineedit
and so on
but i want when user press enter on expirydate it set the focus on add drug line edit
and when the user press enter from add drug line edit it goes to the new line row ready to edit the qua + bo and so on