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