在 QTableWidget 中选择 QComboBox

发布于 2024-08-02 20:39:57 字数 611 浏览 13 评论 0原文

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) 中我必须

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

取回组合框的副本并获取新的选择。
但我无法获取行/列。
当选择或更改嵌入项并且未设置 currentRow()/currentColumn() 时,不会发出任何表格 cellXXXX 信号。

One cell in each row of a QTableWidget contains a combobox

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

舟遥客 2024-08-09 20:39:57

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

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

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

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

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

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();
断念 2024-08-09 20:39:57

扩展 Troubadour 的答案

这是对 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 &)));

在处理程序函数::changed(QStringposition)中:

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

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

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

Expanding on Troubadour's answer:

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

 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):

 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.

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.

南烟 2024-08-09 20:39:57

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

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.

不离久伴 2024-08-09 20:39:57

刚刚遇到同样的问题,这就是我解决的方法。我使用 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;
}

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.

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文