Page 1 of 2 12 LastLast
Results 1 to 20 of 22

Thread: how i can chagne Qtableview horizontal header text as well as remove Indexes vHeader

  1. #1
    Join Date
    Jul 2013
    Posts
    54
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Question how i can chagne Qtableview horizontal header text as well as remove Indexes vHeader

    Hi every one

    i'm using qsqlquerymodel to get data from sqlite database and Qtableview widget to retrive data

    i want to custimize ( vertical header and horizontal header in Qtableview )

    vertical header ---> remove indexes numbers but vertical header still shown and place icon when the entier row is selected
    horizontal header ---> change the text of caption



    her what i code


    Qt Code:
    1. // for horizontal header
    2. ProduitModel->setHeaderData(0,Qt::Horizontal, "Id");
    3. ProduitModel->setHeaderData(1,Qt::Horizontal, "Designation");
    4. ProduitModel->setHeaderData(2,Qt::Horizontal, "Famille");
    5. ProduitModel->setHeaderData(3,Qt::Horizontal, "Qte Maximum");
    6. ProduitModel->setHeaderData(4,Qt::Horizontal, "Qte Minimum");
    7. ProduitModel->setHeaderData(5,Qt::Horizontal, "Prix");
    8. ProduitModel->setHeaderData(6,Qt::Horizontal, "Prix vente");
    9. // form vertical header
    10. ProduitView->verticalHeader()->setStyleSheet("QHeaderView::section:checked {"
    11. " background-color: black; }"
    12. " background-image: url(imgs/arrow.png)"
    13. );
    To copy to clipboard, switch view to plain text mode 


    Her's image that explain what i mean

    qsqlquerymodelprobleme.jpg

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: how i can chagne Qtableview horizontal header text as well as remove Indexes vHea

    Write a proxy model derived from QIdentityProxyModel, and implement the QIdentityProxyModel::headerData() function and return what ever data is required in in header.

    In Qt::Vertical header, return nothing for Qt::ItemDataRole and return icon for Qt::DecorationRole
    In Qt::Horizontal header return the desired label in Qt::ItemDataRole
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

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

    advseo32 (15th July 2013)

  4. #3
    Join Date
    Jul 2013
    Posts
    54
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how i can chagne Qtableview horizontal header text as well as remove Indexes vHea

    thank's Santosh but really, i havn't any idea how to do it

    can you give an example OR some hints to start coding ??
    Last edited by advseo32; 15th July 2013 at 21:33.

  5. #4
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: how i can chagne Qtableview horizontal header text as well as remove Indexes vHea

    Here is fully functional code with usage example
    Qt Code:
    1. class HeaderModel : public QIdentityProxyModel
    2. {
    3. public:
    4. explicit HeaderModel(QObject * parent = 0)
    5. : QIdentityProxyModel(parent)
    6. { }
    7.  
    8. QVariant headerData(int section, Qt::Orientation orientation, int role) const
    9. {
    10. if(orientation == Qt::Horizontal)
    11. {
    12. if(role == Qt::DisplayRole)
    13. switch(section)
    14. {
    15. case 0: return "Id"; break;
    16. case 1: return "Designation"; break;
    17. case 2: return "Famille"; break;
    18. case 3: return "Qte Maximum"; break;
    19. case 4: return "Qte Minimum"; break;
    20. case 5: return "Prix"; break;
    21. case 6: return "Prix vente"; break;
    22. default:
    23. return QString("Column %1").arg(section + 1);
    24. break;
    25. }
    26. }
    27. else if(orientation == Qt::Vertical)
    28. {
    29. if(role == Qt::DecorationRole)
    30. return QIcon("imgs/arrow.png");
    31. }
    32.  
    33. return QVariant();
    34. }
    35. };
    36.  
    37. //usage
    38. {
    39. QSqlQueryModel sqlQueryModel;
    40. ...
    41.  
    42. HeaderModel headerModel;
    43.  
    44. QTableView tableView;
    45.  
    46. headerModel.setSourceModel(&sqlQueryModel);
    47.  
    48. tableView.setModel(&headerModel);
    49.  
    50. tableView.show();
    51. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by Santosh Reddy; 15th July 2013 at 22:02.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  6. #5
    Join Date
    Jul 2013
    Posts
    54
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how i can chagne Qtableview horizontal header text as well as remove Indexes vHea

    Quote Originally Posted by Santosh Reddy View Post
    Here is fully functional code with usage example
    Qt Code:
    1. class HeaderModel : public QIdentityProxyModel
    2. {
    3. public:
    4. explicit HeaderModel(QObject * parent = 0)
    5. : QIdentityProxyModel(parent)
    6. { }
    7.  
    8. QVariant headerData(int section, Qt::Orientation orientation, int role) const
    9. {
    10. if(orientation == Qt::Horizontal)
    11. {
    12. if(role == Qt::DisplayRole)
    13. switch(section)
    14. {
    15. case 0: return "Id"; break;
    16. case 1: return "Designation"; break;
    17. case 2: return "Famille"; break;
    18. case 3: return "Qte Maximum"; break;
    19. case 4: return "Qte Minimum"; break;
    20. case 5: return "Prix"; break;
    21. case 6: return "Prix vente"; break;
    22. default:
    23. return QString("Column %1").arg(section + 1);
    24. break;
    25. }
    26. }
    27. else if(orientation == Qt::Vertical)
    28. {
    29. if(role == Qt::DecorationRole)
    30. return QIcon("imgs/arrow.png");
    31. }
    32.  
    33. return QVariant();
    34. }
    35. };
    36.  
    37. //usage
    38. {
    39. QSqlQueryModel sqlQueryModel;
    40. ...
    41.  
    42. HeaderModel headerModel;
    43.  
    44. QTableView tableView;
    45.  
    46. headerModel.setSourceModel(&sqlQueryModel);
    47.  
    48. tableView.setModel(&headerModel);
    49.  
    50. tableView.show();
    51. }
    To copy to clipboard, switch view to plain text mode 
    Wow Reddy,
    Your code Work perfectly, but i have Qt 4.7 witch mean that QIdentityProxyModel is not supported. So, i have used QSortFilterProxyModel
    the horziontal header changed with my text , and vertical header to arrow instead of numbers
    i need the vertical header show arrow.png only when the user select the row , what's the Role that allow me to do this ?

  7. #6
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: how i can chagne Qtableview horizontal header text as well as remove Indexes vHea

    Oops... Sorry I din't see that you using Qt4, anyway you have figured out a way.

    For icon use Qt::DecorationRole
    Last edited by Santosh Reddy; 16th July 2013 at 08:07.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  8. #7
    Join Date
    Jul 2013
    Posts
    54
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how i can chagne Qtableview horizontal header text as well as remove Indexes vHea

    Quote Originally Posted by Santosh Reddy View Post
    Oops... Sorry I din't see that you using Qt4, anyway you have figured out a way.

    For icon use Qt:ecorationRole
    Sorry, i don't noticed that i'm using Qt 4.7

    any way Qt:ecorationRole get all vertical index filled by arrow.png icon , i want the icon only shown when the user select the entier row

  9. #8
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: how i can chagne Qtableview horizontal header text as well as remove Indexes vHea

    Looks like it is not supported to change the icon/image on the header only when the row/column is selected. Only thing that works is backgroud color using stylesheets
    Qt Code:
    1. tableView.setStyleSheet("QHeaderView::section:checked { background-color: red; }");
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  10. #9
    Join Date
    Jul 2013
    Posts
    54
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how i can chagne Qtableview horizontal header text as well as remove Indexes vHea

    Maybe , but i have an application use this action


    arrow.jpg

  11. #10
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: how i can chagne Qtableview horizontal header text as well as remove Indexes vHea

    Ok I can think of a way to do this. We can create and install a custom QHeaderView on QTableView's verticalheader. Here a revised solution.

    Qt Code:
    1. // Handles Horizontal header
    2. class HeaderModel : public QSortFilterProxyModel
    3. {
    4. Q_OBJECT
    5.  
    6. public:
    7. explicit HeaderModel(QObject * parent = 0)
    8. { }
    9.  
    10. QVariant headerData(int section, Qt::Orientation orientation, int role) const
    11. {
    12. if(orientation == Qt::Horizontal)
    13. {
    14. if(role == Qt::DisplayRole)
    15. switch(section)
    16. {
    17. case 0: return "Id"; break;
    18. case 1: return "Designation"; break;
    19. case 2: return "Famille"; break;
    20. case 3: return "Qte Maximum"; break;
    21. case 4: return "Qte Minimum"; break;
    22. case 5: return "Prix"; break;
    23. case 6: return "Prix vente"; break;
    24. default:
    25. return QString("Column %1").arg(section + 1);
    26. break;
    27. }
    28. }
    29.  
    30. return QVariant();
    31. }
    32. };
    33.  
    34. // Handles Vertical header
    35. class Header : public QHeaderView
    36. {
    37. public:
    38. explicit Header(QWidget * parent)
    39. : QHeaderView(Qt::Vertical, parent)
    40. , mSelectedIndex(-1)
    41. {
    42. setMinimumWidth(QImage("imgs/arrow.png").width());
    43. }
    44.  
    45. protected:
    46. void mousePressEvent(QMouseEvent * event)
    47. {
    48. QHeaderView::mousePressEvent(event);
    49.  
    50. int section = logicalIndexAt(event->pos());
    51.  
    52. if(mSelectedIndex != section)
    53. {
    54. emit sectionClicked(section);
    55. emit sectionPressed(section);
    56. emit sectionEntered(section);
    57.  
    58. mSelectedIndex = section;
    59. update();
    60. }
    61. }
    62.  
    63. void paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const
    64. {
    65. painter->save();
    66. if(mSelectedIndex > -1)
    67. {
    68. if(mSelectedIndex == logicalIndex)
    69. if(!isSectionHidden(logicalIndex))
    70. painter->drawImage(rect, QImage("imgs/arrow.png"));
    71. }
    72. painter->restore();
    73. }
    74.  
    75. private:
    76. int mSelectedIndex;
    77. };
    78.  
    79. //Usage
    80.  
    81. QTableView tableView;
    82.  
    83. HeaderModel headerModel;
    84. headerModel.setSourceModel(&model); //<<<< Horizontal Header
    85.  
    86. tableView.setVerticalHeader(new Header(&tableView)); //<<<< Vertial Header
    87. tableView.setModel(&headerModel);
    88. tableView.show();
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  12. #11
    Join Date
    Jul 2013
    Posts
    54
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how i can chagne Qtableview horizontal header text as well as remove Indexes vHea

    thank's it's nearly what i need ,
    how i can handle when the user use up and down keyboard keys

    with keyPressEvent ???

  13. #12
    Join Date
    Jul 2013
    Posts
    54
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how i can chagne Qtableview horizontal header text as well as remove Indexes vHea

    how i can handle this , only one the user use Key_Up and Key_Down

  14. #13
    Join Date
    Jul 2013
    Posts
    54
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how i can chagne Qtableview horizontal header text as well as remove Indexes vHea

    what should i learn to be able to custimise Qtableview completly

  15. #14
    Join Date
    Aug 2012
    Location
    Loughborough, UK
    Posts
    29
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows

    Default Re: how i can chagne Qtableview horizontal header text as well as remove Indexes vHea


  16. #15
    Join Date
    Jul 2013
    Posts
    54
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how i can chagne Qtableview horizontal header text as well as remove Indexes vHea

    Quote Originally Posted by rockdemon View Post
    thanks, this is a great book


    Added after 12 minutes:


    any body can help me ??
    Last edited by advseo32; 18th July 2013 at 11:48.

  17. #16
    Join Date
    Aug 2012
    Location
    Loughborough, UK
    Posts
    29
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows

    Default Re: how i can chagne Qtableview horizontal header text as well as remove Indexes vHea

    something like? http://qt.developpez.com/doc/4.7/modelview/

    They're not all great - but there are some examples on t'internet if you google

    The book above does have the best walkthrough i've found for model view in Qt.

  18. #17
    Join Date
    Jul 2013
    Posts
    54
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how i can chagne Qtableview horizontal header text as well as remove Indexes vHea

    Quote Originally Posted by rockdemon View Post
    something like? http://qt.developpez.com/doc/4.7/modelview/

    They're not all great - but there are some examples on t'internet if you google

    The book above does have the best walkthrough i've found for model view in Qt.
    thanks i will read page by page


    Added after 13 minutes:


    Her is detailled description for my problem

    i use Qsqlquerymodel with sqlite3 DBMS

    to retrieve data from database i use two Qtableview widgets

    i want to the user to be able to work like so:

    the user uses only keyboard(no mouse )

    when the data grabed from database and showed in the Qtableview via Qsqlquerymodel the first row in the table must be selected by default

    the user uses arrow keys ( key_Down and Key_Up) to move between rows with "arrow.png" icon shown in verticalheader of Qtableview

    when the user clics Entrer_Key, the row selected will be appened in the second Qtableview


    plz help me , i have read model/view many times , and i figured out what really mean , but i still have a porbleme with customizing mytable view to work with


    Qkeymouse events, currentrowchanged signal
    Last edited by advseo32; 18th July 2013 at 19:36.

  19. #18
    Join Date
    Jul 2013
    Posts
    54
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how i can chagne Qtableview horizontal header text as well as remove Indexes vHea

    no body can help me ??

  20. #19
    Join Date
    Jul 2013
    Posts
    54
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how i can chagne Qtableview horizontal header text as well as remove Indexes vHea

    plz help me, i need the soulution

  21. #20
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: how i can chagne Qtableview horizontal header text as well as remove Indexes vHea

    Here you go, keyboard only operation

    Qt Code:
    1. // Handles Horizontal header
    2. class HeaderModel : public QSortFilterProxyModel
    3. {
    4. Q_OBJECT
    5.  
    6. public:
    7. explicit HeaderModel(QObject * parent = 0)
    8. { }
    9.  
    10. QVariant headerData(int section, Qt::Orientation orientation, int role) const
    11. {
    12. if(orientation == Qt::Horizontal)
    13. {
    14. if(role == Qt::DisplayRole)
    15. switch(section)
    16. {
    17. case 0: return "Id"; break;
    18. case 1: return "Designation"; break;
    19. case 2: return "Famille"; break;
    20. case 3: return "Qte Maximum"; break;
    21. case 4: return "Qte Minimum"; break;
    22. case 5: return "Prix"; break;
    23. case 6: return "Prix vente"; break;
    24. default:
    25. return QString("Column %1").arg(section + 1);
    26. break;
    27. }
    28. }
    29.  
    30. return QVariant();
    31. }
    32. };
    33.  
    34. // Handles Vertical header
    35. class Header : public QHeaderView
    36. {
    37. Q_OBJECT
    38. public:
    39. explicit Header(QWidget * parent)
    40. : QHeaderView(Qt::Vertical, parent)
    41. , mSelectedIndex(-1)
    42. {
    43. setMinimumWidth(QImage("imgs/arrow.png").width());
    44. }
    45.  
    46. public slots:
    47. void changeCurrent(const QModelIndex & current, const QModelIndex & previous)
    48. {
    49. int section = current.row();
    50.  
    51. if(mSelectedIndex != section)
    52. {
    53. mSelectedIndex = section;
    54. viewport()->update();
    55. }
    56. }
    57.  
    58. protected:
    59. void paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const
    60. {
    61. painter->save();
    62. if(mSelectedIndex > -1)
    63. {
    64. if(mSelectedIndex == logicalIndex)
    65. if(!isSectionHidden(logicalIndex))
    66. painter->drawImage(rect, QImage("imgs/arrow.png"));
    67. }
    68. painter->restore();
    69. }
    70.  
    71. private:
    72. int mSelectedIndex;
    73. };
    74.  
    75. //Usage
    76.  
    77. HeaderModel headerModel;
    78. headerModel.setSourceModel(&model);
    79.  
    80. QTableView tableView;
    81. Header * header = new Header(&tableView);
    82. tableView.setModel(&headerModel);
    83. tableView.connect(tableView.selectionModel(), SIGNAL(currentChanged(QModelIndex,QModelIndex)), header, SLOT(changeCurrent(QModelIndex,QModelIndex)));
    84. tableView.setVerticalHeader(header);
    85. tableView.setSelectionMode(QAbstractItemView::SingleSelection);
    86. tableView.setSelectionBehavior(QAbstractItemView::SelectRows);
    87. tableView.show();
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

Similar Threads

  1. QTableView Hidden Sections and Indexes
    By mechsin in forum Newbie
    Replies: 1
    Last Post: 15th July 2012, 17:49
  2. Replies: 1
    Last Post: 1st May 2010, 23:03
  3. QTableView Horizontal Header's Width
    By araglin in forum Newbie
    Replies: 3
    Last Post: 21st December 2008, 08:54
  4. How to set QTableView width to width of horizontal header?
    By martinb0820 in forum Qt Programming
    Replies: 0
    Last Post: 2nd December 2008, 20:51
  5. How to customize horizontal header (diagonal header view)
    By vairamuthu.g in forum Qt Programming
    Replies: 4
    Last Post: 4th September 2008, 15:59

Tags for this Thread

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.