Results 1 to 10 of 10

Thread: QMenu in QHeaderView

  1. #1
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default QMenu in QHeaderView

    Hi,

    How can I get the rect of a section of headerView so that I can make a menu appear in a section of a headerView.

    I wrote the following code but it is not working.

    Qt Code:
    1. DataTable::DataTable(QWidget *parent) : QTableWidget(parent)
    2. {
    3. setAlternatingRowColors(true);
    4.  
    5. verticalHeader()->hide();
    6.  
    7. tableHeader = new TableHeader(this);
    8. connect(tableHeader,SIGNAL(clicked(const QModelIndex &)),this,SLOT(headerClicked(const QModelIndex &)));
    9. setHorizontalHeader(tableHeader);
    10.  
    11. headerMenu = new QMenu(tr("Undefined"),tableHeader);
    12. connect(headerMenu,SIGNAL(triggered(QAction *)),this,SLOT(headerMenuTriggered(QAction *)));
    13.  
    14. populateHeaderMenu();//Add the actions to the menu
    15. }
    16.  
    17. //The following slot is not called itself
    18. void DataTable::headerClicked(const QModelIndex &index)
    19. {
    20. //I wonder if this is correct.
    21. QRect rect = visualRect(index);
    22. headerMenu->exec(tableHeader->mapToGlobal(QPoint(rect.x(),rect.y())));
    23. }
    To copy to clipboard, switch view to plain text mode 

    The same menu needs to appear for all the columns and therefore I used this way.
    Is there a better way of doing it.

    Can someone please help me with this?

    Thanks a lot.

  2. #2
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QMenu in QHeaderView

    Any ideas ??

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QMenu in QHeaderView

    Shouldn't it be:
    Qt Code:
    1. void DataTable::headerClicked(const QModelIndex &index)
    2. {
    3. QRect rect = tableHeader->visualRect(index);
    4. headerMenu->exec(tableHeader->mapToGlobal(QPoint(rect.x(),rect.y())));
    5. }
    To copy to clipboard, switch view to plain text mode 
    ?

  4. #4
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QMenu in QHeaderView

    Thanks for the reply. But the problem is that this slot itself is not called.

    Am I doing anything wrong.

    Thanks a lot.

  5. #5
    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: QMenu in QHeaderView

    The reason is that QHeaderView doesn't emit clicked() signal. Use QHeaderView::sectionClicked(int section) instead.
    J-P Nurmi

  6. #6
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QMenu in QHeaderView

    Quote Originally Posted by jpn
    Use QHeaderView::sectionClicked(int section) instead
    But then how can I get the bound of that section ?

    Thanks

  7. #7
    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: QMenu in QHeaderView

    Qt Code:
    1. void headerClicked(int section)
    2. {
    3. QModelIndex idx = tableHeader->model()->index(0, section);
    4. // about jacek's solution: AbstractItemView::visualRect()
    5. // is protected and this seems to work..
    6. QRect rect = visualRect(idx);
    7. headerMenu->exec(tableHeader->mapToGlobal(rect.topLeft()));
    8. }
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  8. #8
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QMenu in QHeaderView

    The slot is now called but the rect is(0,0,-1,-1) which is wrong. I am not getting the right co-ordiantes.

    Any ideas?

    Here is my code

    Qt Code:
    1. void DataTable::headerClicked(int logicalIndex)
    2. {
    3. QModelIndex index = tableHeader->model()->index(0,logicalIndex);
    4.  
    5. QRect rect = tableHeader->visualRect(index);//QHeaderView::visualRect(index)
    6. //rect is wrong
    7. headerMenu->exec(tableHeader->mapToGlobal(QPoint(rect.x(),rect.y())));
    8. }
    To copy to clipboard, switch view to plain text mode 

    Thaks a lot

  9. #9
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QMenu in QHeaderView

    I have finally solve this. The following code will make the horizontal header of a QTableWidget behave like a menu bar.

    Qt Code:
    1. //slot connected to the signal of sectionClicked(int) of QHeaderView.
    2. void DataTable::headerClicked(int logicalIndex)
    3. {
    4. QModelIndex index = model()->index(0,logicalIndex);
    5. QRect rect = visualRect(index);
    6. headerMenu->exec(mapToGlobal(QPoint(rect.x(),rect.y()+tableHeader->height())));
    7. }
    To copy to clipboard, switch view to plain text mode 

  10. #10
    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: QMenu in QHeaderView

    Notice the difference?
    Qt Code:
    1. headerMenu->exec(mapToGlobal(QPoint(rect.x(),rect.y()+tableHeader->height())));
    To copy to clipboard, switch view to plain text mode 
    and
    Qt Code:
    1. headerMenu->exec(tableHeader->mapToGlobal(rect.topLeft()));
    To copy to clipboard, switch view to plain text mode 
    It matters from which widget's point of view the coordinates are mapped to global..
    J-P Nurmi

Similar Threads

  1. Caption in QMenu
    By Lykurg in forum Newbie
    Replies: 4
    Last Post: 16th April 2014, 22:41
  2. Qmenu Stable close on QTableWidget
    By patrik08 in forum Qt Programming
    Replies: 1
    Last Post: 17th July 2006, 10:03
  3. Custom QHeaderView in Qt 4.1.1
    By Tair in forum Qt Programming
    Replies: 2
    Last Post: 11th July 2006, 09:44
  4. subclassing QHeaderView
    By chemstar in forum Qt Programming
    Replies: 7
    Last Post: 22nd May 2006, 12:34
  5. Replies: 2
    Last Post: 23rd February 2006, 16:38

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.