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

Simpliest way for creating contextMenu for QTableWidget cells

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