There's easy way to achieve this.
Just use one of the signals of QTableWidget - cellChanged(int,int);
QObject::connect(m_pTableWidget, SIGNAL(cellChanged(int,int)),
this, SLOT(OnTableWidgetCellChanged(int,int)));
Inside slot function (OnTableWidgetCellChanged(int row,int column)), use a switch case with column to validate each column seperately.
Get the input text from user of that particular column, and store as QString.
Now convert that QString to int by passing the bool variable and check the status of bool variable. If its false, then user entered is not a valid number. example is shown as below :
bool bRetValue = false;
QString strValue = m_pTableWidget->item(row, VALUE_INDEX)->text();
int nValue = strValue.toInt(&bRetValue);
if(bRetValue == false)
{
// show dialog to user to enter only numbers
}
If you want to convert to double or other type, simply convert the value to qvariant and can follow same procedure.
QVariant qvarValue(strValue);
double dVal = qvarValue.toDouble(&bRetValue);
if(bRetValue == false)
{
// show dialog to user to enter only numbers
}
If you want to be more specific you can use QRegExp or QRegularExpression(Qt5)
QRegularExpression regexp("enter regx here",
QRegularExpression::CaseInsensitiveOption);
QRegularExpressionMatch regxmatch = regexp.match(strValue);
if (regxmatch.hasMatch())
{
// your functionality
}
else
{
// show dialog to user to enter only numbers
}