In my Qt C++ GUI application I have a QDialog window, there I have a few line-edits and I am setting the display texts by function call and setText(). I have stored the values in a QStringList (the QStringList I am populating via Database Query) and setting text as follows--
void MyDialog::setDataToForm(QStringList sl)
{
ui->nameLineEdit->setText(sl[0]);
ui->emailLineEdit->setText(sl[1]);
}
Now, I have a QComboBox as well (GenderComboBox). I have set three items there - Male, Female, Other (through QT Creater Layout editor). In my QStringList sl, this value is getting saved in sl[2].
How can I set the value of sl[2] to QComboBox ???
You need to set the currentIndex of the QComboBox:
QStringList genderList;
genderList << "Male" << Female" << "Other";
ui->genderComboBox->setCurrentIndex(genderList.indexOf(sl[2]));
While this works for your example, I suggest having a look at the samples provided in the Qt documentation (Books example, SQL Widget Mapper Example) which use models to automatically populate widget contents based on SQL tables.
Related
Initially, I used qsqlrelationtablemodel in my database project. And the values were entered into the database using the combobox inside the tableView. And it looked something like this:
enter image description here
And the code looked very simple:
model->setTable(sql_query);
model->setRelation(1, QSqlRelation("my_schema.aircrafts","id_aircraft","registration_number"));
model->setRelation(3, QSqlRelation("my_schema.airports","id_airport","name_airport"));
model->setRelation(4, QSqlRelation("my_schema.airports","id_airport","name_airport"));
ui->tableView->setModel(model);
ui->tableView->setItemDelegateForColumn(1, new QSqlRelationalDelegate(this));
ui->tableView->setItemDelegateForColumn(3, new QSqlRelationalDelegate(this));
ui->tableView->setItemDelegateForColumn(4, new QSqlRelationalDelegate(this));
But now I have redone the data entry on the forms and there are no problems with transferring information to qlineedit. There are difficulties with transferring data from foreign key's to an external combobox.
Now it looks like this (the value of the combobox does not change when you click on another row of the tableView):
enter image description here
Now I'm using this code, but I don't understand how to pass the value of the selected index in the tableView to the combobox.
QString query = QString("SELECT * FROM my_schema.route WHERE id_route = '%1'").arg(id);
QSqlQuery qw;
qw.exec(query);
while(qw.next())
{
ui->lineEdit->setText(qw.value(1).toString());
ui->lineEdit_2->setText(qw.value(2).toString());
ui->lineEdit_3->setText(qw.value(3).toString());
ui->comboBox_2->setModel(model);
ui->comboBox_2->setModelColumn(4);
}
I would like to see something like this result:
enter image description here
Please tell me in the most accessible form how to achieve this
You have model and tableview for fetching all columns for clicked row by using QAbstractItemView::clicked(const QModelIndex &index) signal for receive event. And implement the slot that loops all columns in selected row like this example snippet:
// create query with join for combobox model only containing desired values not foreign keys
// after that create QStringList and fill it with query result
// set stringlistmodel to combobox
ui->comboBox_2->setModel(stringlistmodel); // this is different from tableview's model
connect(ui->tableView, &QTableView::clicked, [this](const QModelIndex &index)
{
auto row{index.row()};
ui->lineEdit->setText(model.data(model.index(row, 0).toString());
//...
ui->comboBox_2->setCurrentIndex(model.data(model.index(row, 4).toInt());
//...
});
I'm retrieving a set of results from the database and I want to populate the QComboBox with the resulting columns from the database (each row of the QComboBox should have the same columns as database result) and after that I would like to be able to retrieve from one row of the QComboBox a specific column and use it further in the app. I'm thinking if it would be possible to add the QTableView to QComboBox. I'm want to do this because I want to add more meaning to the results in a way that some result columns are just plain numbers and other are the description information.
I found out that it would be possible to concatenate the result and populate the QComboBox but this will leave me with only one value for each row to work with and I have to explode the string to obtain the exact part that it is needed to work with.
The popup that comes by default is a QListView, this can be changed with any object that inherits from QAbstractItemView, and in this case a QTableView will be used for it to use the setView() method, the result when clicking only should return a item of the selected row, then to set the column to be displayed after being selected will use the method setModelColumn() indicating the position of the column, but before that the model is set to the QComboBox using the method setModel().
# my model
model = new QSqlTableModel;
model->setTable("person");
model->select();
# setModel
comboBox->setModel(model);
# select column
comboBox->setModelColumn(1);
QTableView *view = new QTableView(this);
comboBox->setView(view);
Note: The model is set to QComboBox, not to QTableView. Also you could have problems with the width of QTableView, so we must resize, in my case use the following:
view->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
view->setMinimumWidth(500);
The complete example can be found in the following link
I am New to Qt creator .
I want to create a table View with 1 row and 2 columns using QmodelIndex.
There was an error in assigning the model to the tableView and creating data .
QTableView* const tableView
= { htca_ui->tableView,
};
tableView->setModel(&mymodel);
How to create a model and assign to the view to create the row with 2 columns using QmodelIndex?
Please Help
If you have added a tableView with the UI designer you don't need to create it again in code. Assuming your QTableView is called tableView and that htca_ui is your UI pointer, you can do
htca_ui->tableView->setModel(&mymodel);
Edit : And it occurs to me the &mymodel means you're creating it on the stack, which may not be the best idea. Better to do :
MyModel *mymodelinstance = new MyModel(this);
htca_ui->tableView->setModel(mymodelinstance);
However: the model classes can be complex to work with. If you are just after a simple table with minimum code, I would suggest using a QTableWidget rather than a QTableView. Add a QTableWidget to your UI, then you can do
htca_ui->tableWidget->setRowCount(1);
htca_ui->tableWidget->setColumnCount(2);
QTableWidgetItem *item = new QTableWidgetItem("An Item");
htca_ui->tableWidget->setItem(0,0, item); // The item at the top left will read "An Item".
See the documentation for QTableWidget for more info
I would like to have a given item in comboBox selected when opening editor, and when loading given data that should update in editor. Combobox is not working as expected!
I have these lines for having given item selected when I open the window:
normBox = new QComboBox(page1);
gridBox->addWidget(normBox, 2, 1, 1, 1);
QStringList normsLst;
normsLst.append(tr("sum"));
normsLst.append(tr("maxF"));
normsLst.append(tr("sumF"));
setComboBoxItems(normsLst, m_normBox);
m_normBox->setCurrentIndex(0);
but first item 'Sum' is not displayed in combobox when I create editor.
Then, I implemented my 'load' function, which should load the saved data in interface ending with a refresher:
normBox->setCurrentIndex(model->getNormIdx());
where model has accurate member normIdx.
How can I achieve the right selection in combo box with Qt?
Maybe you create a combobox and then set the current index of another one?
normBox = new QComboBox(page1);
...
m_normBox->setCurrentIndex(0);
And what is setComboBoxItems(normsLst, m_normBox)? I didn't find this function (with two arguments) in the Qt documentation.
This code should work:
QComboBox* combo = new QComboBox;
QStringList list;
list << "sum" << "maxF" << "sumF";
combo->addItems(list);
combo->setCurrentIndex(0);
What I would like to do is taking input from a sql database and put all these datas in a QListWidget, but I don't know how many of them there gonna be, I need also to know the id of which one was clicked when clicked.
Any ideas?
if str is the label from your sql query and n is the id then:
create your items with:
QListWidgetItem* i = new QListWidgetItem(str);
Set the id with:
i->setData(Qt::UserRole, n);
and add it to the widget:
myListWidget->addItem(i);
Then when its clicked you will get the signal
void QListWidget::itemActivated ( QListWidgetItem * item ) [signal]
connect this to a slot in your class and get the id back with
item->data(Qt::UserRole).toInt();
But this is also a good time to use QTableView and QSqlQueryModel.