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. KeyPressevent (please review)
Qt 6.11 is out! See what's new in the release blog

KeyPressevent (please review)

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 2 Posters 2.6k Views 2 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.
  • Q Offline
    Q Offline
    Qt Enthusiast
    wrote on last edited by mrjj
    #1
    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)

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      1
      • Q Offline
        Q Offline
        Qt Enthusiast
        wrote on last edited by
        #3

        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();
        }

        1 Reply Last reply
        0
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          1
          • Q Offline
            Q Offline
            Qt Enthusiast
            wrote on last edited by
            #5

            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();
            }

            1 Reply Last reply
            0
            • mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              I would guess on version 1

              1 Reply Last reply
              0
              • Q Offline
                Q Offline
                Qt Enthusiast
                wrote on last edited by
                #7

                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()
                }

                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