17

QTableWidget の各行の 1 つのセルにコンボボックスが含まれています

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 シグナルは発行されません。

4

4 に答える 4

9

トルバドールの答えを拡張する:

状況に合わせて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(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()

QString は、この情報を渡すのに非常に不器用な方法であることに注意してください。より良い選択は、あなたが渡す新しい QModelIndex であり、変更された関数はそれを削除します。

このソリューションの欠点は、currentIndexChanged が出力する値を失うことですが、::changed から QComboBox のインデックスを照会できます。

于 2009-08-26T14:00:57.520 に答える
2

QSignalMapper を見てみたいと思います。これは、そのクラスの典型的な使用例のように思えます。つまり、オブジェクトのコレクションがあり、それぞれで同じシグナルに接続しているが、どのオブジェクトがシグナルを発したかを知りたい場合です。

于 2009-08-26T05:34:58.323 に答える
-1

同じ問題が発生しましたが、これが私が解決した方法です。QString よりも xy 値を保存するためのよりクリーンな方法である QPoint を使用します。お役に立てれば。

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;
}
于 2013-10-19T20:53:35.517 に答える