Results 1 to 2 of 2

Thread: QTableView and custom QHeaderView: header not showing

  1. #1
    Join Date
    Sep 2009
    Posts
    7
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default QTableView and custom QHeaderView: header not showing

    Edited:

    guys,

    I want to use this http://qt-apps.org/content/show.php/...content=103154 in my app. So I included the files and placed some code in my model class ( the code that's actually in example1.cpp ). But I no header is shown. And when I don't use QHeaderView but only the reimplemented headerData function from QAbstractTableModel I get my header. This is my code:

    mymodel.h:
    Qt Code:
    1. #ifndef TABLEMODEL_H
    2. #define TABLEMODEL_H
    3.  
    4. #include "tabledata.h"
    5.  
    6. #include <QAbstractTableModel>
    7. #include <QModelIndex>
    8. #include <QStandardItemModel>
    9. #include <QVariant>
    10.  
    11. typedef std::list< std::pair < HeaderData, std::vector< std::string > > > dataMap;
    12.  
    13. class TABLEModel : public QAbstractTableModel
    14. {
    15. Q_OBJECT
    16.  
    17. public:
    18. TABLEModel( QObject* Qparent = 0 );
    19. TABLEModel( QObject* Qparent, TABLEData* tableData );
    20.  
    21. ~TABLEModel();
    22. void fillHeaderModel();
    23. QVariant data( const QModelIndex &indexQ, int role ) const;
    24. QVariant headerData(int section, Qt::Orientation orientation,
    25. int role = Qt::DisplayRole ) const;
    26.  
    27. }
    To copy to clipboard, switch view to plain text mode 
    mymodel.cpp:
    Qt Code:
    1. TABLEModel::TABLEModel( QObject *parentQ, TABLEData* tableData )
    2. : QAbstractTableModel( parentQ ), m_tableData( tableData )
    3. {
    4. setColumnCount( m_tableData->getTable()->getColumnCount());
    5. fillHeaderModel();
    6. }
    7.  
    8. QVariant TABLEModel::data( const QModelIndex &indexQ, int role ) const
    9. {
    10. if( role == HierarchicalHeaderView::HorizontalHeaderDataRole )
    11. {
    12. v.setValue(( QObject* )& m_horizontalHeaderModel );
    13. return v;
    14. }
    15.  
    16. if(role==HierarchicalHeaderView::VerticalHeaderDataRole)
    17. {
    18. v.setValue((QObject*)& m_verticalHeaderModel);
    19. return v;
    20. }
    21.  
    22. if( role == Qt::DisplayRole && indexQ.isValid())
    23. {
    24. return QString( m_tableData->getParameterData( indexQ.column(), indexQ.row()).c_str());
    25. }
    26.  
    27. return QVariant();
    28. }
    29.  
    30. void TABLEModel::fillHeaderModel()
    31. {
    32. QStandardItem* rootItem = new QStandardItem("root");
    33. QList<QStandardItem*> l;
    34.  
    35. QStandardItem* cell=new QStandardItem("level 2");
    36. l.push_back(cell);
    37. rootItem->appendColumn(l);
    38.  
    39. l.clear();
    40.  
    41. l.push_back(new QStandardItem("level 2"));
    42. rootItem->appendColumn(l);
    43.  
    44. l.clear();
    45.  
    46. m_horizontalHeaderModel.setItem( 0, 0, rootItem );
    47. }
    48.  
    49. QVariant TABLEModel::headerData(int section, Qt::Orientation orientation, int role ) const
    50. {
    51. if( role != Qt::DisplayRole )
    52. return QVariant();
    53.  
    54. if( orientation == Qt::Horizontal )
    55. {
    56. QString data( m_tableData->getColumns()[ section ]->getLabel().c_str());
    57.  
    58. return data;
    59. }
    60.  
    61. return QVariant();
    62.  
    63. }
    To copy to clipboard, switch view to plain text mode 

    dialog.h:
    Qt Code:
    1. #ifndef DIALOGTABLE_H
    2. #define DIALOGTABLE_H
    3.  
    4. #include "tabledata.h"
    5. #include "tabletable.h"
    6.  
    7. #include "dialogparameterdialog.h"
    8.  
    9. #include <QDialog>
    10. #include <QTableView>
    11.  
    12. class TABLEModel;
    13.  
    14. class DIALOGTable : public DIALOGParameterDialog
    15. {
    16. Q_OBJECT
    17.  
    18. public:
    19. DIALOGTable( TABLETable &table, QWidget *parent = 0 );
    20.  
    21. private slots:
    22. void slotCreateTable();
    23.  
    24. private:
    25. QTableView* m_view;
    To copy to clipboard, switch view to plain text mode 

    dialog.cpp:
    Qt Code:
    1. DIALOGTable::DIALOGTable( TABLETable &table, QWidget *parentW )
    2. :DIALOGParameterDialog( table, parentW, ), m_table( table )
    3. {
    4. m_view = new QTableView( this );
    5. }
    6.  
    7. void DIALOGTable::slotCreateTable()
    8. {
    9. HierarchicalHeaderView* hv = new HierarchicalHeaderView( Qt::Horizontal, m_view );
    10. hv->setHighlightSections( true );
    11. hv->setClickable( true );
    12. hv->setMovable(true);
    13. m_view->setHorizontalHeader( hv );
    14.  
    15. TABLEModel* model = new TABLEModel( this, m_tableData );
    16. model->setRowCount( 0 ); //this is because I only want to show the header at first
    17.  
    18. m_view->setModel( model );
    19. m_view->resizeColumnsToContents();
    20. m_view->resizeRowsToContents();
    21. m_view->show();
    22. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. class HeaderData
    2. {
    3. public:
    4. HeaderData(){};
    5. ~HeaderData(){};
    6.  
    7. public:
    8. const std::string&
    9. getLabel() const
    10. {
    11. return m_label;
    12. }
    13.  
    14. void
    15. setLabel( const std::string& label )
    16. {
    17. m_label = label;
    18. }
    19.  
    20. const std::string&
    21. getUnit() const
    22. {
    23. return m_unit;
    24. }
    25.  
    26. void
    27. setUnit( const std::string& unit )
    28. {
    29. m_unit = unit;
    30. }
    31.  
    32. const std::vector< std::string >&
    33. getSubLabel() const
    34. {
    35. return m_subLabel;
    36. }
    37.  
    38. void
    39. setSubLabel( const std::vector< std::string >& subLabel )
    40. {
    41. m_subLabel = subLabel;
    42. }
    43.  
    44. private:
    45. std::string m_label;
    46. std::string m_unit;
    47. std::vector< std::string > m_subLabel;
    48. };
    To copy to clipboard, switch view to plain text mode 
    Last edited by xyfix; 8th September 2010 at 19:30.

  2. #2
    Join Date
    Sep 2009
    Posts
    7
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: QTableView and custom QHeaderView: header not showing

    when I comment out the setting of the custom headerview in the dialog class I can see the header. And when I just add a simple QHeaderView instead of the HierarchicalHeaderView I don't get a header.

Similar Threads

  1. Replies: 2
    Last Post: 17th July 2010, 21:07
  2. QHeaderView: background of selected header item with CSS
    By antarctic in forum Qt Programming
    Replies: 5
    Last Post: 8th June 2009, 07:37
  3. QTableView with custom QHeaderView
    By xbase in forum Qt Programming
    Replies: 6
    Last Post: 14th September 2007, 13:21
  4. Showing Icon in vertical header of a table
    By vishal.chauhan in forum Qt Programming
    Replies: 7
    Last Post: 15th January 2007, 10:44
  5. Custom QHeaderView in Qt 4.1.1
    By Tair in forum Qt Programming
    Replies: 2
    Last Post: 11th July 2006, 09:44

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.