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. how to get selected items of a QTableWidget from eventFilter?
QtWS25 Last Chance

how to get selected items of a QTableWidget from eventFilter?

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 4 Posters 947 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.
  • RipleyR Offline
    RipleyR Offline
    Ripley
    wrote on last edited by
    #1

    Hi everybody.

    My next problem is when in qt implements a filter in a QTableWidget after this activated according every movement of the event, I dont know how to get selected items because is neccesary to after create the algorith to read copy (ctrl+c) and paste (ctrl+v) into the table. Please help me is a broken header.

    The next code show me what it is the current index selected but the program crashs when open it.

    bool StudyEventFilter::eventFilter(QObject *obj, QEvent *e) {
        //Q_UNUSED(obj)
        QTableWidget *table = obj->findChild<QTableWidget*>("deliverablesTableWidget");
        qDebug() << table->currentIndex();
        if(e->type() == QEvent::KeyPress){
            QKeyEvent * keyEvent = static_cast<QKeyEvent *>(e);
            if(keyEvent->matches(QKeySequence::Copy) ){
                qDebug() << "Copy";
                return true;
    
            }
            else if(keyEvent->matches(QKeySequence::Paste)){
                qDebug() << "Paste";
                return true;
            }
        }
        else {
                return false;
            }
    }
    

    The main target is to goal copy and paste into the same qtablewidget. Thanks a lot, waiting you understand me.

    jsulmJ 1 Reply Last reply
    0
    • RipleyR Ripley

      Hi everybody.

      My next problem is when in qt implements a filter in a QTableWidget after this activated according every movement of the event, I dont know how to get selected items because is neccesary to after create the algorith to read copy (ctrl+c) and paste (ctrl+v) into the table. Please help me is a broken header.

      The next code show me what it is the current index selected but the program crashs when open it.

      bool StudyEventFilter::eventFilter(QObject *obj, QEvent *e) {
          //Q_UNUSED(obj)
          QTableWidget *table = obj->findChild<QTableWidget*>("deliverablesTableWidget");
          qDebug() << table->currentIndex();
          if(e->type() == QEvent::KeyPress){
              QKeyEvent * keyEvent = static_cast<QKeyEvent *>(e);
              if(keyEvent->matches(QKeySequence::Copy) ){
                  qDebug() << "Copy";
                  return true;
      
              }
              else if(keyEvent->matches(QKeySequence::Paste)){
                  qDebug() << "Paste";
                  return true;
              }
          }
          else {
                  return false;
              }
      }
      

      The main target is to goal copy and paste into the same qtablewidget. Thanks a lot, waiting you understand me.

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Ripley said in how to get selected items of a QTableWidget from eventFilter?:

      the program crashs when open it

      Well, you don't check whether table is a valid pointer. In general in such cases you should use the debugger to see where exactly it is crashing and what the values of involved variables are.

      There is https://doc.qt.io/qt-5/qtablewidget.html#selectedItems - isn't that what you need?

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

      RipleyR 1 Reply Last reply
      3
      • jsulmJ jsulm

        @Ripley said in how to get selected items of a QTableWidget from eventFilter?:

        the program crashs when open it

        Well, you don't check whether table is a valid pointer. In general in such cases you should use the debugger to see where exactly it is crashing and what the values of involved variables are.

        There is https://doc.qt.io/qt-5/qtablewidget.html#selectedItems - isn't that what you need?

        RipleyR Offline
        RipleyR Offline
        Ripley
        wrote on last edited by
        #3

        @jsulm Yes, I tried to debug and this return QWidget(0x0). And not get any idea how recover the data that was selected by the key or cursor.

        QTableWidget *table = obj->findChild<QTableWidget*>("deliverablesTableWidget");
        qDebug() << table;
        
        1 Reply Last reply
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Why not simply derive from QTableView/Widget instead an eventHandler ? Why that complicated?

          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
          • RipleyR Offline
            RipleyR Offline
            Ripley
            wrote on last edited by
            #5

            @Christian-Ehrlicher because I am beginner I wish I look at for a solution or simple solution.

            1 Reply Last reply
            0
            • RipleyR Offline
              RipleyR Offline
              Ripley
              wrote on last edited by
              #6

              Now i get the ctrl+c data with the next code:

              QClipboard *clip;
              clip = QApplication::clipboard();
              if(keyEvent->matches(QKeySequence::Copy) ){
                          QItemSelectionModel *table = obj->findChild<QItemSelectionModel*>("");
                          QModelIndex index = table->currentIndex();
                          clip->setText(index.data().toString());
                          return true;
              
                      }
              

              And now I dont know how paste it the correspondent cells that i was selected.

                      else if(keyEvent->matches(QKeySequence::Paste)){
                          if(clip->mimeData()->hasText())
                              foreach (QModelIndex item, obj->findChild<QItemSelectionModel*>("")->selectedIndexes()){
                                  QStandardItemModel *sModel = new QStandardItemModel(this);
                                  QStandardItem *model = sModel->itemFromIndex(item);
                                  QModelIndex *l = dynamic_cast<QModelIndex*>(model);
                                  model->setText(clip->text());
                                  item.model()->itemData(*l);
                              }
                          return true;
                      }
              
              JonBJ 1 Reply Last reply
              0
              • RipleyR Ripley

                Now i get the ctrl+c data with the next code:

                QClipboard *clip;
                clip = QApplication::clipboard();
                if(keyEvent->matches(QKeySequence::Copy) ){
                            QItemSelectionModel *table = obj->findChild<QItemSelectionModel*>("");
                            QModelIndex index = table->currentIndex();
                            clip->setText(index.data().toString());
                            return true;
                
                        }
                

                And now I dont know how paste it the correspondent cells that i was selected.

                        else if(keyEvent->matches(QKeySequence::Paste)){
                            if(clip->mimeData()->hasText())
                                foreach (QModelIndex item, obj->findChild<QItemSelectionModel*>("")->selectedIndexes()){
                                    QStandardItemModel *sModel = new QStandardItemModel(this);
                                    QStandardItem *model = sModel->itemFromIndex(item);
                                    QModelIndex *l = dynamic_cast<QModelIndex*>(model);
                                    model->setText(clip->text());
                                    item.model()->itemData(*l);
                                }
                            return true;
                        }
                
                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by JonB
                #7

                @Ripley said in how to get selected items of a QTableWidget from eventFilter?:

                QStandardItemModel *sModel = new QStandardItemModel(this);

                You don't want to create a new model. You want to use or find the model your QTableWidget is showing and put the value in there.

                1 Reply Last reply
                2

                • Login

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