Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Custom menu on QTextEdit inherited class
QtWS25 Last Chance

Custom menu on QTextEdit inherited class

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 2.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    LauraB
    wrote on last edited by
    #1

    I've created a class that inherits from QTextEdit (public inheritance).

    In the constructor, I would like to link the signal customContextMenuRequested with a slot of mine so I can custom the right click menu.

    I wrote this :

    this->setContextMenuPolicy(Qt::CustomContextMenu);
    connect(this, SIGNAL(customContextMenuRequested(const QPoint &pos)), this, SLOT(onRightClick(const QPoint &pos)));
    

    At run-time, I get this error :

    QObject::connect: No such signal COwnTextEdit::customContextMenuRequested(const QPoint &pos)
    

    I've tried to look around and the two main reasons it's usually not working are : setContextMenu(Qt::CustomContextMenu) forgotten OR Macro Q_OBJECT forgotten

    I'm not fallin in those pitfalls currently, and I have no clue why this is not working.

    I've tried this as well :

    connect(this, SIGNAL(QTextEdit::customContextMenuRequested(const QPoint &pos)), this, SLOT(onRightClick(const QPoint &pos)));
    

    But it doesn't work either.

    Thanks a lot

    Class declaration

    #ifndef MYTEXTEDIT_H
    #define MYTEXTEDIT_H
    
    #include <QWidget>
    #include <QTextEdit>
    
    using namespace std;
    
    class MyTextEdit : public QTextEdit
    {
        Q_OBJECT
    
    public:
    
        explicit MyTextEdit(bool displayLines, QWidget *_parent = 0);
    
        ~MyTextEdit();
    signals:
    public slots:
        void onRightClick(const QPoint &point);
    
    private slots:
        void highlightCurrentLine();
    
    };
    
    #endif // MYTEXTEDIT_H
    

    And I don't think I have to declare the signal customContextMenuRequested as it's already declared in QWidget : http://doc.qt.io/qt-5/qwidget.html#customContextMenuRequested

    The constructor :

    MyTextEdit::MyTextEdit(bool displayLines, QWidget *_parent) :
        QTextEdit(_parent)
    {
        highlightCurrentLine();
        this->setContextMenuPolicy(Qt::CustomContextMenu);
        // I Also tried
        //connect(this, SIGNAL(QWidget::customContextMenuRequested(const QPoint &pos)), this, SLOT(onRightClick(const QPoint &pos)));
        //connect(this, SIGNAL(QTextEdit::customContextMenuRequested(const QPoint &pos)), this, SLOT(onRightClick(const QPoint &pos)));
    
        connect(this, SIGNAL(customContextMenuRequested(const QPoint &pos)), this, SLOT(onRightClick(const QPoint &pos)));
    
    }
    
    1 Reply Last reply
    0
    • P Offline
      P Offline
      Peer Schneider
      wrote on last edited by Peer Schneider
      #2

      Customizing a context menu for a QTextEdit is done by reimplementing the createStandardContextMenu() method of the QTextEdit. This method is virtual and therefore your reimplemented version gets called.

      See the documentation to QTextEdit::contextMenuEvent: " ... If you want to customize the context menu, reimplement this function. If you want to extend the standard context menu, reimplement this function, call createStandardContextMenu() and extend the menu returned. ... "

      1 Reply Last reply
      2
      • VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by
        #3

        could you try the Qt5 connection syntax?
        connect(this, &MyTextEdit::customContextMenuRequested, this, &MyTextEdit::onRightClick);

        In general, btw, you should not specify const refgerence inside SIGNAL and SLOT macros, you just slow down the connect process as the will be converted into the by-value version (e.g. const QPoint &pos will be converted to QPoint)

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        1 Reply Last reply
        2
        • L Offline
          L Offline
          LauraB
          wrote on last edited by
          #4

          Both your answers are really helpful and helped me a great deal.
          I wasnt familiar with that qt5 syntax for connect, and I didn't notice about reimplementing contextMenuEvent.

          Really, thank you guys

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved