单击时如何获取放置在Qtablewidget单元格中的小部件的行号? [英] How to get the row number of widget placed in a cell of Qtablewidget when it get clicked?

查看:739
本文介绍了单击时如何获取放置在Qtablewidget单元格中的小部件的行号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试的是在用户选择项目时获取QcomboBox的行号.尽管使用

What i'm trying is to get the row number of QcomboBox when user selects items. Although its easy to to get the cell column and row using

cellClicked(int,int)

信号,但仅在单元格上没有小部件时才起作用.

signal, but it only works when there is no widget on the cell.

因此,如果单元格中放置了小部件,如何获取行号.

so how to get the row number in case if there is a widget placed in a cell.

注意:所有组合框都是动态添加的

Note: All the combobox are added dynamically

推荐答案

最后,我发现了两种解决方法.

At-last i found 2 ways of doing it.

  1. 通过设置QComboBox的属性
  2. 使用QSignalMapper

第一种方法

QComboBox* mCombo = new QCombobox();
mComboBox->setProperty("row",(int) i); // i represents the row number in qtablewidget

在处理函数中,用于处理单击的QComboBox

In handler function where you are handling the clicked QComboBox

int row = sender()->property("row").toInt();

第二种方法

QSignalMapper *signalMapper= new QSignalMapper(this);   //Create a signal mapper instance 

for (each row in table) {
     QComboBox* mCombo = new QComboBox();
     table->setCellWidget(row,col,combo);                          
     connect(mCombo, SIGNAL(currentIndexChanged(int)), signalMapper, SLOT(map()));  

/*connect each signal of QComboBox to signal Mapper slot (i.e map()) which in turns connected to the signal of signalMapper calling the SLOT associated with it (i.e rowFinder) */         

signalMapper->setMapping(combo, (int)row);  //assign mapping to each widgetusing set mapping


}

connect(signalMapper, SIGNAL(mapped(int)),
         this, SLOT(rowFinder(int)));

功能:rowFinder(int rowIndex)

function : rowFinder(int rowIndex)

int row = rowIndex; //here is the row indexof selected QComboBox

这篇关于单击时如何获取放置在Qtablewidget单元格中的小部件的行号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