KeyPressevent (please review)



  • class tableView : public new QTableView {
    }
    tableView::keyPressEvent( QKeyEvent* event ) {
      if (event->key() == Qt::Key_C && event->modifiers() & Qt::ControlModifier) {
        copyText();
      } else {
        QTableView::keyPressEvent(event);
      }
    }
    tableView::copyText() {
      if (!selectionModel()) {
        return;
      }
      QModelIndexList selected = selectionModel()->selectedRows();
      QModelIndex originalIndex ;
      QString copyText;
      for (int idx = 0; idx < selected.count(); idx++) {
        QModelIndex index = selected[idx];
        copyText.append((index.data().toString()));
        if (idx != selected.count() - 1) {
          copyText.append(" ");
        }
      }
      QApplication::clipboard()->setText(copyText);
    }
    

    is implimentation of keyPressEvent is fine
    or you see any improvements in it
    (added code tags)


  • Qt Champions 2016

    Hi
    Beside you dont need tableView:: when inside class ( I guess its for showing code)

    also
    class tableView : public new QTableView

    looks odd. ( copy paste error) I assume.

    Else it looks ok.



  • my major concern is

    is the code has to be like as this

    tableView::keyPressEvent( QKeyEvent *event ) {
    if (event->key() == Qt::Key_C && event->modifiers() & Qt::ControlModifier) {
    copyText();
    } else {
    QTableView::keyPressEvent(event);
    }
    }```

    //your code h](link url)ere
    
        or
    

    void tableView::keyPressEvent( QKeyEvent *event ) {
    QTableView::keyPressEvent(event);
    if (event->key() == Qt::Key_C && event->modifiers() & Qt::ControlModifier) {
    copyText();
    event->accept();
    }


  • Qt Champions 2016

    Hi
    both can work.
    The difference is
    1:
    Do my stuff OR do parent stuff
    2:
    Do parent stuff
    then do my stuff

    So depends on what u really want.-
    U want parent do do as parent do when keypress
    OR do you want to only do what u do
    or do you want both



  • my major query is

    is the code is has to be as follows
    tableView::keyPressEvent( QKeyEvent *event ) {
    if (event->key() == Qt::Key_C && event->modifiers() & Qt::ControlModifier) {
    copyText();
    } else {
    QTableView::keyPressEvent(event);
    }
    }```

    or
    

    void tableView::keyPressEvent( QKeyEvent *event ) {
    QTableView::keyPressEvent(event);
    if (event->key() == Qt::Key_C && event->modifiers() & Qt::ControlModifier) {
    copyText();
    event->accept();
    }


  • Qt Champions 2016

    I would guess on version 1



  • for key press control-C , I want to do copy for rest of keybindings I want to do what parent wants to do this I feel following code is better

    tableView::keyPressEvent( QKeyEvent *event ) {
    if (event->key() == Qt::Key_C && event->modifiers() & Qt::ControlModifier) {
    copyText();
    }
    QTableView::keyPressEvent(event);
    event->accept()
    }


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.