PDA

View Full Version : Default Item View classes and returning data from model



xerionn
15th January 2009, 08:05
Hello,
I am trying really hard to get some data from item view classes.I have tried to do it with QTreeView or QTreeWidget unsuccesfully so i need your help ! WIth QtreeView i know it has to do something with selectionmodel() and Qtreewidget has selectedItems().

I want to setup this 2 dimensional table at which the user can select a row. After the selection i want to get this selection and read lets say the 5th column which contains an ID that i need.

Things is i have totally failed to do that !
Any idea ?
I am open to other implementations if you think that can be done differently ! Even though i would like to have horizontal headers !

Thank you.

caduel
15th January 2009, 08:47
see model-view-programming; we will gladly answer specific questions you have

xerionn
16th January 2009, 00:03
Ok here it is. Maybe this can be more specific... Last is the part that i have problem and focuses over to the part where i have to move the index i get to the correct column


#include <QWidget>
#include <QtGui>
#include "CameraDataBase.h"

class QAbstractItemModel;
class QCheckBox;
class QComboBox;
class QGroupBox;
class QLabel;
class QLineEdit;
class QSortFilterProxyModel;
class QTreeView;

class cameraDataBaseModel : public QWidget
{
Q_OBJECT

public:
cameraDataBaseModel(QWidget *parent = 0,CCameraDataBase * cameraDataBase=0);
~cameraDataBaseModel();

void setSourceModel(QAbstractItemModel *model);
QAbstractItemModel * setupModel();
void addCamera(QAbstractItemModel *model, const QString &cameraModel,
const double &FocalLength, const int &ImageWidth,
const int &ImageHeight, const int &cameraID);

private slots:
void filterRegExpChanged();
void filterColumnChanged();
void sortChanged();
void accept();

private:
QSortFilterProxyModel *cameraModel;

QTreeView *cameraView;
QCheckBox *filterCaseSensitivityCheckBox;
QCheckBox *sortCaseSensitivityCheckBox;
QLabel *filterPatternLabel;
QLabel *filterSyntaxLabel;
QLabel *filterColumnLabel;
QLineEdit *filterPatternLineEdit;
QComboBox *filterSyntaxComboBox;
QDialogButtonBox *buttonBox;

CCameraDataBase * camDataBase;
QComboBox *filterColumnComboBox;
QStandardItemModel *model;

};

#endif // CAMERADATABASEMODEL_H


#include "CameraDataBaseModel.h"
#include <QtGui>

cameraDataBaseModel::cameraDataBaseModel(QWidget *parent,CCameraDataBase * cameraDataBase)
: QWidget(parent)
{
this->camDataBase = cameraDataBase;

this->cameraModel = new QSortFilterProxyModel;
this->cameraModel->setDynamicSortFilter(true);

this->cameraView = new QTreeView();
this->cameraView->setRootIsDecorated(false);
this->cameraView->setAlternatingRowColors(true);
this->cameraView->setModel(cameraModel);
this->cameraView->setSortingEnabled(true);

this->setSourceModel(setupModel());
this->cameraView->hideColumn(4);

this->sortCaseSensitivityCheckBox = new QCheckBox(tr("Case sensitive sorting"));
this->filterCaseSensitivityCheckBox = new QCheckBox(tr("Case sensitive filter"));

this->filterPatternLineEdit = new QLineEdit;
this->filterPatternLabel = new QLabel(tr("&Filter pattern:"));
this->filterPatternLabel->setBuddy(filterPatternLineEdit);

this->filterSyntaxComboBox = new QComboBox;
this->filterSyntaxComboBox->addItem(tr("Regular expression"), QRegExp::RegExp);
this->filterSyntaxComboBox->addItem(tr("Wildcard"), QRegExp::Wildcard);
this->filterSyntaxComboBox->addItem(tr("Fixed string"), QRegExp::FixedString);
this->filterSyntaxLabel = new QLabel(tr("Filter &syntax:"));
this->filterSyntaxLabel->setBuddy(filterSyntaxComboBox);

this->filterColumnComboBox = new QComboBox;
this->filterColumnComboBox->addItem(tr("Camera Model"));
this->filterColumnComboBox->addItem(tr("Focal Length"));
this->filterColumnComboBox->addItem(tr("Image Width"));
this->filterColumnComboBox->addItem(tr("Image Height"));
this->filterColumnLabel = new QLabel(tr("Filter &column:"));
this->filterColumnLabel->setBuddy(filterColumnComboBox);

this->buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok
| QDialogButtonBox::Cancel);

