Results 1 to 4 of 4

Thread: How to use Checkboxes in QMenu?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to use Checkboxes in QMenu?

    Any reason you are not using a checkable action?

    Cheers,
    _

  2. #2
    Join Date
    Jun 2015
    Posts
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to use Checkboxes in QMenu?

    I already implement menu. I have problem with staying on top of the menu when I want to check more than one checkable action. When i use QCheckBox i dont have this problem. Do you know how to select multiple actions without hiding the menu?.

  3. #3
    Join Date
    Jun 2015
    Posts
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to use Checkboxes in QMenu?

    I fixed this problem.

    I used 2 signal&slots. One for non checkable action and second for checkable. The second one have menu->show() in slot to keep menu on top.


    Qt Code:
    1. void QSpreadsheetHeaderView::mousePressEvent ( QMouseEvent * event )
    2. {
    3. QHeaderView::mousePressEvent(event);
    4.  
    5. logicalIndex = logicalIndexAt(event->pos());
    6.  
    7. if (buttonMenuRect(logicalIndex).contains(event->pos())) {
    8.  
    9. if(set_checkbox==false)
    10. {
    11. menu = new QMenu(this);
    12.  
    13. sortAZ = menu->addAction("Sortuj A->Z");
    14. sortZA = menu->addAction("Sortuj Z->A");
    15. clear_sort = menu->addAction("Anuluj");
    16. menu->addSeparator();
    17. hideCol = menu->addAction("Ukryj kolumnÄ™");
    18. clear_hideCol = menu->addAction("Odkryj wszystkie");
    19.  
    20. for(int i = 0; i <model()->columnCount(); i++)
    21. {
    22. actions[i] = new QAction(menu);
    23. actions[i] = menu->addAction(model()->headerData(i, Qt::Horizontal).toString());
    24. actions[i]->setCheckable(true);
    25. connect(actions[i],SIGNAL(triggered(bool)),this,SLOT(hide_selected_column(bool)));
    26. if(i<columns_min)actions[i]->setChecked(true);
    27.  
    28. }
    29.  
    30. set_checkbox=true;
    31. connect(menu, SIGNAL(triggered(QAction*)), this, SLOT(action(QAction*)));
    32. }
    33.  
    34. // Disable hide column if only one column remains. Otherwise
    35. // the gui is no more available to show them back.
    36. hideCol->setEnabled(hiddenSectionCount() < count() - 1);
    37. if(hideCol->isEnabled()==false)
    38. {
    39. for(int i = 0; i <model()->columnCount(); i++)
    40. {
    41. if(actions[i]->isChecked()==true)
    42. {
    43. previous_sender=actions[i];
    44. freeze=true;
    45. actions[i]->setEnabled(false);
    46. break;
    47. }
    48. }
    49. }
    50.  
    51. menu->popup(mapToGlobal(event->pos()));
    52. }
    53.  
    54. // Catch previous arrow mouse click.
    55. else if (prevRect(logicalIndex).contains(event->pos()) && set_checkbox==true) {
    56. showSection(logicalIndex - 1);
    57. actions[logicalIndex - 1]->setChecked(true);
    58. updateSection(logicalIndex - 2);
    59. if(freeze==true) { previous_sender->setEnabled(true); freeze=false;}
    60. }
    61.  
    62. // Catch next arrow mouse click.
    63. else if (nextRect(logicalIndex).contains(event->pos()) && set_checkbox==true) {
    64. showSection(logicalIndex + 1);
    65. actions[logicalIndex + 1]->setChecked(true);
    66. updateSection(logicalIndex + 2);
    67. if(freeze==true) { previous_sender->setEnabled(true); freeze=false; }
    68. }
    69.  
    70.  
    71. }
    To copy to clipboard, switch view to plain text mode 

    Slot for non checkable:

    Qt Code:
    1. void QSpreadsheetHeaderView::action(QAction *action) {
    2.  
    3.  
    4. if (action == hideCol)
    5. {
    6. hideSection(logicalIndex);
    7. actions[logicalIndex]->setChecked(false);
    8. updateSection(logicalIndex-1);
    9. }
    10.  
    11. if (action == sortAZ)
    12. model()->sort(logicalIndex, Qt::AscendingOrder);
    13.  
    14. if (action == sortZA)
    15. model()->sort(logicalIndex, Qt::DescendingOrder);
    16.  
    17. if (action == clear_hideCol){
    18. for(int i = 0; i <model()->columnCount(); i++)
    19. {
    20. showSection(i);
    21. actions[i]->setChecked(true);
    22. actions[i]->setEnabled(true);
    23. if(freeze==true) { previous_sender->setEnabled(true); freeze=false;}
    24. }
    25. }
    26.  
    27. if (action == clear_sort){
    28.  
    29. }
    30.  
    31. }
    To copy to clipboard, switch view to plain text mode 

    and checkable:
    Qt Code:
    1. void QSpreadsheetHeaderView::hide_selected_column(bool checked)
    2. {
    3. menu->show();
    4. QAction * senderCHECK = qobject_cast<QAction *>(this->sender());
    5.  
    6. for(int i=0;i<23;i++)
    7. {
    8. if(senderCHECK==actions[i])
    9. {
    10. if(checked==false && freeze==false)
    11. {
    12. hideSection(i);
    13. actions[i]->setChecked(false);
    14. }
    15.  
    16. else if(checked==true && freeze==true)
    17. {
    18. hideCol->setEnabled(true);
    19. previous_sender->setEnabled(true);
    20. showSection(i);
    21. actions[i]->setChecked(true);
    22. freeze=false;
    23.  
    24. }
    25.  
    26. else if(checked==true)
    27. {
    28. showSection(i);
    29. actions[i]->setChecked(true);
    30. }
    31. }
    32. }
    33.  
    34. if(count()-hiddenSectionCount()==1)
    35. {
    36. freeze=true;
    37. hideCol->setEnabled(false);
    38. for(int i=0;i<23;i++)
    39. {
    40. if(actions[i]->isChecked()==true)
    41. {
    42. previous_sender=actions[i];
    43. actions[i]->setEnabled(false);
    44. break;
    45. }
    46. }
    47. }
    48. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. QTableWidget with checkboxes.
    By chris_helloworld in forum Qt Programming
    Replies: 0
    Last Post: 13th January 2011, 15:42
  2. checkboxes in a tableview,
    By mimmo_kallon in forum Newbie
    Replies: 3
    Last Post: 13th March 2008, 19:01
  3. QTableView and checkboxes
    By ibergmark in forum Qt Programming
    Replies: 3
    Last Post: 23rd February 2008, 15:20
  4. checkboxes
    By abrou in forum Newbie
    Replies: 2
    Last Post: 1st February 2008, 18:52
  5. Checkboxes in QAbstractITemModel
    By Valheru in forum Qt Programming
    Replies: 5
    Last Post: 28th November 2007, 20:23

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
  •  
Qt is a trademark of The Qt Company.