Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QTableWidget - get column/row from under mouse ?



  • Hey

    I'd like to print current row/column from under mouse position (in mouse move event) as well as item. I know I can get itemAt(event->pos()) for item, but if there is no item there I get null, how can I check what column/row that cursor is in now?

    TIA


  • Lifetime Qt Champion

    Hi,

    Can you show the current code you are using ?



  • Actually I found out that I need cellEntered signal, but that does not happen as I'm dragging data in to the view, I just want to know whats under my mouse and if I need to make new cell/new data widget in cell or drop in to existing data widget in cell.

    Not much code yet... except for basic QDropEvent()/dragEnterEvent/dragMoveEvent events...


  • Lifetime Qt Champion

    The events you get as parameter gives you the position of the cursor. So you can check what's under it more precisely.


  • Lifetime Qt Champion

    Hi
    You can use
    https://doc.qt.io/qt-5/qtablewidget.html#itemAt-1
    to get item under the cursor.
    You can then ask Item for Col/row if u need that.



  • @mrjj said in QTableWidget - get column/row from under mouse ?:

    Hi
    You can use
    https://doc.qt.io/qt-5/qtablewidget.html#itemAt-1
    to get item under the cursor.
    You can then ask Item for Col/row if u need that.

    This return null if there is no item in column.

    @SGaist said in QTableWidget - get column/row from under mouse ?:

    The events you get as parameter gives you the position of the cursor. So you can check what's under it more precisely.

    I get no event when I'm dragging in a data from other widget/view/source.


  • Lifetime Qt Champion

    Can you show your current implementation ?



  • filterDropIndator() is where I try to figure out the position of the mouse...

    
    void TableWidget::dragEnterEvent(QDragEnterEvent *event) {
        if (event->source() == this) {
            qDebug() << "Internal";
            setDragDropMode(QAbstractItemView::InternalMove);
        } else {
            qDebug() << "External";
            setDragDropMode(QAbstractItemView::DropOnly);
        }
        if (event->mimeData()->hasFormat("templateItemDrag")) {
            qDebug() << "111" << event << "\n3213213" << event->proposedAction() << event->possibleActions() << event->dropAction();
            event->acceptProposedAction();
        }
        //QAbstractItemView::dragEnterEvent(event);
    }
    void TableWidget::dragMoveEvent(QDragMoveEvent *event) {
        event->accept();
        filterDropIndator(event);
        //QAbstractItemView::dragMoveEvent(event);
    }
    void TableWidget::dragLeaveEvent(QDragLeaveEvent *event) {
        QAbstractItemView::dragLeaveEvent(event);
    }
    void TableWidget::dropEvent(QDropEvent *event) {
        qDebug() << "yay ?v3whyg345hg3w";
        if (event->mimeData()->hasFormat("templateItemDrag")) {
            qDebug() << "2132145215215";
            event->acceptProposedAction();
            return;
        }
        QTableWidget::dropEvent(event);
    }
    TableWidget::TableWidget(QWidget *parentPtr) : QTableWidget(parentPtr) {
        setAcceptDrops(true);
        setDragEnabled(true);
        //viewport()->setAcceptDrops(true);
        //setDragDropOverwriteMode(false);
        setDropIndatorShown(true);
        setDefaultDropAction(Qt::MoveAction);
        setColumnCount(10);
        setRowCount(1);
        connect(this, &QTableWidget::cellEntered, this, &TableWidget::newMousePos);
    }
    TableWidget::~TableWidget() {
    
    }
    void TableWidget::filterDropIndator(QDragMoveEvent *event) {
        auto *w = itemAt(event->pos());
        if (w) {
    
        } else {
            qDebug() << "Row pos/col = " << mRow << mColumn;
        }
        qDebug() << "filtering indator :" << w;
    
    }
    void TableWidget::newMousePos(int row, int column) {
        mRow = row;
        mColumn = column;
    }
    


  • Ok solved, Im gigantic NUB. I used itemAt instead of indexAt and took me a while to notice it... everything works! thanks!!!


Log in to reply