Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling single click and double click separately in QTableWidget

Tags:

qt

I have a QTableWidget widget in my application. I have to handle both single-click and double click mouse events separately in my application. Right now, only single-click slot is being called even when I double click on a cell. How do I handle them separately?

The following is the code for the signal-slot connection:

connect(ui.tableWidget, SIGNAL(cellClicked(int, int)), this, SLOT(myCellClicked(int, int)));
connect(ui.tableWidget, SIGNAL(itemDoubleClicked(QTableWidgetItem*)), this, SLOT(tableItemClicked(QTableWidgetItem*)));

Am I missing any other configuration here?

like image 774
Rakesh K Avatar asked Jul 16 '12 11:07

Rakesh K


1 Answers

Okey, now i see. I had similar problem few weeks ago. The problem is in your QTableWidgetItem. I don't know exactly how does it work, but sometimes you can miss your item and click on cell. That's how you can fix it. Connect it this way:

connect(ui.tableWidget, SIGNAL(cellClicked(int, int)), this, SLOT(myCellClicked(int, int)));
connect(ui.tableWidget, SIGNAL(cellDoubleClicked(int,int)), this, SLOT(tableItemClicked(int,int)));

And in your tableItemClicked slot do it this way:

void MyWidget::tableItemClicked(int row, int column)
{
    QTableWidgetItem *item = new QTableWidgetItem;
    item = myTable->item(row,column)
    /* do some stuff with item */
}
like image 135
Blood Avatar answered Oct 15 '22 18:10

Blood