[ SOLVED ]Problem in getting mouseEvent on QTableWidget



  • I want to have mouse press event & mouse release event on QTableWidget

    my code is

    @
    #include <QMouseEvent>
    #include <QKeyEvent>
    #include <QEvent>

    CTestDlg::CTestDlg()
    {
    ui->setupUi(this);
    ui->TableWidget->setSelectionMode( QAbstractItemView::ExtendedSelection );
    ui->TableWidget->setMouseTracking( true );
    ui->TableWidget->installEventFilter( this );
    }
    bool CTestDlg::eventFilter(QObject *object, QEvent *event)
    {

    if (event->type() == QEvent::MouseButtonPress )
    {
        
        if (  object == ui->TableWidget )
        {
            qDebug()<<"MOUSE BUTTON PRESSED";         
            
        }
        return QObject::eventFilter(object, event);
    
    }
    else if (event->type() == QEvent::MouseButtonRelease )
    {
        if ( object == ui->TableWidget )
        {
            qDebug()<<"MOUSE BUTTON PRESSED";         
         }
          return QObject::eventFilter(object, event);
    
    }
    
    return QObject::eventFilter(object, event);
    

    }
    @

    while i click the tablewidget on my Dialog window , i am not at all getting any output as either "MOUSE BUTTON PRESSED" or "MOUSE BUTTON RELESED"....please let me know...whether i am missing anything on my code...



  • You should probably install the event filter not on the TableWidget itself, but on ui->TableWidget->viewPort()



  • After changing into viewport()...
    like
    @
    #include <QMouseEvent>
    #include <QKeyEvent>
    #include <QEvent>
    CTestDlg::CTestDlg()
    {
    ui->setupUi(this);
    ui->TableWidget->setSelectionMode( QAbstractItemView::ExtendedSelection );
    ui->TableWidget->setMouseTracking( true );
    ui->TableWidget->viewport()->installEventFilter( this );
    }

    bool CTestDlg::eventFilter(QObject *object, QEvent *event)
    {

    if (event->type() == QEvent::MouseButtonPress )
    {
    qDebug()<<"MOUSE BUTTON PRESSED @";
    if ( object == ui->TableWidget )
    {
    QTableWidgetItem *item = ui->TableWidget->currentItem();
    qDebug()<<item->row()<<":"<<item->column();
    }
    return QObject::eventFilter(object, event);
    }
    else if (event->type() == QEvent::MouseButtonRelease )
    {
    qDebug()<<"MOUSE BUTTON RELEASED @";
    if ( object == ui->TableWidget )
    {
    QTableWidgetItem *item = ui->TableWidget->currentItem();
    qDebug()<<item->row()<<":"<<item->column();
    }
    return QObject::eventFilter(object, event);
    }
    return QObject::eventFilter(object, event);

    }
    @

    Now i am getting only the "MOUSE BUTTON PRESSED @" & "MOUSE BUTTON RELEASED @"

    but not the item row or column value..!!!



  • object is not the tableview, it's the view port!

    @
    bool CTestDlg::eventFilter(QObject *object, QEvent *event)
    {
    if (event->type() == QEvent::MouseButtonPress )
    {
    qDebug()<<"MOUSE BUTTON PRESSED @";
    if ( object == ui->TableWidget->viewport() ) // changed here
    {
    QTableWidgetItem *item = ui->TableWidget->currentItem();
    qDebug()<<item->row()<<":"<<item->column();
    }
    return QObject::eventFilter(object, event);
    }
    else if (event->type() == QEvent::MouseButtonRelease )
    {
    qDebug()<<"MOUSE BUTTON RELEASED @";
    if ( object == ui->TableWidget->viewport() ) // changed here
    {
    QTableWidgetItem *item = ui->TableWidget->currentItem();
    qDebug()<<item->row()<<":"<<item->column();
    }
    return QObject::eventFilter(object, event);
    }
    return QObject::eventFilter(object, event);
    }
    @



  • Note that lines 11 and 21 do nothing in the example above. At least, nothing that line 23 would not do just as well... ;-)

    Edit:
    comment is obviously pointed at M_31, not at Gerolf :-)



  • I only fixed the compare statement, not optimized the whole code ;-)



  • There is a way to know if is the right or left button thet he press?

    Luca



  • Cast the event to a QMouseEvent and use the members



  • Now i am able to get the co-ordinates from mouse press & release event

    @
    if( object == ui->TableWidget->viewport() )
    {
    QMouseEvent mouseEvent = static_cast <QMouseEvent>( event );
    qDebug()<<"MouseButtonPress event!@"<<mouseEvent->x()<<":"<<mouseEvent->y();

            QTableWidgetItem *item =  ui->TableWidget->currentItem();
           // qDebug()<<"ITEMS !@"<<item->row()<<":"<<item->column();
    

    //This line is not working ..my app is getting crashing ..saying that ..No value found in "item"

        }
    

    @

    Is there any other to get the QTableWidget row & column value using mouse event other than X & Y
    co-ordinates....



  • [quote author="M_31" date="1316014954"]Now i am able to get the co-ordinates from mouse press & release event

    @
    if( object == ui->TableWidget->viewport() )
    {
    QMouseEvent mouseEvent = static_cast <QMouseEvent>( event );
    qDebug()<<"MouseButtonPress event!@"<<mouseEvent->x()<<":"<<mouseEvent->y();

            QTableWidgetItem *item =  ui->TableWidget->currentItem();
           // qDebug()<<"ITEMS !@"<<item->row()<<":"<<item->column();
    

    //This line is not working ..my app is getting crashing ..saying that ..No value found in "item"

        }
    

    @

    Is there any other to get the QTableWidget row & column value using mouse event other than X & Y
    co-ordinates....[/quote]

    No, it is not.

    The only way is to use "indexAt":http://doc.qt.nokia.com/latest/qabstractitemview.html#indexAt



  • Hello i have one question :

    @bool Widget::eventFilter(QObject *object, QEvent *event){
    if (object == ui->tableWidget->viewport()){
    QMouseEvent mouseEvent = static_cast<QMouseEvent>(event);
    if (mouseEvent->button()==2){
    messaggio = new QMessageBox;
    messaggio->information(this,tr("Packing"),tr("Tasto destro schiacciato"),messaggio->Ok);
    }
    }
    }@

    Why the message is open two time when i click the right button?

    Thanks

    Luca



  • it is called for mousepress and mouserelease... :-)
    and you even do not check, which one is called...

    @
    bool Widget::eventFilter(QObject *object, QEvent *event)
    {
    if (object == ui->tableWidget->viewport())
    {
    QMouseEvent mouseEvent = static_cast<QMouseEvent>(event);
    if ((QEvent::MouseButtonPress == event->type()) && (mouseEvent->button()==Qt::RightButton))
    {
    messaggio = new QMessageBox;
    messaggio->information(this,tr("Packing"),tr("Tasto destro schiacciato"),messaggio->Ok);
    }
    }
    }
    @

    Additionally, I suggest not to use magic numbers, if there are enums that can be used...



  • Thanks Gerolf...for your valuable inputs..
    Its working fine with viewport and i achived my task...



  • Hi, everyone!

    I had a similar problem and used a similiar approach to it, but I faced an obstacle when used method QTableWidget::itemAt: it uses coordinates relative to the left upper corner of data cells and doesn't take to account header cells!
    So, when I transformed global coordiantes to the widget's ones via QWidget::mapFromGlobal I obtained not what I needed at all!
    It gave me coordinates relative to the left upper corner of the widget itself, not the data cells.
    How can I obtain the proper coordinates?

    I wanted to try methods QTableView::horizontalOffset() and QTableView::verticalOffset(), but they are protected :(
    (Sorry for my English, it's not my native language)

    [quote author="Gerolf" date="1316016131"]
    No, it is not.

    The only way is to use "indexAt":http://doc.qt.nokia.com/latest/qabstractitemview.html#indexAt[/quote]



  • Hi, everyone!

    I had a similar problem and used a similiar approach to it, but I faced an obstacle when used method QTableWidget::itemAt: it uses coordinates relative to the left upper corner of data cells and doesn't take to account header cells!
    So, when I transformed global coordiantes to the widget's ones via QWidget::mapFromGlobal I obtained not what I needed at all!
    It gave me coordinates relative to the left upper corner of the widget itself, not the data cells.
    How can I obtain the proper coordinates?

    I wanted to try methods QTableView::horizontalOffset() and QTableView::verticalOffset(), but they are protected :(
    (Sorry for my English, it's not my native language)

    [quote author="Gerolf" date="1316016131"]
    No, it is not.

    The only way is to use "indexAt":http://doc.qt.nokia.com/latest/qabstractitemview.html#indexAt[/quote]



  • Well, as you can see from the QAbstractScrollArea documentation, of which QTableWidget is derived, the actual scrolling area is provided by a widget called viewport. That widget can be also be used for your coordinate transformations.



  • Well, as you can see from the QAbstractScrollArea documentation, of which QTableWidget is derived, the actual scrolling area is provided by a widget called viewport. That widget can be also be used for your coordinate transformations.



  • Thank you, that's what I need!
    Sorry I failed to find it myself...

    I guess it would be advisable to add a link to viewport() member into QTableWidgetItem::itemAt() documentation.

    [quote author="Andre" date="1421132571"]Well, as you can see from the QAbstractScrollArea documentation, of which QTableWidget is derived, the actual scrolling area is provided by a widget called viewport. That widget can be also be used for your coordinate transformations.[/quote]



  • Thank you, that's what I need!
    Sorry I failed to find it myself...

    I guess it would be advisable to add a link to viewport() member into QTableWidgetItem::itemAt() documentation.

    [quote author="Andre" date="1421132571"]Well, as you can see from the QAbstractScrollArea documentation, of which QTableWidget is derived, the actual scrolling area is provided by a widget called viewport. That widget can be also be used for your coordinate transformations.[/quote]


Log in to reply
 

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