Results 1 to 5 of 5

Thread: contextmenu in QDockWidget title

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

    Default contextmenu in QDockWidget title

    I'm working on implementing floating / docking behavior for QDockWidget but when it's floating contextMenuEvent is no longer received so now the user can't right and select Dockable option. QDockWidget is subclassed and from there three different dock widgets inherit from that. The handling of contextMenuEvent is in the QDockWidget subclass.

    Any ideas why this is happening? My goal is to implement something similar to Visual Studio were the user can right click on the title to Float or Dock the window. Side question: Is it possible to have contextMenu work on the title of a dockwidget?

    Thank you

    Using QT: 4.3.2

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

    Default Re: contextmenu in QDockWidget title

    I tested some code myself and it still uses the context menu when floating. Are you sure you are using a contextmenu from the dockwidget and not inheriting one from the mainwindow? When the dock is floating it will lose the one from the mainwindow since it is no longer the parent.

    Can you post some code?

    Bob
    Last edited by coderbob; 4th March 2008 at 13:02. Reason: Original comment was incorrect

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

    Default Re: contextmenu in QDockWidget title

    He is some code:

    - MyDockWidget.h

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

    -- MyDockWidget.cpp

    Qt Code:
    1. #include "MyDockWidget.h"
    2. #include "MainWindow.h"
    3.  
    4. #include <QSettings>
    5. #include <QContextMenuEvent>
    6. #include <QMenu>
    7. #include <QAction>
    8.  
    9.  
    10. MyDockWidget::MyDockWidget(MainWindow * mainWindow, std::string const & title)
    11. : QDockWidget(title.c_str(), mainWindow, 0)
    12. {
    13. std::string group = "gui/dock/";
    14. group += windowTitle().toStdString();
    15.  
    16. setObjectName(windowTitle() + "_view");
    17.  
    18. m_actionMenu = new QMenu();
    19.  
    20. m_actionFloat = new QAction(tr("Floating"), mainWindow);
    21. m_actionFloat->setCheckable(true);
    22. connect(m_actionFloat, SIGNAL(triggered()), this, SLOT(onFloat()));
    23. m_actionMenu->addAction(m_actionFloat);
    24.  
    25. m_actionDock = new QAction(tr("Dock"), mainWindow);
    26. m_actionDock->setCheckable(true);
    27. connect(m_actionDock, SIGNAL(triggered()), this, SLOT(onDock()));
    28. m_actionMenu->addAction(m_actionDock);
    29. }
    30.  
    31. MyDockWidget::~MyDockWidget(void)
    32. {
    33. }
    34.  
    35. void MyDockWidget::contextMenuEvent(QContextMenuEvent *e)
    36. {
    37. m_actionMenu->exec(e->globalPos());
    38. }
    39.  
    40. void MyDockWidget::onFloat()
    41. {
    42. setFloating(true);
    43. setAllowedAreas(Qt::NoDockWidgetArea);
    44. m_actionFloat->setChecked(true);
    45. m_actionDock->setChecked(false);
    46. }
    47.  
    48. void MyDockWidget::onDock()
    49. {
    50. setAllowedAreas(Qt::AllDockWidgetAreas);
    51. m_actionFloat->setChecked(false);
    52. m_actionDock->setChecked(true);
    53. }
    To copy to clipboard, switch view to plain text mode 
    All DockWidgets in the application inherit from MyDockWidget.

    coderbob - You mention that when the dock is floating it will lose the contextMenuEvent from the parent. Does this mean I'll need to put in a contextMenuEvent in each class the inherits from MyDockWidget?
    Last edited by jpn; 4th March 2008 at 20:29. Reason: missing [code] tags

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

    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

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

    klipko (13th March 2008)

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

    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 23: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, 10: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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.