How to set selected item in QComboBox with QtreeView - qt

i have the following code for QComboBox with WtreeView set as combo view
this->db->select("SELECT top 10 company, address, phone, id FROM data");
QTreeView *ptv = new QTreeView(this);
ptv->setModel(this->db->model);
ptv->setColumnHidden(3, true);
ui->comboBox->setModel(this->db->model);
ui->comboBox->setView(ptv);
connect(ui->comboBox, SIGNAL(activated(int)), this, SLOT(getComboIndex(int)));
How can i set selected item or index for column 2 for example. I can set the first column with
ui->comboBox->setCurrentIndex(index);
but this is not working for other column only for the first.

Try setting the model column to the one you want to change:
ui->comboBox->setModelColumn(2);
ui->comboBox->setCurrentIndex(index);

Related

How to update values in QTableView

I have a QTableView which gets its data from QAbstractTableModel and is a 2d array. Below the TableView I have two comboboxes. User first selects the row in the TableView. Now I want that when the user selects an option from the combobox, I want the selected option to update the display in the TableView selection.
Shown below is the UI. For example if the User Selects REF DES in the class combo box I want the selected row to update its class name to REF DES.
I am using c++ qt5. How do I achieve this with signals and slots?
enter image description here
Currently I have a slot that updates the combobox values based on my selection in tableview. I use something like this.
void GetOptionsClass::slotSelectionChange(const QItemSelection&, const QItemSelection&)
{
int rowidx = ui->line_tableView->selectionModel()->currentIndex().row();
data = model->index(rowidx, 2).data().toString();
if (data == "BOARD GEOMETRY") {
ui->lineSubclass->clear();
ui->lineSubclass->addItems(board_geometry_opts);
ui->lineClass->setCurrentIndex(0);
}
However I want to reverse this. Meaning when I change combobox I want to update the qtableview.

Disable (grey-out) some rows in a QTreeView

I have a (very simple yet) QTreeView showing some rows:
m_cameraModel = new QStandardItemModel(this);
QSortFilterProxyModel* cameraProxyModel = new QSortFilterProxyModel(this);
cameraProxyModel->setSourceModel(m_cameraModel);
ui.CameraTreeView->setModel(cameraProxyModel);
m_cameraModel->appendRow(new QStandardItem("Panavision"));
m_cameraModel->appendRow(new QStandardItem("Panaflex"));
Here I want to disable the first row "Panavision" so that it is still visible but can't be selected any more and is somehow greyed-out so that the user can see this entry is not active.
May be this is some kind of beginner-question, but how can this be done?
Thanks!
I would try to do that in the following way:
// Get item that corresponds to the first row
QStandardItem *item = m_cameraModel->item(0, 0);
// Disable the item.
item->setFlags(Qt::NoItemFlags);
You'd want to use the QItemDelegate class, which allows you to disable the row you want to amongst other things. There's a good question here on StackOverflow that shows how to do a very basic example: How to set a delegate for a single cell in Qt item view?

Empty combo box when editable is true

I want to auto select the first element in a combo box:
final ComboBox selectStatus = new ComboBox();
selectStatus.getItems().addAll(
"Active",
"Blocked",
"Suspended"
);
selectStatus.getSelectionModel().select(0);
selectStatus.setEditable(true);
But when I add editable=true the combo box is empty. Can I solve this problem somehow?
Do like this:
//first set it editable
selectStatus.setEditable(true);
//then, set the value of the first item
selectStatus.getSelectionModel().select(0);
When you set it editable the values displayed is cleared, so you have to set the value after set it editable.
See the javadocs.

Getting a signal to a QListView when its populated by QComboBoxes

I'm working on a UI that reads a database and updates it back when items in it are changed. My UI consists of a QListView, and it's populated by QComboBoxes. Now, I can get a signal when a combo box item is changed (though I can't get the index of the widget item that was changed), and I need to let the parent list view know that a member widget's value had changed. Any ideas on how I can get this to work?
A simple hack for obtaining the widget index is to code the widget index into the item data of each combo box item. You could set a QString as itemData which codes a reference to your standard item data and the widget index, e.g.
pComboBox1->setItemText(1, "Item 1");
pComboBox1->setItemData(1, "1-1");
pComboBox1->setItemData(2, "Item 2");
pComboBox1->setItemData(2, "2-1"); // Item 2 in 1st combo box
pComboBox2->setItemText(1, "Item 1");
pComboBox2->setItemData(1, "1-2"); // Item 1 in 2nd combo box
// and so on

How to show an item with given index number in QComboBox?

I have add some items in QComboBox and now I am trying to show the item on QComboBox with given index.
For example,
In my QComboBox I have three items; firstItem, secondItem, thirdItem when I got an index number two, I want to see secondItem shown on QComboBox.
I hope I was clear while asking the question.
Thank you all
You have to set the current index (setCurrentIndex).
ui->combo->setCurrentIndex(2);
You can set QComboBox to display any item as you choose based on index using setCurrentIndex() method. As an example:
....
artistView = new QComboBox;
...
index = <some input>;
...
artistView->setCurrentIndex(index);///sets the index

Resources