QTableWidget, any easy way to make only some items tab stoppable



  • Have used QTableWidgetItem::setFlags to disable ItemIsSelectable and ItemIsEditable, but the item is still tab stoppable, any easy way to make a table item tab unstoppable?


  • Qt Champions 2016

    Hi
    What is stoppable and unstoppable ?
    Never seen these words in relation to QTableWidgetItem :)



  • @mrjj For example

    1. a 2x2 table
    2. focus on cell (0,1)
    3. press Tab key, focus to cell(1.1) instead of cell(1,0)
      Is there any built in support for this?

  • Qt Champions 2016

    @jronald
    HI
    I think
    http://doc.qt.io/qt-5/qabstractitemview.html#moveCursor
    can be used but I have not tested it.



  • @mrjj
    Helpful
    QAbstractItemView::setCurrentIndex can be used to move to any cell
    Though not so built in, but also very good way



  • Try reimplementing moveCursor

    
    #include <QTableWidget>
    #include <QAbstractItemModel>
    class MyTableWidget : public QTableWidget{
        Q_OBJECT
        Q_DISABLE_COPY(MyTableWidget)
    public:
        explicit MyTableWidget(QWidget* parent = Q_NULLPTR)
            :QTableWidget(parent)
        {}
    protected:
        QModelIndex moveCursor(CursorAction cursorAction, Qt::KeyboardModifiers modifiers) Q_DECL_OVERRIDE{
            QModelIndex beginIdx = currentIndex();
            QModelIndex currIdx, prevIdx;
            if(model()){
                for(;;){
                    currIdx = QTableWidget::moveCursor(cursorAction,modifiers);
                    if(!beginIdx.isValid())
                        beginIdx=currIdx;
                    if(!currIdx.isValid())
                        break;
                    if(model()->flags(currIdx) & Qt::ItemIsSelectable)
                        break;
                    if(currIdx==beginIdx)
                        break;
                    if(prevIdx==currIdx)
                        break;
                    prevIdx=currIdx;
                    setCurrentIndex(currIdx);
                }
            }
            return currIdx;
        }
    
    };
    
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.