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. [ SOLVED ]Problem in getting mouseEvent on QTableWidget
Forum Updated to NodeBB v4.3 + New Features

[ SOLVED ]Problem in getting mouseEvent on QTableWidget

Scheduled Pinned Locked Moved General and Desktop
19 Posts 5 Posters 21.5k Views 1 Watching
  • 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.
  • G Offline
    G Offline
    giesbert
    wrote on last edited by
    #4

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

    Nokia Certified Qt Specialist.
    Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on last edited by
      #5

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

      1 Reply Last reply
      0
      • G Offline
        G Offline
        giesbert
        wrote on last edited by
        #6

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

        Nokia Certified Qt Specialist.
        Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

        1 Reply Last reply
        0
        • L Offline
          L Offline
          luca72
          wrote on last edited by
          #7

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

          Luca

          1 Reply Last reply
          0
          • G Offline
            G Offline
            giesbert
            wrote on last edited by
            #8

            Cast the event to a QMouseEvent and use the members

            Nokia Certified Qt Specialist.
            Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

            1 Reply Last reply
            0
            • M Offline
              M Offline
              M_31
              wrote on last edited by
              #9

              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....

              1 Reply Last reply
              0
              • G Offline
                G Offline
                giesbert
                wrote on last edited by
                #10

                [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

                Nokia Certified Qt Specialist.
                Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

                1 Reply Last reply
                0
                • L Offline
                  L Offline
                  luca72
                  wrote on last edited by
                  #11

                  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

                  1 Reply Last reply
                  0
                  • G Offline
                    G Offline
                    giesbert
                    wrote on last edited by
                    #12

                    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...

                    Nokia Certified Qt Specialist.
                    Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

                    1 Reply Last reply
                    0
                    • M Offline
                      M Offline
                      M_31
                      wrote on last edited by
                      #13

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

                      1 Reply Last reply
                      0
                      • T Offline
                        T Offline
                        timswoosh
                        wrote on last edited by
                        #14

                        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]

                        1 Reply Last reply
                        0
                        • T Offline
                          T Offline
                          timswoosh
                          wrote on last edited by
                          #15

                          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]

                          1 Reply Last reply
                          0
                          • A Offline
                            A Offline
                            andre
                            wrote on last edited by
                            #16

                            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.

                            1 Reply Last reply
                            0
                            • A Offline
                              A Offline
                              andre
                              wrote on last edited by
                              #17

                              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.

                              1 Reply Last reply
                              0
                              • T Offline
                                T Offline
                                timswoosh
                                wrote on last edited by
                                #18

                                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]

                                1 Reply Last reply
                                0
                                • T Offline
                                  T Offline
                                  timswoosh
                                  wrote on last edited by
                                  #19

                                  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]

                                  1 Reply Last reply
                                  0

                                  • Login

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