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. Right Click on a cell to copy the content of it
Forum Updated to NodeBB v4.3 + New Features

Right Click on a cell to copy the content of it

Scheduled Pinned Locked Moved Solved General and Desktop
14 Posts 4 Posters 2.0k 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.
  • H hubeytqew
    14 Dec 2022, 09:11

    I have a table created with QTableWidget. I want to copy the cell's content (data) with right click. I've searched the web but haven't found anything that works for the right click event. How can I achieve this?

    J Offline
    J Offline
    JonB
    wrote on 14 Dec 2022, 09:21 last edited by
    #2

    @hubeytqew said in Right Click on a cell to copy the content of it:

    I've searched the web but haven't found anything that works for the right click event.

    I don't know whether this is the best or only way, but every widget has a mousePressEvent(QMouseEvent *event). Overriding that you can inspect whether it's a right button and QTableWidgetItem *QTableWidget::itemAt(const QPoint &point) const can tell you which item it's on.

    H 1 Reply Last reply 14 Dec 2022, 10:31
    1
    • J JonB
      14 Dec 2022, 09:21

      @hubeytqew said in Right Click on a cell to copy the content of it:

      I've searched the web but haven't found anything that works for the right click event.

      I don't know whether this is the best or only way, but every widget has a mousePressEvent(QMouseEvent *event). Overriding that you can inspect whether it's a right button and QTableWidgetItem *QTableWidget::itemAt(const QPoint &point) const can tell you which item it's on.

      H Offline
      H Offline
      hubeytqew
      wrote on 14 Dec 2022, 10:31 last edited by hubeytqew
      #3

      @JonB
      Now, I catch right click in the app with below code.

      void manager::mousePressEvent(QMouseEvent *e)
      {
          if (e->button() == Qt::RightButton)
          {
              qDebug() << "hi!";
          }
      }
      

      But it can not catch right click on the QTableWidget. How do I catch the right click on the table?

      J 1 Reply Last reply 14 Dec 2022, 10:52
      0
      • H hubeytqew
        14 Dec 2022, 10:31

        @JonB
        Now, I catch right click in the app with below code.

        void manager::mousePressEvent(QMouseEvent *e)
        {
            if (e->button() == Qt::RightButton)
            {
                qDebug() << "hi!";
            }
        }
        

        But it can not catch right click on the QTableWidget. How do I catch the right click on the table?

        J Offline
        J Offline
        JonB
        wrote on 14 Dec 2022, 10:52 last edited by
        #4

        @hubeytqew
        By doing this on the QTableWidget? I have no idea what manager might be....

        H 1 Reply Last reply 14 Dec 2022, 13:02
        0
        • J JonB
          14 Dec 2022, 10:52

          @hubeytqew
          By doing this on the QTableWidget? I have no idea what manager might be....

          H Offline
          H Offline
          hubeytqew
          wrote on 14 Dec 2022, 13:02 last edited by
          #5

          @JonB

          manager is the cpp file name. I use this right click event in manager cpp code. So this is the file name..

          for example, I use cellClicked function which belongs to QTableWidget;

          connect(ui->tableWidget, &QTableWidget::cellClicked, this, &manager::cellClicked);
          

          If I click the cell, function do some jobs in that cell..as you can see, manager is the file name.
          Whatever, I can use it for the one click. Thats ok. I want to handle right click.

          J M 2 Replies Last reply 14 Dec 2022, 13:08
          0
          • H hubeytqew
            14 Dec 2022, 13:02

            @JonB

            manager is the cpp file name. I use this right click event in manager cpp code. So this is the file name..

            for example, I use cellClicked function which belongs to QTableWidget;

            connect(ui->tableWidget, &QTableWidget::cellClicked, this, &manager::cellClicked);
            

            If I click the cell, function do some jobs in that cell..as you can see, manager is the file name.
            Whatever, I can use it for the one click. Thats ok. I want to handle right click.

            J Offline
            J Offline
            JonB
            wrote on 14 Dec 2022, 13:08 last edited by
            #6

            @hubeytqew
            You want to know about mouse presses on the QTableWidget, right? So manager::mousePressEvent() on manager is not the right thing.

            You need to subclass Qt's QTableWidget to produce your own derived class and then in that you can override the mousePressEvent() method like you showed --- but not on whatever manager is. And you will not be using signals & slots here, only overriding mousePressEvent(). I don't think you can use any of the clicked signals as I believe they are only generated for left-click, not right-click?

            1 Reply Last reply
            0
            • C Offline
              C Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on 14 Dec 2022, 13:18 last edited by
              #7

              clicked() is only emitted for Qt::LeftButton, pressed() for all buttons. activated() is emitted for all buttons when the style hint SH_ItemView_ActivateItemOnSingleClick is enabled.

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

              J 1 Reply Last reply 14 Dec 2022, 13:22
              0
              • C Christian Ehrlicher
                14 Dec 2022, 13:18

                clicked() is only emitted for Qt::LeftButton, pressed() for all buttons. activated() is emitted for all buttons when the style hint SH_ItemView_ActivateItemOnSingleClick is enabled.

                J Offline
                J Offline
                JonB
                wrote on 14 Dec 2022, 13:22 last edited by JonB
                #8

                @Christian-Ehrlicher , @hubeytqew
                Which I think implies you will indeed need to go for overriding the mousePressEvent() as suggested previously.

                BTW, if you don't want to subclass and override, I presume you could instead use void QObject::installEventFilter(QObject *filterObj) and get a mouse press event where you can test for right button, though I haven't tested.

                1 Reply Last reply
                0
                • H hubeytqew
                  14 Dec 2022, 13:02

                  @JonB

                  manager is the cpp file name. I use this right click event in manager cpp code. So this is the file name..

                  for example, I use cellClicked function which belongs to QTableWidget;

                  connect(ui->tableWidget, &QTableWidget::cellClicked, this, &manager::cellClicked);
                  

                  If I click the cell, function do some jobs in that cell..as you can see, manager is the file name.
                  Whatever, I can use it for the one click. Thats ok. I want to handle right click.

                  M Offline
                  M Offline
                  mpergand
                  wrote on 14 Dec 2022, 13:49 last edited by
                  #9

                  @hubeytqew
                  Using a context menu should work:

                  ui->tableWidget->setContextMenuPolicy(Qt::CustomContextMenu);
                     connect(ui->tableWidget, &QWidget::customContextMenuRequested,this,[this] ()
                     {
                         QList<QTableWidgetItem *> items=ui->tableWidget->selectedItems();
                         if(items.count()>0)
                             qDebug()<<items[0]->text();
                  
                     });
                  
                  J H 2 Replies Last reply 14 Dec 2022, 14:02
                  1
                  • M mpergand
                    14 Dec 2022, 13:49

                    @hubeytqew
                    Using a context menu should work:

                    ui->tableWidget->setContextMenuPolicy(Qt::CustomContextMenu);
                       connect(ui->tableWidget, &QWidget::customContextMenuRequested,this,[this] ()
                       {
                           QList<QTableWidgetItem *> items=ui->tableWidget->selectedItems();
                           if(items.count()>0)
                               qDebug()<<items[0]->text();
                    
                       });
                    
                    J Offline
                    J Offline
                    JonB
                    wrote on 14 Dec 2022, 14:02 last edited by JonB
                    #10

                    @mpergand
                    I agree this is nice "simple" code, but it has flaws, does it not, for "Right Click on a cell to copy the content of it"? Examples: table widget set to not allow selection or set to allow multi-cell selection?

                    H 1 Reply Last reply 16 Dec 2022, 13:19
                    0
                    • J JonB
                      14 Dec 2022, 14:02

                      @mpergand
                      I agree this is nice "simple" code, but it has flaws, does it not, for "Right Click on a cell to copy the content of it"? Examples: table widget set to not allow selection or set to allow multi-cell selection?

                      H Offline
                      H Offline
                      hubeytqew
                      wrote on 16 Dec 2022, 13:19 last edited by
                      #11

                      @JonB

                      thx for the replies both @JonB @mpergand @Christian-Ehrlicher . I'll try this code example and ideas and go back here soon.

                      1 Reply Last reply
                      0
                      • M mpergand
                        14 Dec 2022, 13:49

                        @hubeytqew
                        Using a context menu should work:

                        ui->tableWidget->setContextMenuPolicy(Qt::CustomContextMenu);
                           connect(ui->tableWidget, &QWidget::customContextMenuRequested,this,[this] ()
                           {
                               QList<QTableWidgetItem *> items=ui->tableWidget->selectedItems();
                               if(items.count()>0)
                                   qDebug()<<items[0]->text();
                        
                           });
                        
                        H Offline
                        H Offline
                        hubeytqew
                        wrote on 16 Dec 2022, 13:29 last edited by
                        #12

                        @mpergand

                        bro, you are just perfect....
                        This code is working without any correction...

                        only the thing is I should copy the text to clipboard. I'll find it, I think.

                        M 1 Reply Last reply 16 Dec 2022, 13:51
                        0
                        • H hubeytqew
                          16 Dec 2022, 13:29

                          @mpergand

                          bro, you are just perfect....
                          This code is working without any correction...

                          only the thing is I should copy the text to clipboard. I'll find it, I think.

                          M Offline
                          M Offline
                          mpergand
                          wrote on 16 Dec 2022, 13:51 last edited by
                          #13

                          @hubeytqew said in Right Click on a cell to copy the content of it:

                          copy the text to clipboard.

                          qApp->clipboard()->setText("you are just perfect....");
                          :)

                          H 1 Reply Last reply 19 Dec 2022, 05:18
                          0
                          • M mpergand
                            16 Dec 2022, 13:51

                            @hubeytqew said in Right Click on a cell to copy the content of it:

                            copy the text to clipboard.

                            qApp->clipboard()->setText("you are just perfect....");
                            :)

                            H Offline
                            H Offline
                            hubeytqew
                            wrote on 19 Dec 2022, 05:18 last edited by
                            #14

                            @mpergand

                            Hahaha...bro...thx...

                            1 Reply Last reply
                            0

                            11/14

                            16 Dec 2022, 13:19

                            • Login

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