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
Related
So when I am drawing my ListView items I am sorting them into sections. Each item in a section has a line as a separator from the next item. For the last item I don't want to draw this separator. How can I figure out if the current item which is drawing itself, is the last item in the section (not in the ListView!)?
Doesn't look like it is possible to figure that out. However, it might be possible to achieve the same result in a different way. Instead of each item having a line separating it from the next item have a line separating it from the previous item, have that in the section delegate as well and simply don't draw it for the item where index === 0
So apparently ListView has attached properties called section,previousSection,nextSection. With these it is pretty easy to find out if the next or previous section is the same as the current section. If it is not the same, then it means it is the last item in the section.
Each delegate item knows its own index so it can compare it against the ListView item count:
ListView {
id: theListView
model: ...
delegate: Item {
property bool isLast: index+1 < theListView.count ? false : true
...
}
}
index == 0 shows the first(actually the latest) item of the model if the model is static and not dynamic. That means that if your listView is updated with the new items every now and then, then index == 0 gets bound to different element when the a new list item arrives.
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?
I do not understand why a selection is returned by my QListView, even when the user does not select an item, no item appears as selected, and clearSelection() is called on the pane before it is displayed.
Here is the relevant code that creates the list items:
class WidgetInstanceIdentifier {...} // This class is properly registered with Qt
listpane = new QListView(...);
QStandardItemModel * model = new QStandardItemModel(listpane);
int index = 0;
std::for_each(vg_list.cbegin(), vg_list.cend(), [&](WidgetInstanceIdentifier const & vg)
{
QStandardItem * item = new QStandardItem();
std::string text = "...";
item->setText(text.c_str());
item->setEditable(false);
item->setCheckable(false);
QVariant v;
v.setValue(vg);
item->setData(v);
model->setItem( index, item );
++index;
});
model->sort(0);
listpane->setModel(model);
listpane->clearSelection();
Here is a screenshot showing the dialog at the moment I click "OK". Notice that neither item in the list pane is selected:
... And here is the relevant code that tests for the selection:
QItemSelectionModel * listpane_selectionModel = listpane->selectionModel();
QModelIndex selectedIndex = listpane_selectionModel->currentIndex();
if (!selectedIndex.isValid())
{
// This block of code is not hit!!!!!!
// I expect it would be hit
}
QVariant vg_variant = listpaneModel->item(selectedIndex.row())->data();
// The following variable is properly set to correct data representing
// the first item in the list
vg_to_use = vg_variant.value<WidgetInstanceIdentifier>();
As noted in the code, the block of code that I expect to be hit in the case of "no selection" - the if (!selectedIndex.isValid()) {...} block - is not hit. Instead, the first item in the list is returned, as though it is selected. This is not desired! The user has no way to know which item is really being selected.
What am I misunderstanding? Why does Qt seem to report that there is a valid selection, even with no item selected in the list? What is the proper way to check if there is really no item selected?
I think selected item and current item are not the same things. Although, in some cases current item can be selected too, but this is not necessarily. Usually the current item indicated by the dashed outline in the view. Thus, if you want to check selection items count do it with QItemSelectionModel::selectedIndexes() function, i.e.:
QModelIndexList selected = listpane_selectionModel->selectedIndexes();
if (!selected.isEmpty()) {
// do something
}
The default behavior of the Flex datagrid descending sort is that a selected row remains in view, meaning that the view will scroll down to show the selected row. I would like to change this so that when doing a descending sort the veiw remains at the top, with the selected row staying in the same position with a different row. I have tried different variations with this code but cant' get it to work:
var index:int = new int(myDG.selectedIndex);
var vertPos:int = myDG.grid.verticalScrollPosition;
myDG.selectedIndex = index;
myDG.grid.verticalScrollPosition = vertPos;
Thanks for your help. I am just beginning with Flex.
What you could try is the following:
Lets define newIndex as the desired index you want your datagrid to navigate to.
You could try something like this:
dgInstance.scrollToIndex(newIndex);
dgInstance.selectedIndex = newIndex;
The thing is that I didn't test the code so it might be necessary to add a:
dgInstance.validateNow();
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);