how to send data as hidden to QComboBOX in pyqt - c++

I need to send name and id to QComboBox, But i want to show name and because id is id in dB table, i want to keep as hidden id, but i don’t know how to keep as hidden id.
I can name such as:
self.comboBox_2.addItems(list1)
Bu i don't have any idea on hidden data.
My question is , how send data as hidden to QComboBox?

If you have a data model based on QAbstractItemModel which has to columns 'id' and 'name', you can do this:
enum Columns
{
COL_ID,
COL_NAME
}
QComboBox *combo = new QComboBox;
combo->setModel(model);
combo->setModelColumn(COL_NAME)
If you want to add items to a combobox one by one, you do this:
QComboBox *combo = new QComboBox;
combo->addItem(name, id);
Then you can retrieve id value from combobox item using:
QString id = combo->itemData(index, Qt::UserRole).toString();

Related

Transfer a list from a foreign key to an external combobox

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

How to get one record from sqlite and display in tableview

What I want to do is take the input from a text field (i.e cust_name), match and fetch the record from database with column cust_name.
Database has table cust and column cust_name, address. I want to find a way to display the data in tablview of QSqlTableModel.
QSqlTableModel *model = new QSqlTableModel;
model->setTable("cust");
model->setEditStrategy(QSqlTableModel::OnManualSubmit);
ui->tableView->setModel(model);
ui->tableView->setColumnHidden(0,1);
ui->tableView->setColumnHidden(5,1);
ui->list->setModel(model);
ui->list->setModelColumn(1);
model->select();
This successfully displays the whole table named cust. I want to be able to change it into displaying the single record matching the search term.
Use function index() to access custom row/column.
QSqlTableModel *model = new QSqlTableModel;
int value = model->index(row,column).data().toInt();
In your situation like this:
int value = model->index((model->rowCount()-1),column).data().toInt();
or custom sql query :
SELECT * FROM tablename ORDER BY id DESC LIMIT 1;

Add QTableView to QComboBox

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

How to identify QStandardItem's in a QListView without using the text field?

I'm trying to identify objects in my QListView item model. I'm currently using the text field of the list view which is obviously not the best idea.
This is my minimal code to fill the widget, see how I save a timestamp to the text field:
QListView * view = new QListView(this);
QStandardItemModel * model = new QStandardItemModel(this);
MyElements * mylist = getElements();
while (!mylist->isFirst())
{
QStandardItem * item = new QStandardItem(mylist->getIcon(), mylist->getTimeString());
model->appendRow(item);
mylist = mylist->getPrevious();
}
view->setModel(model);
As you can see I have a unique timestamp for each item storing the unix time in milliseconds as a string. I can use this to identify my objects and this way I can always be sure which item is clicked.
Now, I wish to override the text with a more userfriendly string, like "Click this to do stuff" and not displaying the timestamp anymore.
But I still need the timestamp to identify each unique item in the list. Any idea where else I could store this data?
You can use the QStandardItem::setData() function to set any kind of data to the item. For example:
QStandardItem * item = new QStandardItem(list->getIcon(), "some text");
item->setData(list->getTimeString());
// ...
In order to get this data you will need to:
QString d = item->data().toString();
You can use custom roles to store multiple data sets in an item using Qt::UserRole.

Insert Items dynamically in a QListWidget

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.