Results 1 to 5 of 5

Thread: contextmenu in QDockWidget title

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Oct 2007
    Posts
    78
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    1
    Thanked 3 Times in 3 Posts

    Default Re: contextmenu in QDockWidget title

    I believe if you are going to subclass you should not be declaring
    Qt Code:
    1. private:
    2. void contextMenuEvent(QContextMenuEvent *e);
    To copy to clipboard, switch view to plain text mode 

    but instead
    Qt Code:
    1. protected:
    2. void contextMenuEvent(QContextMenuEvent *e);
    To copy to clipboard, switch view to plain text mode 

    And why are you parenting your QAction to the QMainWindow and not the QDockWidget?

    Not sure but floating the widget and having the actions parents set to mainwindow could be causing problems.

    Bob

  2. The following user says thank you to coderbob for this useful post:

    klipko (13th March 2008)

  3. #2
    Join Date
    Oct 2007
    Posts
    5
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    2

    Default Re: contextmenu in QDockWidget title

    I don't know why I parented the actions on mainwindow. Think it was an over site.

    I sat down and made a few changes after re-re-reviewing the documentation and got something working for the most part. I still can get it into the mode were it will not display the menu when floating which is not good at all... but it works. The ultimate goal is to have the menu show when the user right clicks the mouse when over the title bar (docked or floating).


    Qt Code:
    1. #ifndef VIEW_H
    2. #define VIEW_H
    3.  
    4. #include <QDockWidget>
    5. class QMenu;
    6. class QAction;
    7.  
    8. class MainWindow;
    9.  
    10. class View : public QDockWidget
    11. {
    12. Q_OBJECT;
    13.  
    14. public:
    15. View(MainWindow * mainWindow, std::string const & title);
    16. virtual ~View(void);
    17.  
    18. private slots:
    19. void onFloat();
    20. void onDock();
    21. void contextMenu(QPoint const & point);
    22.  
    23. private:
    24. QMenu *m_actionMenu;
    25.  
    26. QAction *m_actionFloat;
    27. QAction *m_actionDock;
    28.  
    29. };
    30.  
    31. #endif
    To copy to clipboard, switch view to plain text mode 


    Qt Code:
    1. #include "View.h"
    2.  
    3. #include "MainWindow.h"
    4.  
    5. #include <QMenu>
    6. #include <QAction>
    7. #include <QContextMenuEvent>
    8.  
    9. View::View(MainWindow * mainWindow, std::string const & title)
    10. : QDockWidget(title.c_str(), mainWindow, 0)
    11. {
    12. std::string group = "gui/dock/";
    13. group += windowTitle().toStdString();
    14.  
    15. setObjectName(windowTitle() + "_view");
    16.  
    17. m_actionMenu = new QMenu();
    18.  
    19. m_actionFloat = new QAction(tr("Floating"), this);
    20. m_actionFloat->setCheckable(true);
    21. connect(m_actionFloat, SIGNAL(triggered()), this, SLOT(onFloat()));
    22. m_actionMenu->addAction(m_actionFloat);
    23.  
    24. m_actionDock = new QAction(tr("Dock"), this);
    25. m_actionDock->setCheckable(true);
    26. connect(m_actionDock, SIGNAL(triggered()), this, SLOT(onDock()));
    27. m_actionMenu->addAction(m_actionDock);
    28.  
    29. setContextMenuPolicy(Qt::CustomContextMenu);
    30. connect(this, SIGNAL(customContextMenuRequested(QPoint const &)),
    31. this, SLOT(contextMenu(QPoint const &)));
    32. }
    33.  
    34. View::~View(void)
    35. {
    36. std::string group = "gui/dock/";
    37. group += windowTitle().toStdString();
    38. }
    39.  
    40. void View::contextMenu(QPoint const & point)
    41. {
    42. m_actionMenu->exec(mapToGlobal(point));
    43. }
    44.  
    45. void View::onFloat()
    46. {
    47. setFloating(true);
    48. setFeatures(QDockWidget::DockWidgetFloatable);
    49. setAllowedAreas(Qt::NoDockWidgetArea);
    50. m_actionFloat->setChecked(true);
    51. m_actionDock->setChecked(false);
    52. }
    53.  
    54. void View::onDock()
    55. {
    56. setFeatures(QDockWidget::AllDockWidgetFeatures);
    57. setAllowedAreas(Qt::AllDockWidgetAreas);
    58. m_actionFloat->setChecked(false);
    59. m_actionDock->setChecked(true);
    60. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by klipko; 7th March 2008 at 22:36. Reason: missing [code] tags

Similar Threads

  1. No restore button in QDockWidget title bar
    By trskel in forum Qt Programming
    Replies: 1
    Last Post: 21st September 2007, 09:59

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.