Qt 5.10 Book Examples
ContextMenu.h
Go to the documentation of this file.
1 // ======================================================================
2 // ContextMenu.h
3 // ======================================================================
4 // This file is a part of the book
5 // "Qt 5.10 Professional programming with C++"
6 // http://qt-book.com
7 // ======================================================================
8 // Copyright (c) 2017 by Max Schlee
9 // ======================================================================
10 
11 #pragma once
12 
13 #include <QtWidgets>
14 
15 // ======================================================================
16 class ContextMenu : public QTextEdit {
17 Q_OBJECT
18 private:
19  QMenu* m_pmnu;
20 
21 protected:
22  virtual void contextMenuEvent(QContextMenuEvent* pe)
23  {
24  m_pmnu->exec(pe->globalPos());
25  }
26 
27 public:
28  ContextMenu(QWidget* pwgt = 0)
29  : QTextEdit(pwgt)
30  {
31  setReadOnly(true);
32  m_pmnu = new QMenu(this);
33  m_pmnu->addAction("Red");
34  m_pmnu->addAction("Green");
35  m_pmnu->addAction("Blue");
36  connect(m_pmnu,
37  SIGNAL(triggered(QAction*)),
38  SLOT(slotActivated(QAction*))
39  );
40  }
41 
42 public slots:
43  void slotActivated(QAction* pAction)
44  {
45  QString strColor = pAction->text().remove("&");
46 
47  setHtml(QString("<BODY BGCOLOR=%1></BODY>").arg(strColor));
48  }
49 };
50 
ContextMenu(QWidget *pwgt=0)
Definition: ContextMenu.h:28
void slotActivated(QAction *pAction)
Definition: ContextMenu.h:43
virtual void contextMenuEvent(QContextMenuEvent *pe)
Definition: ContextMenu.h:22