Reputation:
I want to enable mouseTracking in a QTableWidget using Qt4.7. Usually this should be straight forward with setMouseTracking(true). This worked on other widgets, but QTableWidget resists it. Is there anything special I have to deal with in order to activate mouseTracking?
Although i'm actually doing more complex stuff i could gain confidence that its not enabled by printing out QMouseEvent.x() in mouseMoveEvent. thanks so far :)
Here is what I tried:
QGridLayout * layout = new QGridLayout(this);
SCTableWidget * aTable = new SCTableWidget(tableRows, tableCols, this);
QTableWidgetItem * newItem;
for(int i = 0; i<(3); i++) {
for(int j = 0; j<(17); j++) {
newItem = new QTableWidgetItem(QString::number(tableData[i][j]));
aTable->setItem(i,j, newItem);
}
}
// First try
aTable->setMouseTracking(true);
//Second try. Main Window further down.
MainWindow::instance->enableMouseTracking(aTable->children());
MainWindow::instance->enableMouseTracking(aTable->viewport()->children());
// Third try
aTable->viewport()->setMouseTracking(true);
layout->addWidget(aTable, 0, 0, 1, 2);
The second try contains a method that is implemented in MainWindow which looks like this:
void
MainWindow::enableMouseTracking(const QObjectList & pChildren)
{
foreach(QObject * obj, pChildren)
{
QWidget * w = qobject_cast<QWidget *>(obj);
if(w)
{
w->setMouseTracking(true);
enableMouseTracking(w->children());
}
}
}
Upvotes: 3
Views: 5651
Reputation: 509
This worked for me:
aTable->setMouseTracking(true);
aTable->viewport()->setMouseTracking(true);
Upvotes: 0
Reputation: 21
By default, the mouse events are disabled because QAbstractItemView
implements the mousePressEvent
, mouseReleaseEvent
, etc functions, and QTableWidget
doesn't implement them at all, this means the mouse messages are blocked by the functions in QAbstractItemView
.
So one very easy way to resolve this issue is to subclass the QTableWidget
class, and provide the mouse signals by yourself, it should be very easy, just try it!
Upvotes: 2
Reputation: 20492
switching on mouse tracking for QTableWidget->viewport() usually works. Are you also catching mouse events for the QTableWidget->viewport() widget? Pls check if an example below would work fine for you:
test.h:
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
bool eventFilter(QObject *obj, QEvent *event);
private:
Ui::MainWindow *ui;
QTableWidget *table;
};
test.cpp:
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
table = new QTableWidget(5, 10, this);
table->setGeometry(10, 20, 300, 200);
for (int row=0; row<5; ++row)
for (int column=0; column<10; ++column)
table->setItem(row, column, new QTableWidgetItem(tr("%1").arg((row+1)*(column+1))));
table->setMouseTracking(true);
table->viewport()->setMouseTracking(true);
table->installEventFilter(this);
table->viewport()->installEventFilter(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
bool MainWindow::eventFilter(QObject *obj, QEvent *event)
{
if (obj == table)
{
if (event->type() == QEvent::MouseButtonPress)
qDebug() << "table mouse press event";
else if (event->type() == QEvent::MouseMove)
qDebug() << "table mouse moveevent";
}
else if (obj == table->viewport())
{
if (event->type() == QEvent::MouseButtonPress)
qDebug() << "table->viewport mouse press event";
else if (event->type() == QEvent::MouseMove)
qDebug() << "table->viewport mouse moveevent";
}
return QMainWindow::eventFilter(obj, event);
}
hope this helps, regards
Upvotes: 3