TreeView - Context menu



  • I'm using Qt 5.1.1's Desktop controls. I need to be able to do a context menu on a selected row in the TableView.

    I looked into "clicked" and "activated" signals but they don't provide which mouse button was pressed. Any ideas?

    Thanks
    -Atif



  • First Create Actions in you application UI. Then for your TreeView Widget, use setContextMenuPolicy(Qt::ActionsContextMenu); In your constructor, assign those actions to your Widget with ui->tableWidget->addAction(ui->actionNew);



  • Your title is a bit misleading as it refers to TableView.

    There are two ways to achieve this:

    @ TableView {
    id: table
    anchors.fill: parent
    model: 100
    TableViewColumn { title: "First" }
    MouseArea {
    id: mouse
    acceptedButtons: Qt.RightButton
    anchors.fill: parent
    onClicked: menu.popup()
    Menu { id: menu ; MenuItem { text: "Popup at row " + table.rowAt(mouse.mouseX, mouse.mouseY) } }
    }
    }
    @
    Alternatively you can set a custom itemDelegate and place the menu in there.


Log in to reply
 

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