Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. PySide2 context menu not showing for QTableView
Forum Updated to NodeBB v4.3 + New Features

PySide2 context menu not showing for QTableView

Scheduled Pinned Locked Moved Solved Qt for Python
12 Posts 3 Posters 3.0k 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.
  • jsulmJ jsulm

    @adutzu89 I think the problem is that popup() is non blocking call (I'm actually not 100% sure), so your menu simply goes out of scope and is deleted. Try to use exec() instead of popup().

    A Offline
    A Offline
    adutzu89
    wrote on last edited by
    #3

    Hi @jsulm ,
    Thanks for taking your time to help, I tried your solution and while it doesn't show the menu I can only interact with the view on the second click.
    Guessing it opens the menu, but it isn't visible, somehow.

    jsulmJ 1 Reply Last reply
    0
    • A adutzu89

      Hi @jsulm ,
      Thanks for taking your time to help, I tried your solution and while it doesn't show the menu I can only interact with the view on the second click.
      Guessing it opens the menu, but it isn't visible, somehow.

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #4

      @adutzu89 Did you try to show it without providing coordinates? Just to see whether it is related?

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      A 2 Replies Last reply
      0
      • jsulmJ jsulm

        @adutzu89 Did you try to show it without providing coordinates? Just to see whether it is related?

        A Offline
        A Offline
        adutzu89
        wrote on last edited by adutzu89
        #5

        @jsulm same thing, I also tried to check isVisible() and returns true. But I am looking on entire screen and nothing.
        I forgot to mention I am working on KDE Neon, which is based on Ubuntu 18.04. I will try and fire up other machines and see the result on them, to see if it's a Linux/window manager problem.

        Will return shortly with result.

        1 Reply Last reply
        0
        • jsulmJ jsulm

          @adutzu89 Did you try to show it without providing coordinates? Just to see whether it is related?

          A Offline
          A Offline
          adutzu89
          wrote on last edited by
          #6

          @jsulm so I tried on MacOS and has the same issue.

          On Windows 10 I cannot install PySide2, I get the following error:

          ERROR: Could not find a version that satisfies the requirement pyside2 (from versions: none)
          ERROR: No matching distribution found for pyside2
          

          I get the error when trying to install using pip:

          pip install --index-url=https://download.qt.io/official_releases/QtForPython/ pyside2
          

          and

          pip install PySide2
          

          Tried with Python2 and Python3.8, tried a couple of solutions.
          Will try to find a solution in a few hours.

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #7

            Hi,

            Can you provide a minimal complete code sample that reproduces that behaviour ?

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

            A 1 Reply Last reply
            0
            • SGaistS SGaist

              Hi,

              Can you provide a minimal complete code sample that reproduces that behaviour ?

              A Offline
              A Offline
              adutzu89
              wrote on last edited by
              #8

              Hi @SGaist ,
              Thanks for joining the help :D.
              So I tried to minimize the code as much as I could, didn't trimmed much from the imports though:

              # This Python file uses the following encoding: utf-8
              import sys
              from PySide2.QtWidgets import QApplication, QMainWindow, QWidget, QPushButton, QVBoxLayout, QLabel, QTableView
              from PySide2.QtWidgets import QHBoxLayout, QGroupBox, QHeaderView, QDialog, QLineEdit, QMessageBox, QAbstractItemView
              from PySide2.QtWidgets import QMenu, QAction
              from PySide2.QtCore import QSettings, Qt, Signal, Slot, QModelIndex
              from PySide2.QtGui import QStandardItemModel, QStandardItem
              
              
              class MainWindow(QMainWindow):
                  closeDialog = Signal()
                  def __init__(self):
                      QMainWindow.__init__(self)
                      self.dbView = QTableView(self)
                      self.dbView.setContextMenuPolicy(Qt.CustomContextMenu)
                      self.dbView.customContextMenuRequested.connect(self.showContextMenu)
                      self.dbView.verticalHeader().hide()
                      self.dbViewContextMenu = QMenu()
                      self.dbViewContextMenu.addAction(QAction("Sterge"))
              
                      mainWidget = QWidget(self)
                      mainLayout = QVBoxLayout()
                      mainWidget.setLayout(mainLayout)
                      self.addItems(mainLayout)
                      self.setCentralWidget(mainWidget)
                      self.listDiverse(self.dbView)
              
                  def addItems(self, layout):
                      label = QLabel(self)
                      label.setText("Test pentru PostgreSQL cu Qt for Python")
                      layout.addWidget(label)
                      label.show()
                      layout.addWidget(self.dbView)
                      self.dbView.show()
              
                  def closeEvent(self, event):
                      settings = QSettings("SDC", "PGExample")
                      settings.setValue("geometry", self.saveGeometry())
                      settings.setValue("windowState", self.saveState())
                      QMainWindow.closeEvent(self, event)
              
                  def readSettings(self):
                      settings = QSettings("SDC", "PGExample")
                      self.restoreGeometry(settings.value("geometry"))
                      self.restoreState(settings.value("windowState"))
              
                  def listDiverse(self, view):
                      model = QStandardItemModel (4, 4)
                      for row in range(4):
                          for column in range(4):
                              item = QStandardItem("row %d, column %d" % (row, column))
                              model.setItem(row, column, item)
                      view.setModel(model)
                      view.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
                      view.setSelectionBehavior(QAbstractItemView.SelectRows)
              
              
                  def showContextMenu(self, pos):
                      print("pos " + str(pos))
                      index = self.dbView.indexAt(pos)
              #        self.dbViewContextMenu.popup(self.dbView.viewport().mapToGlobal(pos))
                      self.dbViewContextMenu.exec_(self.dbView.viewport().mapToGlobal(pos))
                      print("isVisible " + str(self.dbViewContextMenu.isVisible()))
              
              
              if __name__ == "__main__":
                  app = QApplication([])
                  window = MainWindow()
                  window.show()
                  window.resize(800,600)
                  window.readSettings()
                  sys.exit(app.exec_())
              
              

              Managed to finally install PySide2 on Windows 10 x64, with Python3.7 x86.

              1 Reply Last reply
              0
              • A Offline
                A Offline
                adutzu89
                wrote on last edited by adutzu89
                #9

                @jsulm , @SGaist I tested that sample code in Windows 10 and I experience the same issue.
                The menu is there, but not showing up on the screen.

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #10

                  An educated guess: your action has no parent and is destroyed early.

                  Change self.dbViewContextMenu.addAction(QAction("Sterge")) to either:

                  • self.dbViewContextMenu.addAction("Sterge")
                    or
                  • self.dbViewContextMenu.addAction(QAction("Sterge", self))

                  Don't forget to connect the created action to some slot to do something.

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

                  A 1 Reply Last reply
                  2
                  • SGaistS SGaist

                    An educated guess: your action has no parent and is destroyed early.

                    Change self.dbViewContextMenu.addAction(QAction("Sterge")) to either:

                    • self.dbViewContextMenu.addAction("Sterge")
                      or
                    • self.dbViewContextMenu.addAction(QAction("Sterge", self))

                    Don't forget to connect the created action to some slot to do something.

                    A Offline
                    A Offline
                    adutzu89
                    wrote on last edited by
                    #11

                    @SGaist said in PySide2 context menu not showing for QTableView:

                    An educated guess: your action has no parent and is destroyed early.

                    Change self.dbViewContextMenu.addAction(QAction("Sterge")) to either:

                    • self.dbViewContextMenu.addAction("Sterge")
                      or
                    • self.dbViewContextMenu.addAction(QAction("Sterge", self))

                    Don't forget to connect the created action to some slot to do something.

                    It worked, cheers!
                    Yes, made to QAction("Sterge") for testing purposes, but since I couldn't manage to make it work it didn't made sense to try and make a functional action.

                    Thanks a lot for all your help.

                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #12

                      You're welcome !

                      Since you have it working now, please mark the thread as solved using the "Topic Tools" button so that other forum users may know a solution has been found :)

                      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

                      • Login

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