Results 1 to 8 of 8

Thread: model/view, can't hide row/col

  1. #1
    Join Date
    Jul 2007
    Posts
    27
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default model/view, can't hide row/col

    I made my own custom model to represent a table.
    I used a QList<QStringList>, each element of the QList represents a column of my table and each element of my QStringList represents a row (with row 0 being my header data).

    I've followed the model/view programming documentation from the Trolltech website, but my view will not hide columns or rows for some reason.

    I'm assuming that the view is populating it's information by using my model's data() function.

    Here's my model's data() function:

    Qt Code:
    1. QVariant TableModel::data(const QModelIndex &index, int role) const
    2. {
    3. int rows = 0;
    4.  
    5. if(!index.isValid())
    6. {
    7. return QVariant();
    8. }
    9.  
    10. // NOTE: I do this to prevent "invalid index" errors, if my QList is empty.
    11. if(column.empty())
    12. {
    13. rows = 0;
    14. }
    15. else
    16. {
    17. rows = column[0].count();
    18. }
    19. if((index.row() >= rows) ||
    20. (index.column() >= column.size()))
    21. {
    22. return QVariant();
    23. }
    24.  
    25. if(role == Qt::DisplayRole)
    26. {
    27. return column[index.column()].at(index.row());
    28. }
    29. else
    30. {
    31. return QVariant();
    32. }
    33. }
    To copy to clipboard, switch view to plain text mode 

    And in my main() function:

    Qt Code:
    1. int main(int argc, char **argv)
    2. {
    3. QApplication myApp(argc, argv);
    4.  
    5. TableModel data;
    6. QAbstractItemModel *model = &data;
    7.  
    8. QTableView *view = new QTableView;
    9. view->setModel(model);
    10. view->setGridStyle(Qt::NoPen);
    11. // Trying to hide row 0, which contains my header data.
    12. view->hideRow(0);
    13. view->setRowHidden(0, true);
    14. // neither of these functions hide anything.
    15.  
    16. view->show();
    17.  
    18. // This is just a test function that fills the table with data.
    19. data.init();
    20.  
    21. return myApp.exec();
    22. }
    To copy to clipboard, switch view to plain text mode 

    I've tried hideColumn() also, and it does nothing. I've also tried other numbers (1,2,3, etc) for hideRow() and hideColumn(), but nothing happens.

    What am I doing wrong?

  2. #2
    Join Date
    Jan 2006
    Location
    Edmonton, Canada
    Posts
    101
    Thanks
    13
    Thanked 6 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: model/view, can't hide row/col

    If you're trying to hide the horizontal header, you might want to take this approach:

    Qt Code:
    1. view->horizontalHeader()->hide();
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to Jimmy2775 for this useful post:

    tituslup (10th September 2009)

  4. #3
    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: model/view, can't hide row/col

    Perhaps it's because by the time you hide the row there is nothing in the model. You insert (init()) items afterwards.
    J-P Nurmi

  5. #4
    Join Date
    Jul 2007
    Posts
    27
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: model/view, can't hide row/col

    Quote Originally Posted by Jimmy2775 View Post
    If you're trying to hide the horizontal header, you might want to take this approach:

    Qt Code:
    1. view->horizontalHeader()->hide();
    To copy to clipboard, switch view to plain text mode 
    I don't want to hide my horizontal header, but I want to hide row 0, which contains my header data.

  6. #5
    Join Date
    Jul 2007
    Posts
    27
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: model/view, can't hide row/col

    Quote Originally Posted by jpn View Post
    Perhaps it's because by the time you hide the row there is nothing in the model. You insert (init()) items afterwards.
    I thought that might be the case, so I moved things around so that the init was first and the hide*() functions were after it, but it didn't make any difference.

  7. #6
    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: model/view, can't hide row/col

    May we see TableModel::TableModel() and TableModel::init()?
    J-P Nurmi

  8. #7
    Join Date
    Jul 2007
    Posts
    27
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: model/view, can't hide row/col

    As requested:

    Qt Code:
    1. TableModel::TableModel(QObject *parent)
    2. {
    3. init();
    4. }
    5.  
    6. //--//--//
    7. void TableModel::init()
    8. {
    9. // Set my header data in element 0 of my stringlist.
    10. column.insert(0, QStringList("Col A"));
    11. column.insert(1, QStringList("Col B"));
    12. column.insert(2, QStringList("Col C"));
    13.  
    14. // set row 1 data in column 1, 2 and 3
    15. column[0].append("greco");
    16. column[1].append("dan");
    17. column[2].append("gordon");
    18.  
    19. // set row 2 data in column 1, 2 and 3
    20. column[0].append("trish");
    21. column[1].append("jessie");
    22. column[2].append("morgan");
    23. }
    To copy to clipboard, switch view to plain text mode 

    I've attached a picture (screenshot) of what this looks like... notice row 0 has the same data as my header... now if I could just hide row 0, everything would be fine!!!

    Sincerely,

    Gordon E.
    Attached Images Attached Images

  9. #8
    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: model/view, can't hide row/col

    Apparently headerData() returns column[N].at(0) so couldn't you make data() return data starting from column[N].at(1), and make rowCount() return column[N].count() - 1?
    J-P Nurmi

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.