Results 1 to 9 of 9

Thread: Default Item View classes and returning data from model

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #3
    Join Date
    Nov 2008
    Posts
    18
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Default Item View classes and returning data from model

    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

    Qt Code:
    1. #include <QWidget>
    2. #include <QtGui>
    3. #include "CameraDataBase.h"
    4.  
    5. class QCheckBox;
    6. class QComboBox;
    7. class QGroupBox;
    8. class QLabel;
    9. class QLineEdit;
    10. class QTreeView;
    11.  
    12. class cameraDataBaseModel : public QWidget
    13. {
    14. Q_OBJECT
    15.  
    16. public:
    17. cameraDataBaseModel(QWidget *parent = 0,CCameraDataBase * cameraDataBase=0);
    18. ~cameraDataBaseModel();
    19.  
    20. void setSourceModel(QAbstractItemModel *model);
    21. QAbstractItemModel * setupModel();
    22. void addCamera(QAbstractItemModel *model, const QString &cameraModel,
    23. const double &FocalLength, const int &ImageWidth,
    24. const int &ImageHeight, const int &cameraID);
    25.  
    26. private slots:
    27. void filterRegExpChanged();
    28. void filterColumnChanged();
    29. void sortChanged();
    30. void accept();
    31.  
    32. private:
    33. QSortFilterProxyModel *cameraModel;
    34.  
    35. QTreeView *cameraView;
    36. QCheckBox *filterCaseSensitivityCheckBox;
    37. QCheckBox *sortCaseSensitivityCheckBox;
    38. QLabel *filterPatternLabel;
    39. QLabel *filterSyntaxLabel;
    40. QLabel *filterColumnLabel;
    41. QLineEdit *filterPatternLineEdit;
    42. QComboBox *filterSyntaxComboBox;
    43. QDialogButtonBox *buttonBox;
    44.  
    45. CCameraDataBase * camDataBase;
    46. QComboBox *filterColumnComboBox;
    47.  
    48. };
    49.  
    50. #endif // CAMERADATABASEMODEL_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include "CameraDataBaseModel.h"
    2. #include <QtGui>
    3.  
    4. cameraDataBaseModel::cameraDataBaseModel(QWidget *parent,CCameraDataBase * cameraDataBase)
    5. : QWidget(parent)
    6. {
    7. this->camDataBase = cameraDataBase;
    8.  
    9. this->cameraModel = new QSortFilterProxyModel;
    10. this->cameraModel->setDynamicSortFilter(true);
    11.  
    12. this->cameraView = new QTreeView();
    13. this->cameraView->setRootIsDecorated(false);
    14. this->cameraView->setAlternatingRowColors(true);
    15. this->cameraView->setModel(cameraModel);
    16. this->cameraView->setSortingEnabled(true);
    17.  
    18. this->setSourceModel(setupModel());
    19. this->cameraView->hideColumn(4);
    20.  
    21. this->sortCaseSensitivityCheckBox = new QCheckBox(tr("Case sensitive sorting"));
    22. this->filterCaseSensitivityCheckBox = new QCheckBox(tr("Case sensitive filter"));
    23.  
    24. this->filterPatternLineEdit = new QLineEdit;
    25. this->filterPatternLabel = new QLabel(tr("&Filter pattern:"));
    26. this->filterPatternLabel->setBuddy(filterPatternLineEdit);
    27.  
    28. this->filterSyntaxComboBox = new QComboBox;
    29. this->filterSyntaxComboBox->addItem(tr("Regular expression"), QRegExp::RegExp);
    30. this->filterSyntaxComboBox->addItem(tr("Wildcard"), QRegExp::Wildcard);
    31. this->filterSyntaxComboBox->addItem(tr("Fixed string"), QRegExp::FixedString);
    32. this->filterSyntaxLabel = new QLabel(tr("Filter &syntax:"));
    33. this->filterSyntaxLabel->setBuddy(filterSyntaxComboBox);
    34.  
    35. this->filterColumnComboBox = new QComboBox;
    36. this->filterColumnComboBox->addItem(tr("Camera Model"));
    37. this->filterColumnComboBox->addItem(tr("Focal Length"));
    38. this->filterColumnComboBox->addItem(tr("Image Width"));
    39. this->filterColumnComboBox->addItem(tr("Image Height"));
    40. this->filterColumnLabel = new QLabel(tr("Filter &column:"));
    41. this->filterColumnLabel->setBuddy(filterColumnComboBox);
    42.  
    43. this->buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok
    44. | QDialogButtonBox::Cancel);
    45.  
    46. connect(filterPatternLineEdit, SIGNAL(textChanged(const QString &)),
    47. this, SLOT(filterRegExpChanged()));
    48. connect(filterSyntaxComboBox, SIGNAL(currentIndexChanged(int)),
    49. this, SLOT(filterRegExpChanged()));
    50. connect(filterColumnComboBox, SIGNAL(currentIndexChanged(int)),
    51. this, SLOT(filterColumnChanged()));
    52. connect(filterCaseSensitivityCheckBox, SIGNAL(toggled(bool)),
    53. this, SLOT(filterRegExpChanged()));
    54. connect(sortCaseSensitivityCheckBox, SIGNAL(toggled(bool)),
    55. this, SLOT(sortChanged()));
    56.  
    57. connect(this->buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
    58. connect(this->buttonBox, SIGNAL(rejected()), this, SLOT(close()));
    59.  
    60. QGridLayout *cameraLayout = new QGridLayout;
    61. cameraLayout->addWidget(cameraView, 0, 0, 1, 3);
    62. cameraLayout->addWidget(filterPatternLabel, 1, 0);
    63. cameraLayout->addWidget(filterPatternLineEdit, 1, 1, 1, 2);
    64. cameraLayout->addWidget(filterSyntaxLabel, 2, 0);
    65. cameraLayout->addWidget(filterSyntaxComboBox, 2, 1, 1, 2);
    66. cameraLayout->addWidget(filterColumnLabel, 3, 0);
    67. cameraLayout->addWidget(filterColumnComboBox, 3, 1, 1, 2);
    68. cameraLayout->addWidget(filterCaseSensitivityCheckBox, 4, 0, 1, 2);
    69. cameraLayout->addWidget(sortCaseSensitivityCheckBox, 4, 2);
    70. cameraLayout->addWidget(buttonBox,5,2);
    71. setLayout(cameraLayout);
    72.  
    73. setWindowTitle(tr("Select Camera"));
    74. resize(450, 400);
    75.  
    76. this->cameraView->sortByColumn(2, Qt::AscendingOrder);
    77. this->filterColumnComboBox->setCurrentIndex(0);
    78.  
    79. this->filterPatternLineEdit->setText("");
    80. this->filterCaseSensitivityCheckBox->setChecked(false);
    81. this->sortCaseSensitivityCheckBox->setChecked(false);
    82. }
    83.  
    84. cameraDataBaseModel::~cameraDataBaseModel()
    85. {
    86.  
    87. }
    88.  
    89. void cameraDataBaseModel::setSourceModel(QAbstractItemModel *model)
    90. {
    91. cameraModel->setSourceModel(model);
    92. }
    93.  
    94. void cameraDataBaseModel::filterRegExpChanged()
    95. {
    96. QRegExp::PatternSyntax syntax =
    97. QRegExp::PatternSyntax(filterSyntaxComboBox->itemData(
    98. filterSyntaxComboBox->currentIndex()).toInt());
    99. Qt::CaseSensitivity caseSensitivity =
    100. filterCaseSensitivityCheckBox->isChecked() ? Qt::CaseSensitive
    101. : Qt::CaseInsensitive;
    102.  
    103. QRegExp regExp(filterPatternLineEdit->text(), caseSensitivity, syntax);
    104. cameraModel->setFilterRegExp(regExp);
    105. }
    106.  
    107. void cameraDataBaseModel::filterColumnChanged()
    108. {
    109. cameraModel->setFilterKeyColumn(filterColumnComboBox->currentIndex());
    110. }
    111.  
    112. void cameraDataBaseModel::sortChanged()
    113. {
    114. cameraModel->setSortCaseSensitivity(
    115. sortCaseSensitivityCheckBox->isChecked() ? Qt::CaseSensitive
    116. : Qt::CaseInsensitive);
    117. }
    118.  
    119.  
    120. void cameraDataBaseModel::addCamera(QAbstractItemModel *model, const QString &cameraModel,
    121. const double &FocalLength,const int &ImageWidth, const int &ImageHeight, const int &cameraID)
    122. {
    123. model->insertRow(0);
    124. model->setData(model->index(0, 0), cameraModel);
    125. model->setData(model->index(0, 1), FocalLength);
    126. model->setData(model->index(0, 2), ImageWidth);
    127. model->setData(model->index(0, 3), ImageHeight);
    128. model->setData(model->index(0, 4), cameraID);
    129. }
    130.  
    131.  
    132. QAbstractItemModel * cameraDataBaseModel::setupModel()
    133. {
    134. model = new QStandardItemModel(0, 5);
    135.  
    136. model->setHeaderData(0, Qt::Horizontal, QObject::tr("Camera Model"));
    137. model->setHeaderData(1, Qt::Horizontal, QObject::tr("Focal Length"));
    138. model->setHeaderData(2, Qt::Horizontal, QObject::tr("Image Width"));
    139. model->setHeaderData(3, Qt::Horizontal, QObject::tr("Image Height"));
    140. model->setHeaderData(4, Qt::Horizontal, QObject::tr("Camera ID"));
    141.  
    142. for (int i =0; i < this->camDataBase->globalCameras.GetSize(); i++)
    143. {
    144. addCamera( model, QString::fromUtf8(this->camDataBase->globalCameras.GetAt(i)->getMakeAndModel()),
    145. this->camDataBase->globalCameras.GetAt(i)->getFocalLength(),
    146. this->camDataBase->globalCameras.GetAt(i)->getImageWidth(),
    147. this->camDataBase->globalCameras.GetAt(i)->getImageHeight(), i );
    148. }
    149.  
    150. return model;
    151. }
    152.  
    153. void cameraDataBaseModel::accept()
    154. {
    155. //this where i want to grab the selection of the user and find the Camera ID which is the 4th colum of the model
    156. }
    To copy to clipboard, switch view to plain text mode 


    Here is What i have tried


    Qt Code:
    1. // What i have tried is getting the currentindex() which actually works like a charm
    2. // from what is see while debugging as i get the row and column select
    3. QModelIndex curIndex = cameraView->currentIndex() ;
    4.  
    5. // Then get the row
    6. int numRows = curIndex.row();
    7. //move to the column but over here i get an invalid ModelIndex cause i see in debugging r=-1 c=-1
    8. QModelIndex index = model->index(numRows, 4, curIndex);
    9.  
    10. //then get the data
    11. QVariant blabla = index.data (Qt::DisplayRole)
    To copy to clipboard, switch view to plain text mode 

    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...!
    Last edited by xerionn; 16th January 2009 at 01:10.

Similar Threads

  1. Item View classes and returning data !
    By xerionn in forum Newbie
    Replies: 3
    Last Post: 16th January 2009, 08:52

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.