C++ 在QTableWidget中选择QComboBox

C++ 在QTableWidget中选择QComboBox,c++,qt,qt4,qtablewidget,qcombobox,C++,Qt,Qt4,Qtablewidget,Qcombobox,QTableWidget每行中的一个单元格包含一个组合框 for (each row in table ... ) { QComboBox* combo = new QComboBox(); table->setCellWidget(row,col,combo); combo->setCurrentIndex(node.type()); connect(combo, SIGNAL(curr

QTableWidget每行中的一个单元格包含一个组合框

for (each row in table ... ) {
   QComboBox* combo = new QComboBox();      
   table->setCellWidget(row,col,combo);             
   combo->setCurrentIndex(node.type());                 
   connect(combo, SIGNAL(currentIndexChanged(int)),this, SLOT(changed(int)));
   ....
}
在处理函数::changed(int index)中

获取组合框的副本并获取新选择。
但我找不到行/列。

选择或更改嵌入项且未设置currentRow()/currentColumn()时,不会发出任何表格cellXXXX信号。

我想您应该看看QSignalMapper。这听起来像是该类的典型用例,即您有一个对象集合,其中每个对象都连接到相同的信号,但希望知道哪个对象发出了信号。

展开对Troubadour的:

以下是对文档的修改,以适应您的情况:

 QSignalMapper* signalMapper = new QSignalMapper(this);

 for (each row in table) {
     QComboBox* combo = new QComboBox();
     table->setCellWidget(row,col,combo);                         
     combo->setCurrentIndex(node.type()); 
     connect(combo, SIGNAL(currentIndexChanged(int)), signalMapper, SLOT(map()));
     signalMapper->setMapping(combo, QString("%1-%2").arg(row).arg(col));
 }

 connect(signalMapper, SIGNAL(mapped(const QString &)),
         this, SLOT(changed(const QString &)));
在处理函数::changed(QString位置)中:

请注意,QString是传递此信息的非常笨拙的方式。更好的选择是传递一个新的QModelIndex,更改后的函数将删除它


此解决方案的缺点是您丢失了currentIndexChanged发出的值,但您可以从::changed查询QComboBox的索引。

刚刚遇到同样的问题,我就是这样解决的。我使用QPoint,这是一种比QString更干净的保存x-y值的方法。希望这有帮助

classConstructor() {
    //some cool stuffs here
    tableVariationItemsSignalMapper = new QSignalMapper(this);
}

void ToolboxFrameClient::myRowAdder(QString price) {
    QLineEdit *lePrice;
    int index;
    //
    index = ui->table->rowCount();
    ui->table->insertRow(index);
    //
    lePrice = new QLineEdit(price);
    connect(lePrice, SIGNAL(editingFinished()), tableVariationItemsSignalMapper, SLOT(map()));
    tableVariationItemsSignalMapper->setMapping(lePrice, (QObject*)(new QPoint(0, index)));
    // final connector to various functions able to catch map
    connect(tableVariationItemsSignalMapper, SIGNAL(mapped(QObject*)),
             this, SLOT(on_tableVariationCellChanged(QObject*)));
}

void ToolboxFrameClient::on_tableVariationCellChanged(QObject* coords) {
    QPoint *cellPosition;
    //
    cellPosition = (QPoint*)coords;
}

不需要信号映射器。。。创建组合框后,只需向其添加两个自定义属性:

combo->setProperty("row", (int) nRow);
combo->setProperty("col", (int) nCol);
在handler函数中,您可以获得指向信号发送者(您的组合框)的指针

现在,通过询问属性,您可以收回行/列:

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

连接(信号映射器、信号(已映射(常量QString&))、此、插槽(已更改(常量QString&))@J.Chomel抱歉,我看不出你的评论与我第一个代码块的最后一行有什么不同。你能详细解释一下吗?@比尔,谢谢。。。它帮助我解决了我的问题。我认为QPoint对象存在内存泄漏,为什么要使用新的?另外,在这种情况下,您并不真正需要QPoint提供的工具,因此我使用它并没有多大好处。@ymoreau感谢您指出这一点。我已经回答了很长时间了,我不能回答你的问题。祝您愉快。如果在运行时删除了一些行,该怎么办?如何更新项目的属性?中的问题得到了答案
combo->setProperty("row", (int) nRow);
combo->setProperty("col", (int) nCol);
int nRow = sender()->property("row").toInt();
int nCol = sender()->property("col").toInt();