connect(filterPatternLineEdit, SIGNAL(textChanged(const QString &)),
this, SLOT(filterRegExpChanged()));
connect(filterSyntaxComboBox, SIGNAL(currentIndexChanged(int)),
this, SLOT(filterRegExpChanged()));
connect(filterColumnComboBox, SIGNAL(currentIndexChanged(int)),
this, SLOT(filterColumnChanged()));
connect(filterCaseSensitivityCheckBox, SIGNAL(toggled(bool)),
this, SLOT(filterRegExpChanged()));
connect(sortCaseSensitivityCheckBox, SIGNAL(toggled(bool)),
this, SLOT(sortChanged()));

connect(this->buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
connect(this->buttonBox, SIGNAL(rejected()), this, SLOT(close()));

QGridLayout *cameraLayout = new QGridLayout;
cameraLayout->addWidget(cameraView, 0, 0, 1, 3);
cameraLayout->addWidget(filterPatternLabel, 1, 0);
cameraLayout->addWidget(filterPatternLineEdit, 1, 1, 1, 2);
cameraLayout->addWidget(filterSyntaxLabel, 2, 0);
cameraLayout->addWidget(filterSyntaxComboBox, 2, 1, 1, 2);
cameraLayout->addWidget(filterColumnLabel, 3, 0);
cameraLayout->addWidget(filterColumnComboBox, 3, 1, 1, 2);
cameraLayout->addWidget(filterCaseSensitivityCheckBox, 4, 0, 1, 2);
cameraLayout->addWidget(sortCaseSensitivityCheckBox, 4, 2);
cameraLayout->addWidget(buttonBox,5,2);
setLayout(cameraLayout);

setWindowTitle(tr("Select Camera"));
resize(450, 400);

this->cameraView->sortByColumn(2, Qt::AscendingOrder);
this->filterColumnComboBox->setCurrentIndex(0);

this->filterPatternLineEdit->setText("");
this->filterCaseSensitivityCheckBox->setChecked(false);
this->sortCaseSensitivityCheckBox->setChecked(false);
}

cameraDataBaseModel::~cameraDataBaseModel()
{

}

void cameraDataBaseModel::setSourceModel(QAbstractItemM odel *model)
{
cameraModel->setSourceModel(model);
}

void cameraDataBaseModel::filterRegExpChanged()
{
QRegExp::PatternSyntax syntax =
QRegExp::PatternSyntax(filterSyntaxComboBox->itemData(
filterSyntaxComboBox->currentIndex()).toInt());
Qt::CaseSensitivity caseSensitivity =
filterCaseSensitivityCheckBox->isChecked() ? Qt::CaseSensitive
: Qt::CaseInsensitive;

QRegExp regExp(filterPatternLineEdit->text(), caseSensitivity, syntax);
cameraModel->setFilterRegExp(regExp);
}

void cameraDataBaseModel::filterColumnChanged()
{
cameraModel->setFilterKeyColumn(filterColumnComboBox->currentIndex());
}

void cameraDataBaseModel::sortChanged()
{
cameraModel->setSortCaseSensitivity(
sortCaseSensitivityCheckBox->isChecked() ? Qt::CaseSensitive
: Qt::CaseInsensitive);
}


void cameraDataBaseModel::addCamera(QAbstractItemModel *model, const QString &cameraModel,
const double &FocalLength,const int &ImageWidth, const int &ImageHeight, const int &cameraID)
{
model->insertRow(0);
model->setData(model->index(0, 0), cameraModel);
model->setData(model->index(0, 1), FocalLength);
model->setData(model->index(0, 2), ImageWidth);
model->setData(model->index(0, 3), ImageHeight);
model->setData(model->index(0, 4), cameraID);
}


