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. QTreeView Context Menus
Forum Updated to NodeBB v4.3 + New Features

QTreeView Context Menus

Scheduled Pinned Locked Moved Unsolved General and Desktop
qmenuqtreeview
2 Posts 2 Posters 3.7k 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.
  • S Offline
    S Offline
    Suths
    wrote on 23 Oct 2015, 12:37 last edited by
    #1

    Hi All,

    I'm having some issues with context menus in QTreeViews, where i can't explain why an action in a menu appears to be disabled. The menu action 'colour' apears but it can't be selected, and no signal is transmitted.

    TreeView Constructor:

    ui->ui_treeView->setContextMenuPolicy(Qt::CustomContextMenu);
           connect(ui->ui_treeView, SIGNAL(customContextMenuRequested(QPoint)),
                       this, SLOT(ContextMenuRequested(QPoint)));
    

    TreeView ContextMenuRequested function:

    void TreeView::ContextMenuRequested(const QPoint &pos)
    {
      QModelIndex index = ui->ui_treeView->indexAt(pos);
    
      if(index.isValid())
        {
          
           IElement* genericelement = static_cast<IElement*>(index.internalPointer());
           IModelElement* modelElement = dynamic_cast<IModelElement*>(genericelement);
    
            if(modelElement)
              {
                QMenu* menu = modelElement->getContextMenu();
    
                if(menu)
                  {
                    menu->popup(ui->ui_treeView->viewport()->mapToGlobal(pos));
                  }
              } 
        }
    }
    

    ModelElement Constructor:

    m_contextMenu = new QMenu();
      m_colourAction = new QAction(tr("Colour"),this);
    
      connect(m_colourAction,SIGNAL(triggered(bool)),this,SLOT(context_Colour()));
      m_contextMenu->addAction(m_colourAction);
    
    QMenu *IModelElement::getContextMenu()
    {
      return m_contextMenu;
    }
    
    void IModelElement::context_Colour()
    {
      qDebug() << this->getName() << " changing colour";
    }
    

    As you can see from the above code extracts the menu is constructed and the signal connected for each model element, and each element supplies it's own menu to the view (so they can be customized), and each has it's own action connected to a function which should just provide the debug statement. There are no signal mapping error shown in the program output, suggesting the signals are mapped correctly.

    Does anyone have any ideas?

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 23 Oct 2015, 22:46 last edited by
      #2

      Hi,

      Good question but I haven't had the use case. One thing that looks "incorrect" is that you don't add your action to the menu the usual way:

      m_colourAction = m_contextMenu->addAction(tr("Colour"));
      

      If not, then you should also make the menu the parent of your QAction rather than your IModelElement.

      Out of curiosity, why the two casts one after the other ? You should also rather use qobject_cast since your IModelElement is also a QObject.

      On a side note, did you take into account the memory consumption of your model ? You are creating as many menus and actions (and also QObjects) as you have elements in your model for a functionality that doesn't really demand it.

      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

      1/2

      23 Oct 2015, 12:37

      • Login

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