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

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
  • 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 Offline
    H Offline
    hubeytqew
    wrote on last edited by hubeytqew
    #1

    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?

    JonBJ 1 Reply Last reply
    0
    • H hubeytqew

      @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 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();
      
         });
      
      JonBJ H 2 Replies Last reply
      1
      • H hubeytqew

        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?

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on 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
        1
        • JonBJ JonB

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

          JonBJ 1 Reply Last reply
          0
          • H hubeytqew

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

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #4

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

            H 1 Reply Last reply
            0
            • JonBJ JonB

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

              H Offline
              H Offline
              hubeytqew
              wrote on 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.

              JonBJ M 2 Replies Last reply
              0
              • H hubeytqew

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

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on 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
                • Christian EhrlicherC Offline
                  Christian EhrlicherC Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on 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

                  JonBJ 1 Reply Last reply
                  0
                  • Christian EhrlicherC Christian Ehrlicher

                    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.

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on 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

                      @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 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();
                      
                         });
                      
                      JonBJ H 2 Replies Last reply
                      1
                      • M mpergand

                        @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();
                        
                           });
                        
                        JonBJ Offline
                        JonBJ Offline
                        JonB
                        wrote on 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
                        0
                        • JonBJ JonB

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

                            @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 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
                            0
                            • H hubeytqew

                              @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 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
                              0
                              • M mpergand

                                @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 last edited by
                                #14

                                @mpergand

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

                                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