Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Solved tableContextMenu sender not work

    General and Desktop
    4
    8
    143
    Loading More Posts
    • 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
      LCorona last edited by

      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?

      artwaw 1 Reply Last reply Reply Quote 0
      • Christian Ehrlicher
        Christian Ehrlicher Lifetime Qt Champion last edited by

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

        Qt has to stay free or it will die.

        L 1 Reply Last reply Reply Quote 0
        • L
          LCorona @Christian Ehrlicher last edited by

          @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 Reply Quote 0
          • Christian Ehrlicher
            Christian Ehrlicher Lifetime Qt Champion last edited by

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

            Qt has to stay free or it will die.

            L 1 Reply Last reply Reply Quote 2
            • L
              LCorona @Christian Ehrlicher last edited by

              @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 Ehrlicher 1 Reply Last reply Reply Quote 0
              • artwaw
                artwaw @LCorona last edited by

                @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 Reply Quote 0
                • Christian Ehrlicher
                  Christian Ehrlicher Lifetime Qt Champion @LCorona last edited by

                  @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 has to stay free or it will die.

                  1 Reply Last reply Reply Quote 0
                  • JoeCFD
                    JoeCFD last edited by JoeCFD

                    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 Reply Quote 0
                    • First post
                      Last post