QStandardItemModel append new items on top of the rest - c++

i like to make my QStandardItemModel that populates items in qtreeview
to append rows on top the allready defined items in the view .
something like the twitter view , new items first.
all i see in the QStandardItemModel is the appendRow/s that appends then to button.
this is what im using now.
SWidget *widget = new SWidget;
QStandardItem *newItem = new QStandardItem;
newItem->setSizeHint( widget->size() );
appendRow( newItem );
view->setIndexWidget( newItem->index(), widget );

void QStandardItemModel::insertRow ( int row, QStandardItem * item ) inserts a row at row containing item. So instead of calling appendRow(newItem); call insertRow(0, newItem);

Related

How to select item in QComboBox with QTreeView

I was trying to select item "leaf2" from QComboBox with QTreeView in the below code.
I just want to select items with no child from text by code. (if there is a child, it won't be selectable)
How can I select item or index with no child item?
Can anyone help me with this problem?
Thank you.
void Main::initView()
{
QStandardItemModel *model = new QStandardItemModel;
QStandardItem *root_item = model->invisibleRootItem();
// Build Model Items
QStandardItem *node_item = NULL;
node_item = new QStandardItem("Node");
node_item->setFlags(Qt::ItemIsEnabled);
root_item->appendRow(node_item);
QStandardItem *leaf_item = new QStandardItem("leaf1");
leaf_item ->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
node_item->appendRow(leaf_item );
leaf_item = new QStandardItem("leaf2");
leaf_item ->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
node_item->appendRow(leaf_item );
// Set Model to TreeViewComboBox
ui.cb_treevew->setModel(model);
ui.cb_treeview->setCurrentIndex(0); // "Node" is selected.
ui.cb_treeview->setCurrentIndex(1); // Nothing is selected.
ui.cb_treeview->setCurrentIndex(2); // Nothing is selected.
ui.cb_treeview->setCurrentIndex(3); // Nothing is selected.
}
Here is my code for CTreeViewComboBox.
CTreeViewComboBox::CTreeViewComboBox(QWidget *parent) : QComboBox(parent)
{
QTreeView* treeView = new QTreeView(this);
treeView->setEditTriggers(QTreeView::NoEditTriggers);
treeView->setSelectionBehavior(QTreeView::SelectRows);
treeView->setSelectionMode(QTreeView::SingleSelection);
treeView->setItemsExpandable(true);
treeView->header()->setVisible(false);
treeView->setWordWrap(true);
setView(treeView);
}
PS: I tried to select items as following, but not working. :(
ui.cb_treeview->treeView()->setCurrentIndex(getModelIndexFromText("leaf2"));
If the nodes with children will never have the same text with the nodes without children then the following method is appropriate.
QModelIndexList modelIndexes = model->match(
model->index(0, 0),
Qt::DisplayRole,
"leaf2",
-1,
Qt::MatchRecursive);
QModelIndex index = modelIndexes.first();
ui.cb_treeview.setRootModelIndex(index.parent());
ui.cb_treeview.setCurrentIndex(index.row());
On the other hand, if the nodes with children can have the same text as the nodes without children, you should use the following method.
QModelIndexList modelIndexes = model->match(
model->index(0, 0),
Qt::DisplayRole,
"leaf2",
-1,
Qt::MatchRecursive);
QModelIndexList::iterator tstIt = std::find_if(modelIndexes.begin(),
modelIndexes.end(),
[] (const QModelIndex & index) {
return !index.model()->hasChildren(index);
});
ui.cb_treeview.setRootModelIndex(tstIt->parent());
ui.cb_treeview.setCurrentIndex(tstIt->row());
In both cases I am assuming that nodes without children always have different texts. If nodes without children match the name, choose one of them.

Add items to columns in QStandardItemModel

I am currently adding rows to my QTableView as such
QStandardItem* itm;
QStandardItemModel* model = new QStandardItemModel(this);
model->setColumnCount(2);
model->appendRow(new QStandardItem("Some Text in Column1");
How do I add items to column 2 dynamically by appending?
In the above example column 2 is empty. How do I add item to column 2?
Calling appendRow(QStandardItem *) only adds a single item to the first column. You would need to pass in a QList to appendRow() to add items to each column, e.g.:
QList<QStandardItem *> items;
items.append(new QStandardItem("Column 1 Text"));
items.append(new QStandardItem("Column 2 Text"));
QStandardItemModel* model = new QStandardItemModel(this);
model->setColumnCount(2);
model->appendRow(items);
See http://doc.qt.io/qt-5/qstandarditemmodel.html#appendRow for more detail.

C++ QListView with icons And Signals on items clicked

I have a QListview where I have set a model. The model contains the QList. Now I want to get the contents of QListview on clicking the item. But I don't know how to do this? Through some tutorials I followed I am able to get two items appear in the QListview as follows. But I dont know how to make it work?? Please anyone help. The code which I am working as follows.
listviewmodel =new QListView;
listviewmodel->setModel( createModel() );
listviewmodel->setViewMode(QListView::IconMode);
listviewmodel->setIconSize(QSize(size().width()/8, size().height()/8));
connect(listviewmodel,SIGNAL(clicked(const QModelIndex)),this,SLOT(ItemClicked(QModelIndex)));
QAbstractItemModel *MainWindow::createModel()
{
QStandardItemModel *model = new QStandardItemModel();
QList<QStandardItem *> listItem;
QStandardItem *item2 = new QStandardItem();
item2->setIcon(QIcon(QPixmap::fromImage(qimages2)));
listItem << item2;
QStandardItem *item1 = new QStandardItem();
item1->setIcon(QIcon(QPixmap::fromImage(qimages1)));
listItem << item1;
model->appendColumn(listItem);
return model;
}
void MainWindow::ItemClicked (QModelIndex index )
{
textEdit->setText(index.data().toString());
}
I checked your code in QtCreator and it works as you described. So what is the problem? Are you getting some errors? Maybe did you not put declaration of ItemClicked(QModelIndex) below the public slots:? Are you sure that connect returns true? Try to check it:
bool success = connect(listviewmodel,SIGNAL(clicked(const QModelIndex)),this,SLOT(ItemClicked(QModelIndex)));
Q_ASSERT(success);
EDIT: If you want to display only icons at QListView and get some text informations after clicking on specified item, you can do it in the following way:
item2->setIcon(QIcon(QPixmap::fromImage(qimages2)));
item2->setData("informations about item2", Qt::UserRole);
(...)
void MainWindow::ItemClicked (QModelIndex index )
{
QString data = index.data(Qt::UserRole).value<QString>();
ui->textEdit->setText(data);
}

Qt: set columns in treeView

How can I implement this code I have for a qTreeWidget for a qTreeView?
for (const auto & i : names) {
QTreeWidgetItem * item = new QTreeWidgetItem(ui->treeWidget);
item->setText(0, QString::fromStdString(i));
ui->treeWidget->addTopLevelItem(item);
const std::unordered_map<std::string, double> map = m_reader.getMapFromEntry(i);
for (const auto & j : map) {
QTreeWidgetItem * item2 = new QTreeWidgetItem();
item2->setText(0,QString::fromStdString(j.first));
item2->setText(1,QString::number(j.second));
item->addChild(item2);
}
}
I have a model and a treeView, like this:
m_model = new QStandardItemModel(m_reader.getAllNames().size(),2,this);
ui->treeView->setModel(m_model);
I tried this, but that only shows one column:
QStandardItem * parentItem = m_model->invisibleRootItem();
for (const auto & i : names) {
QStandardItem * item = new QStandardItem(QString::fromStdString(i));
parentItem->appendRow(item);
const std::unordered_map<std::string, double> map = m_reader.getMapFromEntry(i);
for (const auto & j : map) {
QList<QStandardItem *> rowItems;
rowItems << new QStandardItem(QString::fromStdString(j.first));
rowItems << new QStandardItem(QString::number(j.second));
item->appendRow(rowItems);
}
}
With the treeWidget, I had so set the columnCount, like this:
ui->treeWidget->setColumnCount(2);
But treeView does not have a method like this.
So, to summarize: How can I implement a TreeView with more than one column?
EDIT:
To clarify, I want something like this:
|-A
| |-B-C
| |-D-E
where A is the parent and B,C,D,E the children, with B,D being in column 0 and C,E in column 1.
Hope this helps!
To support multiple columns, the model must contain data for multiple columns.
So in some sense, columns are a property of the model, not the view. Views then can decide to hide or rearrange certain columns (For example, a QListView always only shows the first column, while one can hide or reorder columns in a QTableView).
As you use QStandardItemModel, its documentation should give a few hints how to create multiple columns.
E.g., look at this example from the documentation:
QStandardItemModel model(4, 4);
for (int row = 0; row < 4; ++row) {
for (int column = 0; column < 4; ++column) {
QStandardItem *item = new QStandardItem(QString("row %0, column %1").arg(row).arg(column));
model.setItem(row, column, item);
}
}
It creates a model with 4 initial rows and columns each, and then fills it with items via setItem().
Alternatively, you can pass a list of items to QStandardItemModel::appendRow(), with an item for each column:
QList<QStandardItem*> items;
items.append(new QStandardItem(tr("One"));
items.append(new QStandardItem(tr("Two"));
model->appendRow(items);
This adds a new row with "One' in the first column and "Two" in the second. For even more ways to deal with multiple columns, see the QStandardItemModel docs.
Note: QTreeView expects the same number of columns on all levels of the hierarchy, so one should fill rows with empty items for the unused columns if need be.
Just an addition to answer by Frank Osterfeld:
QTreeView displays all columns of subtables inserted into top level QStandardItems. You just have to "force" it to show additional columns by inserting dummy QStandardItems into top-level table. Example:
QStandardItemModel *objectTreeModel = new QStandardItemModel(NULL);
QStandardItem *mainItem = new QStandardItem(tr("Main Item"));
QStandardItem *subItem1 = new QStandardItem(tr("Sub-Item 1"));
QStandardItem *subItem2 = new QStandardItem(tr("Sub-Item 2"));
mainItem->appendRow(QList<QStandardItem *>() << subItem1 << subItem2);
QStandardItem *dummyItem = new QStandardItem();
objectTreeModel->appendRow(QList<QStandardItem *>() << mainItem << dummyItem );
Now you will be able to see 2 columns and if you expand mainItem, both subitems will be visible.

Can’t make child in Qtreeview using QStandardItemModel

After reading some examples I am still missing here something.
I have Qtreeview for view and QStandardItemModel for the data interface, also using QSortFilterProxyModel subclass but I don't know if its relevant.
This is my logic:
First I create the model with the QWidget as the parent:
QStandardItemModel m_model = new QStandardItemModel(0,4,parent);
then setSourceModel(m_model) for the widget
Set the treeview with QSortFilterProxyModel. something like this:
GroupProxyModel = new GroupSortFilterProxyModel;
GroupProxyModel->setDynamicSortFilter(true);
setSourceModel(createSubjectModel(parent));
ui.treeView_mainwindow->setModel(GroupProxyModel);
ui.treeView_mainwindow->setSortingEnabled(true);
Then later I fill the first row like this:
QList<QStandardItem *> items;
items.insert(0,new QStandardItem("Test 0"));
items.at(0)->setEditable(false);
m_model->insertRow(0,items);
Until now every thing working fine and I see the row with the data. But when I like to
add child to the row like this:
QModelIndex parentQModelIndex = m_model->item(0,0)->index();
m_model->insertRows(0,1,parentQModelIndex);
m_model->insertColumns(0,1,parentQModelIndex);
QModelIndex indexB = m_model->index(0, 0, parentQModelIndex);
m_model->setData(indexB,"Child test",Qt::DisplayRole);
But I don't see the child, why?
That's not how QStandardItemModel works - to add a child, call appendRow(s)/insertRow(s) on the parent QStandardItem:
QStandardItem* child = new QStandardItem( tr("Child test") );
...
QStandardItem* parentItem = m_model->item( 0, 0 );
parentItem->appendRow( child );