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. Simpliest way for creating contextMenu for QTableWidget cells
Forum Updated to NodeBB v4.3 + New Features

Simpliest way for creating contextMenu for QTableWidget cells

Scheduled Pinned Locked Moved Solved General and Desktop
17 Posts 5 Posters 14.5k Views 1 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.
  • SGaistS SGaist

    Hi,

    What about customContextMenuRequested ?

    You can get the model index using indexAt.

    EngelardE Offline
    EngelardE Offline
    Engelard
    wrote on last edited by
    #4

    @SGaist said in Simpliest way for creating contextMenu for QTbaleWidget cells:

    Hi,

    You can get the model index using indexAt.

    How can i use this function? I can't use it directly because it's not static. Create variable of this QAbstractItemView i also can't because alot of errors and in google it seems like a pretty complicated stuff(advanced Qt).

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

      Why would you need it as static ?

      You have QTableWidget instance (e.g. _myTableWidget), then connect its customContextMenuRequested signal to some slots and in that slot call indexAt.

      Something along the lines:

      MainWindow(QWidget *parent)
          QWidget(parent)
      {
          _myTableWidget = new QTableWidget;
          // layout stuff etc.
          connect(_myTableWidget, &QTableWidget::customContextMenuRequested, this, &handleContextMenu);
      }
      
      void MainWindow::handleContextMenu(const QPoint& pos)
      {
          QTableWidgetItem *item = _myTableWidget->itemAt(pos);
          if (item) {
              // do what you want with the item.
          }
      }
      

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

      EngelardE 1 Reply Last reply
      3
      • Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #6

        @SGaist said in Simpliest way for creating contextMenu for QTableWidget cells:

        QTableWidgetItem *item = _myTableWidget->itemAt(pos);

        Imo there is a small mapping needed:
        The exception to this rule is QAbstractScrollArea and its subclasses that map the context menu event to coordinates of the viewport().

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        1 Reply Last reply
        1
        • SGaistS SGaist

          Why would you need it as static ?

          You have QTableWidget instance (e.g. _myTableWidget), then connect its customContextMenuRequested signal to some slots and in that slot call indexAt.

          Something along the lines:

          MainWindow(QWidget *parent)
              QWidget(parent)
          {
              _myTableWidget = new QTableWidget;
              // layout stuff etc.
              connect(_myTableWidget, &QTableWidget::customContextMenuRequested, this, &handleContextMenu);
          }
          
          void MainWindow::handleContextMenu(const QPoint& pos)
          {
              QTableWidgetItem *item = _myTableWidget->itemAt(pos);
              if (item) {
                  // do what you want with the item.
              }
          }
          
          EngelardE Offline
          EngelardE Offline
          Engelard
          wrote on last edited by Engelard
          #7

          @SGaist Ah now i get it. You misswrite in first post. Not indexAt but itemAt.

          P.S. there is no need to making connect, i just use all stuff inside customContextMenuRequested SLOT.

          P.P.S. everything seems to be well, compiles well, but not working i rly can't understand why:

          void MWindow::on_tableWidMemory_customContextMenuRequested(const QPoint &pos)
          {
              QTableWidgetItem *tempItem = ui->tableWidMemory->itemAt(pos);
          
              ui->tableWidMemory->item(tempItem->row(), tempItem->column())->setBackgroundColor(Qt::green);
          }
          

          Also tried:

          ui->tableWidMemory->itemAt(pos.x(), pos.y())->setBackgroundColor(Qt::green);
          

          Still nothing....

          jsulmJ 1 Reply Last reply
          0
          • EngelardE Engelard

            @SGaist Ah now i get it. You misswrite in first post. Not indexAt but itemAt.

            P.S. there is no need to making connect, i just use all stuff inside customContextMenuRequested SLOT.

            P.P.S. everything seems to be well, compiles well, but not working i rly can't understand why:

            void MWindow::on_tableWidMemory_customContextMenuRequested(const QPoint &pos)
            {
                QTableWidgetItem *tempItem = ui->tableWidMemory->itemAt(pos);
            
                ui->tableWidMemory->item(tempItem->row(), tempItem->column())->setBackgroundColor(Qt::green);
            }
            

            Also tried:

            ui->tableWidMemory->itemAt(pos.x(), pos.y())->setBackgroundColor(Qt::green);
            

            Still nothing....

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

            @Engelard I don't have a solution, but want to say that you're writing dangerous code: you're not checking whether tempItem is a valid pointer! If it is not your app will crash.
            Also, shouldn't it be http://doc.qt.io/qt-5/qtablewidgetitem.html#setBackground?

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

            EngelardE 1 Reply Last reply
            2
            • VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by
              #9

              You just need to set the menu policy: http://doc.qt.io/qt-5/qwidget.html#contextMenuPolicy-prop

              In MWindow constructor call setContextMenuPolicy(Qt::CustomContextMenu);

              Also note what @Christian-Ehrlicher said.
              Instead of ui->tableWidMemory->itemAt(pos); you'll probably need ui->tableWidMemory->itemAt(ui->tableWidMemory->mapFromGlobal(mapToGlobal(pos)));

              "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

              EngelardE 1 Reply Last reply
              2
              • jsulmJ jsulm

                @Engelard I don't have a solution, but want to say that you're writing dangerous code: you're not checking whether tempItem is a valid pointer! If it is not your app will crash.
                Also, shouldn't it be http://doc.qt.io/qt-5/qtablewidgetitem.html#setBackground?

                EngelardE Offline
                EngelardE Offline
                Engelard
                wrote on last edited by
                #10

                @jsulm said in Simpliest way for creating contextMenu for QTableWidget cells:

                Also, shouldn't it be http://doc.qt.io/qt-5/qtablewidgetitem.html#setBackground?

                Nope. setBackgroundColor is wroked well with leftClick slot.

                1 Reply Last reply
                0
                • VRoninV VRonin

                  You just need to set the menu policy: http://doc.qt.io/qt-5/qwidget.html#contextMenuPolicy-prop

                  In MWindow constructor call setContextMenuPolicy(Qt::CustomContextMenu);

                  Also note what @Christian-Ehrlicher said.
                  Instead of ui->tableWidMemory->itemAt(pos); you'll probably need ui->tableWidMemory->itemAt(ui->tableWidMemory->mapFromGlobal(mapToGlobal(pos)));

                  EngelardE Offline
                  EngelardE Offline
                  Engelard
                  wrote on last edited by
                  #11

                  @VRonin nothing changed, still not working..

                  VRoninV 1 Reply Last reply
                  0
                  • EngelardE Engelard

                    @VRonin nothing changed, still not working..

                    VRoninV Offline
                    VRoninV Offline
                    VRonin
                    wrote on last edited by VRonin
                    #12

                    @Engelard said in Simpliest way for creating contextMenu for QTableWidget cells:

                    nothing changed, still not working

                    Does on_tableWidMemory_customContextMenuRequested get executed at all?

                    Actually realised my mistake. You are asking the context menu only for the table widget so instead of setContextMenuPolicy(Qt::CustomContextMenu); you should have ui->tableWidMemory->setContextMenuPolicy(Qt::CustomContextMenu); and forget about the mapFromGlobal/mapToGlobal part

                    "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

                    EngelardE 1 Reply Last reply
                    1
                    • VRoninV VRonin

                      @Engelard said in Simpliest way for creating contextMenu for QTableWidget cells:

                      nothing changed, still not working

                      Does on_tableWidMemory_customContextMenuRequested get executed at all?

                      Actually realised my mistake. You are asking the context menu only for the table widget so instead of setContextMenuPolicy(Qt::CustomContextMenu); you should have ui->tableWidMemory->setContextMenuPolicy(Qt::CustomContextMenu); and forget about the mapFromGlobal/mapToGlobal part

                      EngelardE Offline
                      EngelardE Offline
                      Engelard
                      wrote on last edited by
                      #13

                      @VRonin said in Simpliest way for creating contextMenu for QTableWidget cells:

                      @Engelard said in Simpliest way for creating contextMenu for QTableWidget cells:

                      nothing changed, still not working

                      Does on_tableWidMemory_customContextMenuRequested get executed at all?

                      Lol, no it does'nt. But after your:

                      Actually realised my mistake. You are asking the context menu only for the table widget so instead of setContextMenuPolicy(Qt::CustomContextMenu); you should have ui->tableWidMemory->setContextMenuPolicy(Qt::CustomContextMenu); and forget about the mapFromGlobal/mapToGlobal part

                      It called, but whole app crashes)

                      jsulmJ 1 Reply Last reply
                      0
                      • EngelardE Offline
                        EngelardE Offline
                        Engelard
                        wrote on last edited by Engelard
                        #14

                        Now it's woring, i removed whole that stuff

                        QTableWidgetItem *tempItem = ui->tableWidMemory->itemAt(pos);
                        

                        The only thing is left now:

                        ui->tableWidMemory->itemAt(pos)->setBackgroundColor(Qt::green);
                        

                        And

                        ui->tableWidMemory->setContextMenuPolicy(Qt::CustomContextMenu);
                        

                        in constructor.

                        Tnx VRonin for help. Still can't get why no slot for rightclick for QTableWidget cells, would be way efficient and simplier for newcomers. But anyway, at least it working with two extra-lines of code.

                        1 Reply Last reply
                        0
                        • EngelardE Engelard

                          @VRonin said in Simpliest way for creating contextMenu for QTableWidget cells:

                          @Engelard said in Simpliest way for creating contextMenu for QTableWidget cells:

                          nothing changed, still not working

                          Does on_tableWidMemory_customContextMenuRequested get executed at all?

                          Lol, no it does'nt. But after your:

                          Actually realised my mistake. You are asking the context menu only for the table widget so instead of setContextMenuPolicy(Qt::CustomContextMenu); you should have ui->tableWidMemory->setContextMenuPolicy(Qt::CustomContextMenu); and forget about the mapFromGlobal/mapToGlobal part

                          It called, but whole app crashes)

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

                          @Engelard said in Simpliest way for creating contextMenu for QTableWidget cells:

                          It called, but whole app crashes)

                          Where do you execute this code? It should be after ui->setupUI

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

                          1 Reply Last reply
                          0
                          • EngelardE Offline
                            EngelardE Offline
                            Engelard
                            wrote on last edited by
                            #16

                            P.S. Ah. Now i get it, that new object

                            QTableWidgetItem *tempItem = ui->tableWidMemory->itemAt(pos);

                            needed for checking if user rightclick to anything in TableWidget but cells, it would crash without it.

                            jsulmJ 1 Reply Last reply
                            0
                            • EngelardE Engelard

                              P.S. Ah. Now i get it, that new object

                              QTableWidgetItem *tempItem = ui->tableWidMemory->itemAt(pos);

                              needed for checking if user rightclick to anything in TableWidget but cells, it would crash without it.

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

                              @Engelard I told you :-)

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

                              1 Reply Last reply
                              1
                              • JonBJ JonB referenced this topic on

                              • Login

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