QAbstractItemModel * cameraDataBaseModel::setupModel()
{
model = new QStandardItemModel(0, 5);

model->setHeaderData(0, Qt::Horizontal, QObject::tr("Camera Model"));
model->setHeaderData(1, Qt::Horizontal, QObject::tr("Focal Length"));
model->setHeaderData(2, Qt::Horizontal, QObject::tr("Image Width"));
model->setHeaderData(3, Qt::Horizontal, QObject::tr("Image Height"));
model->setHeaderData(4, Qt::Horizontal, QObject::tr("Camera ID"));

for (int i =0; i < this->camDataBase->globalCameras.GetSize(); i++)
{
addCamera( model, QString::fromUtf8(this->camDataBase->globalCameras.GetAt(i)->getMakeAndModel()),
this->camDataBase->globalCameras.GetAt(i)->getFocalLength(),
this->camDataBase->globalCameras.GetAt(i)->getImageWidth(),
this->camDataBase->globalCameras.GetAt(i)->getImageHeight(), i );
}

return model;
}

void cameraDataBaseModel::accept()
{
//this where i want to grab the selection of the user and find the Camera ID which is the 4th colum of the model
}


Here is What i have tried



// What i have tried is getting the currentindex() which actually works like a charm
// from what is see while debugging as i get the row and column select
QModelIndex curIndex = cameraView->currentIndex() ;

// Then get the row
int numRows = curIndex.row();
//move to the column but over here i get an invalid ModelIndex cause i see in debugging r=-1 c=-1
QModelIndex index = model->index(numRows, 4, curIndex);

//then get the data
QVariant blabla = index.data (Qt::DisplayRole)

Everything works but the part of moving to the column i want.
So definately there is something wrong over here and i am trying things but cant sort it out :S

Any idea?


PS. Yes i know that this is 99% the same with the example that Trolltech provides about sorting etc...!

jpn
16th January 2009, 16:03
From what I understood, you want to get the sibling, not a child of "curIndex". It's easiest to do with QModelIndex::sibling():

QModelIndex index = curIndex.sibling(numRows, 4);
Alternatively you can do similar than you tried, but with correct parent:

QModelIndex index = model->index(numRows, 4, curIndex.parent());

xerionn
18th January 2009, 23:21
Awesome , this was exactly what i wanted ;)
The sibling thingie makes things much easier but its good to know how to sort it out the way i was trying so i know my mistake :)

Thank you

xerionn
19th January 2009, 05:29
Is there any way to set some of the colums of my model to not be editable ?
Also my second column contains double variables. When you click to it you get a QDoubleSpinBox but it only allows 2 decimals ! How can i change that to lets say 5 ?

wysota
19th January 2009, 08:33
Is there any way to set some of the colums of my model to not be editable ?
Yes, reimplement QAbstractItemModel::flags() for your model and make sure ItemIsEditable is not returned for indexes from those columns.


Also my second column contains double variables. When you click to it you get a QDoubleSpinBox but it only allows 2 decimals ! How can i change that to lets say 5 ?

Reimplement either QAbstractItemDelegate::createEditor() or QAbstractItemDelegate::setEditorData().

jwieland
22nd April 2009, 21:15
Yes, reimplement QAbstractItemModel::flags() for your model and make sure ItemIsEditable is not returned for indexes from those columns.



Reimplement either QAbstractItemDelegate::createEditor() or QAbstractItemDelegate::setEditorData().

Hi Wysota,

Sorry to bug you, but I've been trying to use a QtreeView with checkboxes enclosed for each item with not much progress. I followed some thread in the forum and reimplement the flags method in my model class to include ItemIsUserCheckable. Now I can see the checkboxes appear next to each item in the tree. However, I cannot check or uncheck the checkboxes at all. I see some people advice that I should implement the setData() method in my model class but I have no clue on how to do that. Could you shed some light here?

Thanks a lot

wysota
23rd April 2009, 19:50
What is the base class of your model?