C++ 在 QTableWidget 中选择 QComboBox

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1332110/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 19:35:02  来源:igfitidea点击:

Selecting QComboBox in QTableWidget

c++qtqt4qtablewidgetqcombobox

提问by

One cell in each row of a QTableWidget contains a combobox

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)));
   ....
}

In the handler function ::changed(int index) I have

在处理函数 ::changed(int index) 我有

QComboBox* combo=(QComboBox*)table->cellWidget(_row,_col);  
combo->currentIndex()

To get back a copy of the combobox and get the new selection.
But I can't get the row/col.
None of the table cellXXXX signals is emitted when an embedded item is selected or changed and currentRow()/currentColumn() aren't set.

取回组合框的副本并获得新的选择。
但我无法获得行/列。
None of the table cellXXXX signals is emitted when an embedded item is selected or changed and currentRow()/currentColumn() aren't set.

回答by Richy

No need for the signal mapper... When the combobox is created you can simply add two custom properties to it:

不需要信号映射器...创建组合框后,您可以简单地向其添加两个自定义属性:

combo->setProperty("row", (int) nRow);
combo->setProperty("col", (int) nCol);

In the handler function you can get a pointer back to the sender of the signal (your combobox).

在处理程序函数中,您可以获得一个指向信号发送者(您的组合框)的指针。

Now by asking for the properties you can have your row/col back:

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

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

回答by Bill

Expanding on Troubadour's answer:

扩展 Troubadour 的回答

Here's a modification of the QSignalMapperdocumentation to fit your situation:

这是QSignalMapper文档的修改以适合您的情况:

 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 &)));

In the handler function ::changed(QString position):

在处理函数 ::changed(QString position) 中:

 QStringList coordinates = position.split("-");
 int row = coordinates[0].toInt();
 int col = coordinates[1].toInt();
 QComboBox* combo=(QComboBox*)table->cellWidget(row, col);  
 combo->currentIndex()

Note that a QString is a pretty clumsy way to pass this information. A better choice would be a new QModelIndex that you pass, and which the changed function would then delete.

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

The downside to this solution is that you lose the value that currentIndexChanged emits, but you can query the QComboBox for its index from ::changed.

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

回答by Troubadour

I think you want to take a look at QSignalMapper. This sounds like a typical use case for that class i.e. you have a collection of objects where you hook up to the same signal on each but would like to know which object emitted the signal.

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

回答by Daniele Vrut

Just got same problem and this is how I solved. I use QPoint that is a cleaner way to save a x-y value than a QString. Hope this helps.

刚刚遇到同样的问题,这就是我解决的方法。我使用 QPoint,这是一种比 QString 更简洁的保存 xy 值的方法。希望这可以帮助。

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;
}