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. tableContextMenu sender not work
QtWS25 Last Chance

tableContextMenu sender not work

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 4 Posters 670 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.
  • L Offline
    L Offline
    LCorona
    wrote on last edited by
    #1

    Have a tableContextMenu in a tableWidget but depends of the cell clicked, the actions displayed on the context menu will be different, for identifying de cell clicked the sender is not work for me

    void MainWindow::tableContextMenu(QPoint pos)
    {
        QMenu * menu = new QMenu(this);
        QWidget *widget = qobject_cast<QWidget*>(sender());
        QAction * editColor = new QAction(("Change Color"), this);
        QAction * editChannel = new QAction();
        QWidget *it= ui->channelsTable->cellWidget(32,1);
    
        if(widget == it)
        {
            editChannel->setText("Descombine channels");
        }
        else
        {
            editChannel->setText("Combine channels");
        }
    
        editChannel->setParent(this);
        connect(editColor, SIGNAL(triggered()), this, SLOT(slotEditColor()));
        connect(editChannel, SIGNAL(triggered()), this, SLOT(combineChannelManager()));
        menu->addAction(editColor);
        menu->addAction(editChannel);
        menu->popup(ui->channelsTable->viewport()->mapToGlobal(pos));
    }
    

    Never obtain the Descombine channel text in context menu. What is wrong?

    artwawA 1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      How do you connect this slot? Hopefully from the cellWidget - otherwise sender() can never be the cell widget.

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

      L 1 Reply Last reply
      0
      • Christian EhrlicherC Christian Ehrlicher

        How do you connect this slot? Hopefully from the cellWidget - otherwise sender() can never be the cell widget.

        L Offline
        L Offline
        LCorona
        wrote on last edited by
        #3

        @Christian-Ehrlicher

        In MainWindow constructor

        connect(ui->channelsTable, SIGNAL(customContextMenuRequested(QPoint)), this,SLOT(tableContextMenu(QPoint)));
        

        This already works, but later I need to add a matter of show a different menu option only when the cell (32,1) is right cliked in the tablewidget

        1 Reply Last reply
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          So how should sender ever be the cell widget then?
          You can find the cell under the mouse cursor with QTableWidget::itemAt().

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

          L 1 Reply Last reply
          2
          • Christian EhrlicherC Christian Ehrlicher

            So how should sender ever be the cell widget then?
            You can find the cell under the mouse cursor with QTableWidget::itemAt().

            L Offline
            L Offline
            LCorona
            wrote on last edited by
            #5

            @Christian-Ehrlicher
            I assume that sender is anything that trigger something including the context menu, but whatever, the QTableWidget::itemAt(). works fine and is easier to implement and use.
            Thanks. Problem resolved.

            Christian EhrlicherC 1 Reply Last reply
            0
            • L LCorona

              Have a tableContextMenu in a tableWidget but depends of the cell clicked, the actions displayed on the context menu will be different, for identifying de cell clicked the sender is not work for me

              void MainWindow::tableContextMenu(QPoint pos)
              {
                  QMenu * menu = new QMenu(this);
                  QWidget *widget = qobject_cast<QWidget*>(sender());
                  QAction * editColor = new QAction(("Change Color"), this);
                  QAction * editChannel = new QAction();
                  QWidget *it= ui->channelsTable->cellWidget(32,1);
              
                  if(widget == it)
                  {
                      editChannel->setText("Descombine channels");
                  }
                  else
                  {
                      editChannel->setText("Combine channels");
                  }
              
                  editChannel->setParent(this);
                  connect(editColor, SIGNAL(triggered()), this, SLOT(slotEditColor()));
                  connect(editChannel, SIGNAL(triggered()), this, SLOT(combineChannelManager()));
                  menu->addAction(editColor);
                  menu->addAction(editChannel);
                  menu->popup(ui->channelsTable->viewport()->mapToGlobal(pos));
              }
              

              Never obtain the Descombine channel text in context menu. What is wrong?

              artwawA Offline
              artwawA Offline
              artwaw
              wrote on last edited by
              #6

              @LCorona Apart from @Christian-Ehrlicher said, there is also that little things that contextMenuPolicy must be set to Qt::CustomContextMenu.

              For more information please re-read.

              Kind Regards,
              Artur

              1 Reply Last reply
              0
              • L LCorona

                @Christian-Ehrlicher
                I assume that sender is anything that trigger something including the context menu, but whatever, the QTableWidget::itemAt(). works fine and is easier to implement and use.
                Thanks. Problem resolved.

                Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @LCorona said in tableContextMenu sender not work:

                I assume that sender is anything that trigger something

                Correct, and as you see in your connect() statement the sender is the QTableWidget ...

                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
                0
                • JoeCFDJ Offline
                  JoeCFDJ Offline
                  JoeCFD
                  wrote on last edited by JoeCFD
                  #8
                  if(widget == it) /* here you are comparing two addresses,  print out them. Is the sender channelsTable?
                  {
                      editChannel->setText("Descombine channels");
                  }
                  else
                  {
                      editChannel->setText("Combine channels");
                  }
                  
                  1 Reply Last reply
                  0

                  • Login

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