
Originally Posted by
robgeek
When i click on the table nothing happens. I believe this is happening because i didn't call mousePressedEvent anywhere
first of all you can n't see table, because you are creating table on stack. create it on heap (TestEvt* eve = new TestEvt (this)) or have it as class member.

Originally Posted by
robgeek
but i don't know where to call it.
You don't need to cal this explicitly, when you click on table mousePressEvent will be called automatically.
changes I can suggest you is:
public:
if(event->button( ) == Qt::LeftButton)
qDebug() << "Left Button Clicked.";
QTableWidget::mousePressEvent(event
);
// call base class implementation }
};
class TestEvt : public QTableWidget {
public:
TestEvt(QWidget* parent ):QTableWidget(parent) {}
virtual void mousePressEvent(QMouseEvent *event) {
if(event->button( ) == Qt::LeftButton)
qDebug() << "Left Button Clicked.";
QTableWidget::mousePressEvent(event); // call base class implementation
}
};
To copy to clipboard, switch view to plain text mode
MainWindow
::MainWindow(QWidget *parent
) : ui(new Ui::MainWindow)
{
ui->setupUi(this);
TestEvt* eve = new TestEvt(this); //create instance on heap
}
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
TestEvt* eve = new TestEvt(this); //create instance on heap
}
To copy to clipboard, switch view to plain text mode
Note: you may not see table widget fully here, please use layout in order to fit table widget in you main window.
Added after 41 minutes:

Originally Posted by
d_stranz
I would use the mousePressEvent / mouseReleaseEvent as a pair, for the following reason: If the user presses the mouse in your table widget, it automatically triggers your action. This means there is no way for the user to say, oops, I didn't mean to do that - I clicked accidently. In most other mouse click scenarios, the action isn't triggered unless the mouse press and mouse release both occur in the same window. Mouse press sets a flag, mouse release does the action if and only if the flag is set. This avoids things like clicking in the window and then moving out of it to release (a common way to "escape" out of doing something you didn't intend) or clicking outside the window and moving into it to release. If you only watched for one or the other event, your app would almost certainly have unintended consequences where something is performed that the user didn't want.
Be careful using right mouse press events if you also use context menus.
informative, Thanks a lot.
Bookmarks