Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Qtableview with Standard model focus problem
Forum Updated to NodeBB v4.3 + New Features

Qtableview with Standard model focus problem

Scheduled Pinned Locked Moved Solved General and Desktop
14 Posts 2 Posters 2.2k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • VRoninV VRonin

    can you show the code you used?

    ? Offline
    ? Offline
    A Former User
    wrote on last edited by A Former User
    #5

    @VRonin

            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);
    

    0_1542800510939_058d921d-8000-426a-9b1f-c0017e21f2d4-image.png

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #6

      Yep, you are overriding the existing flags.
      For an item to become not selectable you have to use itprice->setFlags(itprice->flags() & ~Qt::ItemIsSelectable); to make it selectable again use itprice->setFlags(itprice->flags() | Qt::ItemIsSelectable);

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      ? 1 Reply Last reply
      1
      • VRoninV VRonin

        Yep, you are overriding the existing flags.
        For an item to become not selectable you have to use itprice->setFlags(itprice->flags() & ~Qt::ItemIsSelectable); to make it selectable again use itprice->setFlags(itprice->flags() | Qt::ItemIsSelectable);

        ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #7

        @VRonin
        0_1542801974563_d109796c-7e97-48c2-b45c-52f9fe46e060-image.png
        now it's not selectable but it take focus
        i mean that if where on row 1 column Qua + bo
        you will press tab 10 times to go to the Qua + bo in row 2

        1 Reply Last reply
        0
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by VRonin
          #8

          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

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          1 Reply Last reply
          1
          • ? Offline
            ? Offline
            A Former User
            wrote on last edited by
            #9

            @VRonin
            it has some syntax error but i managed to solve them

            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);
            }
            };
            

            still the same
            i have to press tab 10 times to go the same column but in the other row

            1 Reply Last reply
            0
            • VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by VRonin
              #10

              sorry, my code missed a =. opt.state | QStyle::State_Enabled; should be opt.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?

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              1 Reply Last reply
              0
              • ? Offline
                ? Offline
                A Former User
                wrote on last edited by
                #11

                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));
                
                1 Reply Last reply
                0
                • VRoninV Offline
                  VRoninV Offline
                  VRonin
                  wrote on last edited by VRonin
                  #12

                  Where are you setting the flags? you should have something like itprice->setFlags(itprice->flags() & ~Qt::ItemIsSelectable & ~Qt::ItemIsEnabled);

                  "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                  ~Napoleon Bonaparte

                  On a crusade to banish setIndexWidget() from the holy land of Qt

                  1 Reply Last reply
                  0
                  • ? Offline
                    ? Offline
                    A Former User
                    wrote on last edited by
                    #13

                    @VRonin ok it worked

                    now i have another problem in focus
                    0_1542808007489_5e9a7503-edb8-43ba-92ed-8729cabc72f4-image.png

                    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

                    1 Reply Last reply
                    0
                    • VRoninV Offline
                      VRoninV Offline
                      VRonin
                      wrote on last edited by
                      #14

                      create a QStyledItemDelegate set it to the expirydate column. Then connect to the signal QAbstractItemDelegate::closeEditor to know when the user finished editing and in the slot you can manage the focus however you want

                      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                      ~Napoleon Bonaparte

                      On a crusade to banish setIndexWidget() from the holy land of Qt

                      1 Reply Last reply
                      2

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved