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. Trouble with QTableWidget right-click event
Forum Updated to NodeBB v4.3 + New Features

Trouble with QTableWidget right-click event

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 3.8k 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.
  • I Offline
    I Offline
    Imhotep
    wrote on last edited by
    #1

    I was able to simulate the Right-Click event by subclassing the QTableWidget:

    header file:
    @
    #ifndef QRIGHCLICKTABLE_H
    #define QRIGHCLICKTABLE_H

    #include <QTableWidget>
    #include <QMouseEvent>

    class QRightClickTable : public QTableWidget
    {
    Q_OBJECT

    public:
    explicit QRightClickTable(QWidget *parent = 0);

    private slots:
    void mousePressEvent(QMouseEvent *e);

    signals:
    void rightClicked();

    public slots:

    };

    #endif // QRIGHCLICKTABLE_H
    @

    cpp file
    @
    QRightClickTable::QRightClickTable(QWidget *parent) :
    QTableWidget(parent)
    {
    }

    void QRightClickTable::mousePressEvent(QMouseEvent *e)
    {
    if(e->button()==Qt::RightButton)
    emit rightClicked();
    }

    QRightClickTable *tablewidget = new QRightClickTable(this);
    ui->gridLayout->addWidget(tablewidget);
    connect(tablewidget, SIGNAL(rightClicked()), this, SLOT(onRightClicked()));

    void MainWindow::onRightClicked()
    {
    qDebug() << "User right clicked me";
    }
    @

    Now, right-click works correctly, but there are other problems with QTableWidget: all other mouse events, such as the left click to select a cell, no longer work. Can you help me? I know I need to call the base class implementation in your override of mousePressEvent, you could show me how?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      You need to call the base class implementation of the method if you want the original behavior plus your own.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • I Offline
        I Offline
        Imhotep
        wrote on last edited by
        #3

        Could you show me how?

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Please, do your homework, this is basic C++

          @
          void QRightClickTable::mousePressEvent(QMouseEvent *e)
          {
          if(e->button()==Qt::RightButton) {
          emit rightClicked();
          }
          QTableWidget::mousePressEvent(QMouseEvent *e);
          }
          @

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          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