Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

how to get selected items of a QTableWidget from eventFilter?



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


  • Lifetime Qt Champion

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



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

  • Lifetime Qt Champion

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



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



  • 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;
            }
    


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


Log in to reply