QTableWidget: How can I get the row and column index when user click on a particular cell using single mouse click?
-
I have created a TableWidget inside i have tale 4 row and 5 column. Now, I want to select particular row and column place where i can apply some another event. Here my peace of code for creating dynamically QTableWidget at proper place.
QTableWidget *twidget = new QTableWidget(this); twidget->setStyleSheet( "QTableWidget{" "background-color: lightgray;" "}"); twidget->setGeometry(114, 70, 200, 98); twidget->clear(); twidget->verticalHeader()->hide(); twidget->horizontalHeader()->hide(); twidget->setRowCount(4); twidget->setColumnCount(5); for (auto r = 0; r < 4; r++) { for (auto c = 0; c < 5; c++) { QTableWidgetItem *item = new QTableWidgetItem(QString::number(r + 1) + "x" + QString::number(c + 1)); item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled); twidget->setItem(r, c, item); //twidget->setItem(r, c, new QTableWidgetItem(QString::number(r+1) + "x" + QString::number(c+1))); twidget->resizeRowsToContents(); twidget->resizeColumnsToContents(); } } twidget->show();
Now I want to get the index of place wehere i can apply signal and slot to processing on the place.
Thanku in advance
-
http://doc.qt.io/qt-5/qabstractitemview.html#clicked
connect(twidget,&QAbstractItemView::clicked,this,[](const QModelIndex& idx)->void{ qDebug() << "Clicked: " << idx.row()<<","<<idx.column(); });
P.S.
twidget->setGeometry(114, 70, 200, 98);
not a fan of this, any reason why you are not using a layout?
-
http://doc.qt.io/qt-5/qabstractitemview.html#clicked
connect(twidget,&QAbstractItemView::clicked,this,[](const QModelIndex& idx)->void{ qDebug() << "Clicked: " << idx.row()<<","<<idx.column(); });
P.S.
twidget->setGeometry(114, 70, 200, 98);
not a fan of this, any reason why you are not using a layout?
@VRonin I want to create one slot method where i can get row and column . inside the slop function i am using signal and slot to fix the proper method.
Also i am used this one connect signal but this one not working.connect(twidget, SIGNAL(cellClicked(int, int)), this, SLOT(previousWeek(int, int)));
-
If you are using Qt5 you should move to the New connect syntax.
change
previousWeek
from a slot that takes 2int
s to 1 that takes 1const QModelIndex&
then useconnect(twidget, SIGNAL(clicked(QModelIndex)), this, SLOT(previousWeek(QModelIndex)));
.you can then use
QModelIndex::row()
andQModelIndex::column()
to find out the coodinates of the clicked item -
If you are using Qt5 you should move to the New connect syntax.
change
previousWeek
from a slot that takes 2int
s to 1 that takes 1const QModelIndex&
then useconnect(twidget, SIGNAL(clicked(QModelIndex)), this, SLOT(previousWeek(QModelIndex)));
.you can then use
QModelIndex::row()
andQModelIndex::column()
to find out the coodinates of the clicked item@VRonin said in QTableWidget: How can I get the row and column index when user click on a particular cell using single mouse click?:
connect(twidget, SIGNAL(clicked(QModelIndex)), this, SLOT(previousWeek(QModelIndex)));.
When i do this one in constructure then i will getting exception
"Exception thrown at 0x00007FFAEB2F352C (Qt5Cored.dll) in RadSpaEngine.exe: 0xC0000005: Access violation reading location 0xFFFFFFFFFFFFFFFF."