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
QtWS25 Last Chance

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.
  • S SGaist
    23 Sept 2018, 20:40

    Hi,

    What about customContextMenuRequested ?

    You can get the model index using indexAt.

    E Offline
    E Offline
    Engelard
    wrote on 24 Sept 2018, 17:04 last edited by
    #3

    @SGaist That's what i'm talking about. I've tried to read this stuff, i rly can't get it, how it should work and how i'll get info about cell was rightclicked....

    1 Reply Last reply
    0
    • S SGaist
      23 Sept 2018, 20:40

      Hi,

      What about customContextMenuRequested ?

      You can get the model index using indexAt.

      E Offline
      E Offline
      Engelard
      wrote on 24 Sept 2018, 18:11 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
      • S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 24 Sept 2018, 19:47 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

        E 1 Reply Last reply 25 Sept 2018, 07:59
        3
        • C Offline
          C Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 25 Sept 2018, 04:38 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
          • S SGaist
            24 Sept 2018, 19:47

            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.
                }
            }
            
            E Offline
            E Offline
            Engelard
            wrote on 25 Sept 2018, 07:59 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....

            J 1 Reply Last reply 25 Sept 2018, 08:14
            0
            • E Engelard
              25 Sept 2018, 07:59

              @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....

              J Offline
              J Offline
              jsulm
              Lifetime Qt Champion
              wrote on 25 Sept 2018, 08:14 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

              E 1 Reply Last reply 25 Sept 2018, 08:36
              2
              • V Offline
                V Offline
                VRonin
                wrote on 25 Sept 2018, 08:15 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

                E 1 Reply Last reply 25 Sept 2018, 08:43
                2
                • J jsulm
                  25 Sept 2018, 08:14

                  @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?

                  E Offline
                  E Offline
                  Engelard
                  wrote on 25 Sept 2018, 08:36 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
                  • V VRonin
                    25 Sept 2018, 08:15

                    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)));

                    E Offline
                    E Offline
                    Engelard
                    wrote on 25 Sept 2018, 08:43 last edited by
                    #11

                    @VRonin nothing changed, still not working..

                    V 1 Reply Last reply 25 Sept 2018, 08:43
                    0
                    • E Engelard
                      25 Sept 2018, 08:43

                      @VRonin nothing changed, still not working..

                      V Offline
                      V Offline
                      VRonin
                      wrote on 25 Sept 2018, 08:43 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

                      E 1 Reply Last reply 25 Sept 2018, 08:47
                      1
                      • V VRonin
                        25 Sept 2018, 08:43

                        @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

                        E Offline
                        E Offline
                        Engelard
                        wrote on 25 Sept 2018, 08:47 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)

                        J 1 Reply Last reply 25 Sept 2018, 08:52
                        0
                        • E Offline
                          E Offline
                          Engelard
                          wrote on 25 Sept 2018, 08:52 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
                          • E Engelard
                            25 Sept 2018, 08:47

                            @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)

                            J Offline
                            J Offline
                            jsulm
                            Lifetime Qt Champion
                            wrote on 25 Sept 2018, 08:52 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
                            • E Offline
                              E Offline
                              Engelard
                              wrote on 25 Sept 2018, 09:02 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.

                              J 1 Reply Last reply 25 Sept 2018, 09:06
                              0
                              • E Engelard
                                25 Sept 2018, 09:02

                                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.

                                J Offline
                                J Offline
                                jsulm
                                Lifetime Qt Champion
                                wrote on 25 Sept 2018, 09:06 last edited by
                                #17

                                @Engelard I told you :-)

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

                                1 Reply Last reply
                                1
                                • J JonB referenced this topic on 22 Apr 2023, 16:20

                                12/17

                                25 Sept 2018, 08:43

                                • Login

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