Custom menu on QTextEdit inherited class



  • 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)));
    
    }
    


  • 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. ... "



  • 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)



  • 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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.