Can't get item from QTreeView by QModelIndex - c++

I create a QTreeView in a window, and I want to get the text of selected items when double clicking them. I try to use the signal "doubleClicked(const QModelIndex &)" to get the index of selected item.
But when I received the signal and wanted to do something with the index passed in, I can't get the item properly.
I found the index passed in is something like that:
root
|...item1 (0, 0)
|...|...subItem1 (0, 0)
|...|...subitem2 (1, 0)
|...item2 (1, 0)
There are two (0, 0) and (1, 0) items???
EDIT: I got this result by
qDebug(QString::number(index.row()).toLatin1().data()); // row
qDebug(QString::number(index.column()).toLatin1().data()); // column
Here is my code, create QTreeView and QStandardItemModel:
mTree = new QTreeView(this); // mTree is a class member
treeModel = new QStandardItemModel(); // also a class member
proxymodel = new MySortFilterProxyModel(); // for sorting
proxymodel->setSourceModel(treeModel);
mTree->setModel(proxymodel);
and a custom slot to receive the signal:
private slots:
void getSelectedIP(const QModelIndex &);
connect signal and slot:
connect(mTree, SIGNAL(doubleClicked(const QModelIndex &)),
this, SLOT(getSelectedIP(const QModelIndex &)));
implementation of the slot, and the program crashed in this code:
void HostTreeFrame::getSelectedIP(const QModelIndex &index)
{
QStandardItem *selectedItem = treeModel->itemFromIndex(index);
qDebug(QString::number(index.row()).toLatin1().data());
qDebug(QString::number(index.column()).toLatin1().data());
qDebug("1");
QString selectedIPString = selectedItem->text(); // program crashed here, selectedItem == nullptr
qDebug("2");
}
EDIT:
The selectedItem is nullptr, that's why the program crashed, but why it is nullptr?

Consider the code...
void HostTreeFrame::getSelectedIP(const QModelIndex &index)
{
QStandardItem *selectedItem = treeModel->itemFromIndex(index);
The problem is that index is associated with the model being used by the view but that's the proxy model -- not the QStandardItemModel.
You need to map the model index index to the correct model. So something like...
void HostTreeFrame::getSelectedIP(const QModelIndex &index)
{
auto standard_item_model_index = proxymodel->mapToSource(index);
QStandardItem *selectedItem = treeModel->itemFromIndex(standard_item_model_index);
/*
* Check selectedItem before dereferencing.
*/
if (selectedItem) {
...
The code above assumes proxymodel is a member of (or is directly visible to) HostTreeFrame.

Related

How to get selected items from a QListView which uses custom QAbstractListModel in C++

I have created a custom list model following this guide Creating a cusotm model for a QListView. I am able to show a list of custom objects (such as the Employee as in the example) but I don't know how to retrieve back the selected ones (can I retrieve back the "linked" objects directly?).
Maybe do I have to do something with this command:
myLV->selectionModel()->selectedIndexes();
But I don't really know how to retrieve back the original custom objects.
[EDIT]
So far I have solved retrieving back the object adding a custom method inside my custom list model:
Employee* MyEmployeeListModel::getAtSelectedIndex(const QModelIndex& index){
return employees_.at(index.row());
}
And then calling this on the main window:
QModelIndexList selectedRows;
QItemSelectionModel * selmodel = ui->employeesLV->selectionModel();
selectedRows = selmodel->selectedRows();
MyEmployeeListModel* currModel = dynamic_cast <MyEmployeeListModel*>(ui->employeesLV->model());
for (const QModelIndex & index : selectedRows){
Employee* item=currModel->getAtSelectedIndex(index);
if (item) {
// do something with the item
}
}
Now what I am willing to know is if this is the real best practice or not.
I'm using the following code with a QTreeView (ui->treeMessages), but this should work with a QListView as well:
QModelIndexList selectedRows;
QItemSelectionModel * selmodel = ui->treeMessages->selectionModel();
selectedRows = selmodel->selectedRows();
for (const QModelIndex & index : selectedRows)
{
const QModelIndex sourceIndex = m_sortFilterModel->mapToSource(index);
ItemData * item = sourceIndex.internalPointer();
if (item) {
// do something
}
}

C++/QML: ListView is not updated on dataChanged signal from QAbstractListModel

I am trying to write a QML Gui for a large dynamic C/Fortran simulation. The data I want to display is stored in Fortran Common blocks and updated on fixed time steps. My problem is that QML ListView does not refresh when the dataChanged signal is emitted after each time step, although the signal is received by the Gui (test is in the code below).
I am probably missing out something really obvious because when I flick my ListView down and up again, the displayed data is updated and correct (I guess because the QML engine re-renders elements when they get "out of sight" and back in again). So the only thing that does not work is that the ListView gets updated every time the dataChanged signal is received and not only when it is re-rendered. Below is a more detailed description of my approach and the relevant code parts.
Each simulation entity has several attributes (alive, position...), so I decided to create a ListModel containing a DataObject for each entity. This is the corresponding header file (the actual simulation data is declared as extern structs in "interface.h", so I can access it via pointer):
"acdata.h"
#include <QtCore>
#include <QObject>
#include <QtGui>
extern "C" {
#include "interface.h"
}
class AcDataObject : public QObject
{
Q_OBJECT
public:
explicit AcDataObject(int id_, int *pac_live, double *pac_pos_x, QObject *parent = 0) :
QObject(parent)
{
entity_id = id_;
ac_live = pac_live;
ac_pos_x = pac_pos_x;
}
int entity_id;
int *ac_live;
double *ac_pos_x;
};
class AcDataModel : public QAbstractListModel
{
Q_OBJECT
public:
enum RoleNames {
IdRole = Qt::UserRole,
LiveRole = Qt::UserRole + 1,
PosXRole = Qt::UserRole + 2
};
explicit AcDataModel(QObject *parent = 0);
virtual int rowCount(const QModelIndex &parent) const;
virtual QVariant data(const QModelIndex &index, int role) const;
Q_INVOKABLE Qt::ItemFlags flags(const QModelIndex &index) const Q_DECL_OVERRIDE;
void do_update();
protected:
virtual QHash<int, QByteArray> roleNames() const;
private:
QList<AcDataObject*> data_list;
QHash<int, QByteArray> m_roleNames;
QModelIndex start_index;
QModelIndex end_index;
signals:
void dataChanged(const QModelIndex &start_index, const QModelIndex &end_index);
};
Like the header, the .cpp file is also adapted from what you can find in the Qt5 Cadaques Book here, except that my constructor iterates over all simulation entities to set the pointers. Additionally, there is the do_update function that emits the dataChanged signal for the whole list.
"acdata.cpp"
#include "acdata.h"
AcDataModel::AcDataModel(QObject *parent) :
QAbstractListModel(parent)
{
m_roleNames[IdRole] = "entity_id";
m_roleNames[LiveRole] = "ac_live";
m_roleNames[PosXRole] = "ac_pos_x";
for (int i = 0; i < MAX_ENTITIES; i++) // MAX_ENTITIES is defined in interface.h
{
AcDataObject *data_object = new AcDataObject( i,
&fdata_ac_.ac_live[i], // fdata_ac_ is the C struct/Fortran common block defined in interface.h
&fdata_ac_.ac_pos_x[i] );
data_list.append(data_object);
}
}
int AcDataModel::rowCount(const QModelIndex &parent) const {
Q_UNUSED(parent);
return data_list.count();
}
QVariant AcDataModel::data(const QModelIndex &index, int role) const
{
int row = index.row();
if(row < 0 || row >= data_list.count()) {
return QVariant();
}
const AcDataObject *data_object = data_list.at(row);
switch(role) {
case IdRole: return data_object->entity_id;
case LiveRole: return *(data_object->ac_live);
case PosXRole: return *(data_object->ac_pos_x);
}
return QVariant();
}
QHash<int, QByteArray> AcDataModel::roleNames() const
{
return m_roleNames;
}
void AcDataModel::do_update() {
start_index = createIndex(0, 0);
end_index = createIndex((data_list.count() - 1), 0);
dataChanged(start_index, end_index);
}
Qt::ItemFlags AcDataModel::flags(const QModelIndex &index) const
{
if (!index.isValid()) {return 0;}
return Qt::ItemIsEditable | QAbstractItemModel::flags(index);
}
When the simulation is running, do_update() is called every second. I have created a test Gui with a ListView and exposed my model to it with:
Excerpt from "threadcontrol.cpp"
acdata = new AcDataModel();
viewer = new QtQuick2ApplicationViewer();
viewer->rootContext()->setContextProperty("acdata", acdata);
viewer->setMainQmlFile(QStringLiteral("../lib/qml_gui/main.qml"));
viewer->showExpanded();
(This code is part of a larger file that controls the different threads. I am quite sure the rest is not relevant to the actual problem and this question is getting really long...)
So finally there is main.qml. It contains a list with MAX_ENTITIES elements and each elements holds text fields to display my data. I have also added a Connections element to check if the dataChanged signal is received by the Gui.
"main.qml"
ListView {
id: listviewer
model: acdata
delegate: Rectangle {
/* ... some formatting stuff like height etc ... */
Row {
anchors.fill: parent
Text {
/* ... formatting stuff ... */
text: model.entity_id
}
Text {
/* ... formatting stuff ... */
text: model.ac_live
}
Text {
/* ... formatting stuff ... */
text: model.ac_pos_x
}
}
}
Connections {
target: listviewer.model // EDIT: I drew the wrong conclusions here, see text below!
onDataChanged: {
console.log("DataChanged received")
}
}
}
When running the simulation, the "DataChanged received" message is printed every second.
Edit: I was connecting to the ListModel and not to the ListView here, although the ListView has to receive the dataChanged signal. As the console log does not work when connecting to listviewer, I am probably missing the connection between listView and dataChanged signal. However, I think this should work automatically when implementing the dataChanged signal?
Additional information: I have found a similar problem here with Qt Map and it actually seemed to be a bug that was fixed in Qt 5.6. However, running qmake with Qt 5.7 did not fix my problem.
You mustn't declare the dataChanged() signal in your class, because you want to emit the signal AbstractItemModel::dataChanged(). If you re-declare it you add a comleptely new and different Signal that is not connected anywhere. If you remove the declaration in acdata.h everything should work fine.

Hierarchial QTreeView mapping with QStackedWidget

I am creating a UI with QTreeView representing list of items(which may or may not have children) and displaying corresponding widget on right side using QStackedWidget. Previously the behaviour of my data or the list of items mentioned above was flat, hence I was using QListView but when the data had to have children I replaced the QListView with QTreeView so I can add children as well. I have achieved the above in the following manner:
for(std::vector<AWidget* >::iterator it=widgets.begin(); it!=widgets.end(); ++it)
{
AWidget* w = *it;
QStandardItem *parent = new QStandardItem(w->Name());
model->setItem(i, 0, parent);
QWidget *PageWidget = w->getWidget();
QScrollArea * pScrollArea = new QScrollArea();
pScrollArea->setWidget(PageWidget);
m_ui.AStackedWidget->addWidget(pScrollArea);
if(!w->hasChildren())
{
std::vector<AWidget*> children = w->getChildren();
for(std::vector<AWidget*>::iterator iChild=children.begin(); iChild!=children.end(); ++iChild)
{
AWidget* childWidget = *iChild;
QStandardItem *child = new QStandardItem(childWidget->Name());
parent->appendRow(child);
QWidget *childPageWidget = childWidget->getWidget();
QScrollArea * pChildScrollArea = new QScrollArea();
pChildScrollArea->setWidget(childPageWidget);
pChildScrollArea->setWidgetResizable(true);
m_ui.AStackedWidget->addWidget(pChildScrollArea);
}
}
i++;
}
m_ui.ATreeView->setEditTriggers(QAbstractItemView::NoEditTriggers);
m_ui.ATreeView->setTabKeyNavigation(true);
m_ui.ATreeView->setModel(model);
QModelIndex index = m_ui.ATreeView->model()->index(0,0);
QItemSelectionModel * selModel = m_ui.ATreeView->selectionModel();
if(selModel)
{
connect(selModel, SIGNAL( currentChanged(const QModelIndex &, const QModelIndex &) ), this , SLOT(slotOptionSelectedForChangeUI(const QModelIndex & )) );
selModel->select(index,QItemSelectionModel::Select);
}
slotOptionSelectedForChangeUI(index);
Definition of slotOptionSelectedForChangeUI() is as follows:
void slotOptionSelectedForChangeUI(const QModelIndex & indx)
{
int rowNum = indx.row();
if(m_ui.AStackedWidget)
m_ui.AStackedWidget->setCurrentIndex(rowNum);
}
For eg: following is the view:
-A -> widget1
-B -> widget2
-C -> widget3
-D -> widget4
-E -> widget5
-E1 -> widget6
-E2 -> widget7
-E3 -> widget8
-E4 -> widget9
-E5 -> widget10
-E6 -> widget11
- E7 -> widget12
A,B,C,D,E show correct corresponding widgets on the right side in the QStackedWidget. However E1, E2, E3, E4,E5,E6,E7 show widgets widget1,2,3,4,5,6,7 respectively. This means index of E1 starts again from 0 and stacked widget shows widget0 for index 0 and so on. How should E1-E7 be mapped with widget6-12 so that proper widgets are displayed on the right side stacked widget?
The proper way of doing this is by not using a QStackedWidget, but only displaying one. To do this, all you have to do is:
Add the widget to the item using QStandardItem::setData
In the ui, instead of a QStackedWidget, just use a simple QWidget (with a horizontal/vertical layout without margins)
Connect the selection model index changed signal to a slot that removes all children from the simple widget, and adds the one stored with the item. You can retrieve it by using the QStandardItem::data function (or the models data function)
Here a minimal example of those steps:
#define MyRole Qt::UserRole + 42
//...
//adding the item
AWidget* w = *it;
QStandardItem *parent = new QStandardItem(w->Name());
parent->setData(QVariant::fromValue(w), MyRole);
model->setItem(i, 0, parent);
//...
//the index changed slot
void slotOptionSelectedForChangeUI(const QModelIndex & indx)
{
AWidget *w = model->data(index, MyRole).value<AWidget*>();//will internally call QStandardItem::data
//remove old children
QList<QWidget*> children = m_ui.containerWidget->findChildren<QWidget*>(QString(), Qt::FindDirectChildrenOnly);
foreach(QWidget *child, children)
child->deleteLater();
//add the new one
QWidget *PageWidget = w->getWidget();
ScrollArea * pScrollArea = new QScrollArea(m_ui.containerWidget);
pScrollArea->setWidget(PageWidget);
m_ui.containerWidget->layout()->addWidget(pScrollArea);
}

Qt checkboxes in QTableView

I'm using this code to query sqlite and put the results in a QTableView.
//MainWindow.cpp
void MainWindow::on_pushButton_clicked()
{
QSqlQueryModel * modal=new QSqlQueryModel();
connOpen();
QSqlQuery* qry=new QSqlQuery(mydb);
qry->prepare("select * from database");
qry->exec();
modal->setQuery(*qry);
//from stack
modal->insertColumn(0);
ui->tableView->setModel(modal);
//from stack
ui->tableView->resizeColumnsToContents();
int p;
for(p=0; p<modal->rowCount(); p++)
{
ui->tableView->setIndexWidget(modal->index(p,0),new QCheckBox());
}
connClose();
qDebug() <<(modal->rowCount());
}
I've seen several examples of the web for adding checkboxes to a column, but I'm not quite sure what to use for my simple example.
This answer suggests a few lines that doesn't seem standard.
There are more examples like this and this one that appear to outline what I need, but it's unclear to where you place the code.
What I intend to do is to have column 1 checkable. On next btn press, If checked those rows of data get written to a file.
I still need to understand how to loop thru the selected data, or perhaps I need to get the ids of the checked rows and do another query.
Questions:
How do you add 1 column of editable checkboxes to QTableView?
How do you loop through values in the QTableView data, so values of the checked rows can be accessed?
How do you check all/none?
I think the best way to have a column of checkable cells is to create your item model, e.g. by subclassing the QSqlQueryModel.
You must reimplement the flags() method to make checkable the cells.
Also you need to reimplement the data() method to return the check state and the setData() method and to set the check state. You must implement your own logic to keep track of the check state of every rows (e.g. using an array of Qt::CheckState that you must initialize and resize when the model data changes).
Yuo can start with something like this:
class MyModel : public QSqlQueryModel
{
public:
Qt::ItemFlags flags(const QModelIndex & index) const
{
if(index.column() == 0)
return QSqlQueryModel::flags(index) | Qt::ItemIsUserCheckable;
return QSqlQueryModel::flags(index);
}
QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const
{
if(index.column() == 0 && role == Qt::CheckStateRole)
{
//implement your logic to return the check state
//....
}
else
return QSqlQueryModel::data(index, role);
}
bool setData(const QModelIndex & index, const QVariant & value, int role = Qt::EditRole)
{
if(index.column() == 0 && role == Qt::CheckStateRole)
{
//implement your logic to set the check state
//....
}
else
QSqlQueryModel::setData(index, value, role);
}
};
Se also:
Model Subclassing
QAbstractItemModel documentation

Changing data in a QTableView depending on the selection of a QComboBox

I have a QComboBox in one of the columns of a QTableView. How can I change the other columns depending on what I selected in the ComboBox? I am using the QComboBox as a delegate.
There are at least 2 approaches.
Use natural for Qt's model itemChanged signal.
emit signal from your delegate and catch it inside your main window.
If your delegate is standard which means that inside setModelData() method you have something like:
QComboBox *line = static_cast<QComboBox*>(editor);
QString data = line->currentText();
//...
model->setData(index, data);
then I think you should use just natural way. For example:
connect(model,&QStandardItemModel::itemChanged,[=](QStandardItem * item) {
if(item->column() == NEEDED_COLUMN)
{
//you found, just get data and use it as you want
qDebug() << item->text();
}
});
I used here C++11 (CONFIG += c++11 to .pro file) and new syntax of signals and slots, but of course you can use old syntax if you want.
I already reproduced your code(delegate with combobox) and my solution works if I select something in combobox and confirm that by enter clicking for example. But if you want to get solution where data will be changed automatically, when you select another item in combobox(without pressing enter) then see next case:
Create special signal onside delegate:
signals:
void boxDataChanged(const QString & str);
Create connection inside createEditor() method:
QWidget *ItemDelegate::createEditor(QWidget *parent,
const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
QComboBox *editor = new QComboBox(parent);
connect(editor,SIGNAL(currentIndexChanged(QString)),this,SIGNAL(boxDataChanged(QString)));
return editor;
}
And use it!
ItemDelegate *del = new ItemDelegate;
ui->tableView->setItemDelegate( del);
ui->tableView->setModel(model);
connect(del,&ItemDelegate::boxDataChanged,[=](const QString & str) {
//you found, just get data and use it as you want
qDebug() << str;
});