Results 1 to 9 of 9

Thread: Default Item View classes and returning data from model

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

    Default Default Item View classes and returning data from model

    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.

  2. #2
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

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

    see model-view-programming; we will gladly answer specific questions you have

  3. #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.

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

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

    From what I understood, you want to get the sibling, not a child of "curIndex". It's easiest to do with QModelIndex::sibling():
    Qt Code:
    1. QModelIndex index = curIndex.sibling(numRows, 4);
    To copy to clipboard, switch view to plain text mode 
    Alternatively you can do similar than you tried, but with correct parent:
    Qt Code:
    1. QModelIndex index = model->index(numRows, 4, curIndex.parent());
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  5. The following user says thank you to jpn for this useful post:

    xerionn (18th January 2009)

  6. #5
    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

    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

  7. #6
    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

    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 ?

  8. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

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

    Quote Originally Posted by xerionn View Post
    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().

  9. #8
    Join Date
    Feb 2009
    Posts
    51
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

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

    Quote Originally Posted by wysota View Post
    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
    Sincerely,

    Wieland J.

  10. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

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

    What is the base class of your model?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.