设计TableWidget时,有时会向表格内添加很多按钮,每一个按钮有不同的功能。比如两个YES按钮分别启动所在行的process。所以当点击YES时,启动程序的函数需要知道启动程序的名字,这时候就想要获取点击YES的所在行,进而读取processname。当然也可以写多个启动函数分别对应不同的程序,只不过代码会多一些。
processname | start |
xiaoming | YES |
xiaohong | YES |
QPushButton *NowButton = dynamic_cast<QPushButton*>(this->sender()); //sender是获取当前的信号,是QPushButton类
if (NowButton==NULL)
{
qDebug() << "error";
}//判断是否存在
int x = NowButton->frameGeometry().x();//获得当前点击Button的地理位置坐标
int y = NowButton->frameGeometry().y();
QModelIndex index = m_pUI->tableWidget->indexAt(QPoint(x, y));
int row = index.row();
int column = index.column();//转换成tableWiget的行和列
获得当前运行程序的位置,不能依靠地理位置,可以依次判断单元格
QProcess *Now