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. Qt Context Menu Text Edit Within QTreeWidgetItem
Forum Updated to NodeBB v4.3 + New Features

Qt Context Menu Text Edit Within QTreeWidgetItem

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 474 Views 2 Watching
  • 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.
  • Q Offline
    Q Offline
    QTAsker
    wrote on 17 Jun 2021, 21:50 last edited by QTAsker
    #1

    I'm trying to figure out the best way to change the standard context menu of a text edit. For instance, I have a QTreeWidget and when I edit a QTreeItem I am able to change the text data of that QTreeItem. If I right click while editing that text a standard context menu pops up, how do I change this standard context menu of the text edit?

    In general I can use,

    connect( m_treeWgtItem, SIGNAL( customContextMenuRequested(const & QPoint) ), m_treeWidget, SLOT( OnContextMenu( const & QPoint) );

    to set the context menu of the QTreeWidgetItem, but what signal do I use to set the menu of the text edit of said item?

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 18 Jun 2021, 20:33 last edited by
      #2

      Hi and welcome to devnet,

      You can create your own subclass of the editor in order to reimplement contextMenuEvent.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • V Offline
        V Offline
        VRonin
        wrote on 18 Jun 2021, 21:28 last edited by VRonin
        #3
        class ContextMenuDelegate : public QStyledItemDelegate {
        Q_OBJECT
        Q_DISABLE_COPY_MOVE(ContextMenuDelegate)
        public:
        using QStyledItemDelegate::QStyledItemDelegate;
        QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override{
        QWidget* result = QStyledItemDelegate::createEditor(parent,option,index);
        if(result){
        result->setContextMenuPolicy(Qt::CustomContextMenu);
        connect(result,&QWidget::customContextMenuRequested,this,&ContextMenuDelegate::customContextMenuRequested);
        }
        return result;
        }
        Q_SIGNALS:
        void customContextMenuRequested(const QPoint &pos);
        };
        

        To use it:

        contextDelegate = new ContextMenuDelegate(this);
        treeWidget->setItemDelegate(contextDelegate);
        connect(contextDelegate,&ContextMenuDelegate::customContextMenuRequested,this,&MyClass::onContextMenu);
        

        You can create your own subclass of the editor

        Valid but you would still need a delegate to create this subclass so might as well just subclass the delegate

        "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
        3
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 19 Jun 2021, 19:12 last edited by
          #4

          @VRonin said in Qt Context Menu Text Edit Within QTreeWidgetItem:

          Valid but you would still need a delegate to create this subclass so might as well just subclass the delegate

          Agreed, I was mainly thinking about keeping the customization together with the editor itself.

          Your delegate is quite a good solution :-)

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          1

          1/4

          17 Jun 2021, 21:50

          • Login

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