16

Actually I am new to Qt and unable to match up QMouseEvent with QTableview

please help in solving this issue.

AAEM
  • 1,837
  • 2
  • 18
  • 26
ShivaPrasad Gadapa
  • 193
  • 1
  • 2
  • 10

1 Answers1

23

Here is an example of how you can get a table cell's text when clicking on it.

Suppose a QTableView defined in some MyClass class. You need to connect the clicked signal to your own MyClass::onTableClicked() slot, as shown below:

connect(tableView, SIGNAL(clicked(const QModelIndex &)), this, SLOT(onTableClicked(const QModelIndex &)));

Slot implementation:

void MyClass::onTableClicked(const QModelIndex &index)
{
    if (index.isValid()) {
        QString cellText = index.data().toString();        
    }
}

You can use also doubleClicked, pressed or other signals depending on your goal.

ForeverLearning
  • 413
  • 5
  • 25
vahancho
  • 20,808
  • 3
  • 47
  • 55
  • The above example is works fine thank you for providing the solution – ShivaPrasad Gadapa Oct 21 '13 at 05:51
  • @vahancho can you please tell me what's the difference between clicked(const QModelIndex &) and cellClicked(int row, int column) ? Why can't there be a single API? – jxgn Oct 19 '16 at 12:27
  • 2
    @XavierGeoffrey, the difference is that these signals declared in different classes: `QAbstractItemView::clicked()` and `QTableWidget::cellClicked()` respectively. First signal can be used in all item view classes such as treeview, tableview, listview, while the second one only for table widgets. Also arguments are different: sometimes it's much convenient to pass a model index than row and column, especially for treeviews, where there is a parent too. – vahancho Oct 19 '16 at 14:15
  • @vahancho it does not work for me i have used the same signal, but the slot did not execut – AAEM May 11 '18 at 19:26
  • ahaa, i have known what was wrong with it, the pointer i use m_table, was now allocated yet. I mean that i have used the connect statement before the allocation of the table. So it works fine now – AAEM May 11 '18 at 19:31