The connection of the drop-down list with the slot is certainly made in the designer. This conclusion can be made at least by the characteristic name of the slot. Accordingly, the relationship of the change in the current index in the list is established before the list itself receives the data set.
What's happening?
Model is created:
QSqlTableModel *comboModel = model->relationModel(10);
The model connects to the list widget. This automatically activates sending a signal about a change in the index of the current row:
ui->workComboBox->setModel(comboModel);
Since at the time of sending the signal, the modelColumn list property is set to 0 by default, the value contained in the cell of the first row and the first column is sent to the connected slot (numbering, of course, is done from scratch).
Finally, the model column shown in the list is set:
ui->workComboBox->setModelColumn(comboModel->fieldIndex("name"));
Here, a signal about changing the current index of the row is not sent.
How to win?
If you do not want to connect the signal to the slot after filling the list with data from the model, you can do so.
QSqlTableModel *comboModel = model->relationModel(10); ui->workComboBox->blockSignals(true); ui->workComboBox->setModel(comboModel); ui->workComboBox->setModelColumn(comboModel->fieldIndex("name")); ui->workComboBox->blockSignals(false); ui->workComboBox->setCurrentIndex(0);
What is wrong with the slot?
The slot accepts a string as an argument, which is then converted to a database query filter. What happens if the table stores the same string values, but under different identifiers?
For example, if a table contains names, then two "Ivan Ivanovitch" is not such a rare case. If the table contains the names of settlements, then the situation is even more complete coincidence.
It should also be remembered that string comparison is always slower than comparison of numbers, and if the table contains a large number of records, then performance is probably quite significant. However, this detail is a special case.
How else?
You can use the index of the current line, not its value. Suppose there is a table of workers ' workers :
worker_id - INTEGER UNSIGNED PRIMARY KEY AUTO_INCREMENT name - TEXT
Then the slot can be rewritten to something else:
void MainWindow::on_workComboBox_currentIndexChanged(int row) { QSqlTableModel *model = qobject_cast<QSqlTableModel*>(ui ->workComboBox->model()); if(model == Q_NULLPTR) return; if(row >= 0) { // ΠΠ°ΠΆΠ½ΠΎ ΠΏΠΎΠ½ΠΈΠΌΠ°ΡΡ, ΡΡΠΎ ΠΈΠ½Π΄Π΅ΠΊΡ ΡΡΡΠΎΠΊΠΈ ΠΈ ΠΈΠ΄Π΅Π½ΡΠΈΡΠΈΠΊΠ°ΡΠΎΡ - // ΡΡΠΎ Π½Π΅ ΠΎΠ΄Π½ΠΎ ΠΈ ΡΠΎΠΆΠ΅, Π° Π·Π½Π°ΡΠΈΡ ΠΈΠ· ΠΏΠ΅ΡΠ²ΠΎΠ³ΠΎ // Π½Π΅ΠΎΠ±Ρ
ΠΎΠ΄ΠΈΠΌΠΎ ΠΏΠΎΠ»ΡΡΠΈΡΡ Π²ΡΠΎΡΠΎΠ΅. const int worker_id_col = model->fieldIndex("worker_id"); const int name_col = model->fieldIndex("name"); const int worker_id = model->data(model->index(row,worker_id_col)).toInt(); const QString name = model->data(model->index(row,name_col)).toString(); qDebug() << "worker selected = " << name; model->setFilter(QString("worker_id = %1").arg(worker_id)); } else model->setFilter(QString()); model->select(); }
ui->workComboBox->setModel(comboModel);and only then connect hiscurrentIndexChanged(const QString&)signal to youron_workComboBox_currentIndexChanged(const QString &arg1)sloton_workComboBox_currentIndexChanged(const QString &arg1). - aleks.andr