Rakesh K
Rakesh K

Reputation: 8515

Handling single click and double click separately in QTableWidget

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?

Upvotes: 8

Views: 15623

Answers (1)

Blood
Blood

Reputation: 4196

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 */
}

Upvotes: 10

Related Questions