octave-maintainers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Help with copy/paste from/to Qt command Window


From: Patrick Noffke
Subject: Re: Help with copy/paste from/to Qt command Window
Date: Mon, 22 Apr 2013 17:19:31 -0500



On Mon, Apr 22, 2013 at 4:01 PM, John W. Eaton <address@hidden> wrote:
On 04/22/2013 04:49 PM, PhilipNienhuis wrote:

To make things configurable would be great - but IMO that is no priority.

My question wasn't so much about what we should do, as *how* do we make copy and paste work with keybindings using Qt, and what is the correct way to create configurable keybindings, so that the meaning of a key can change depending on the context (not the window context, but the context of what is Octave doing at the moment).

If I knew those things, then I could experiment with these options and better decide what the behavior should be.  Which, I'm pretty sure, is that the bindings should be configurable in some way, and that the default, at least for Windows users, should be that Ctrl-C copies when Octave is waiting for input, otherwise, it interrupts.  But even that might annoy some people, so I suppose an option to have Ctrl-C always copy along with a button to interrupt would be reasonable.

But again, I'm not asking so much for comments about what we should do, as *how* to do it.

I think you want to bind a QShortcut::activated signal to a slot for the action you want (e.g. copy or interrupt for Ctrl-C).
http://qt-project.org/doc/qt-4.8/qshortcut.html

Note that your shortcuts may need translation, so it is good to run them through QApplication::translate.

For a translation example, here is the line that sets the QKeySequence for the "Quit" action, which is part of a File menu in a UI I wrote:
action_Quit->setShortcut(QApplication::translate("MainWindow", "Ctrl+Q", 0, QApplication::UnicodeUTF8));
(The parameter to QAction::setShortcut is a QKeySequence.  You would construct a QShortcut with a (translated) QKeySequence in the constructor.)

I think you'd set the parent of the QShortcut to the top-level widget (the main window).

Alternately, you can override the QKeyEvent handler, but I don't think this is good if you want to handle translations.  Here's a snippet of code from one of my UIs, which allows to enter and exit full-screen mode:

void MainWindow::keyPressEvent(QKeyEvent* keyEvent)
{
   switch (keyEvent->key())
   {
      case Qt::Key_Escape:
         setWindowState(windowState() & ~Qt::WindowFullScreen);
         this->menuWidget()->show();
         break;
      case Qt::Key_F11:
         setWindowState(windowState() ^ Qt::WindowFullScreen);
         windowState() & Qt::WindowFullScreen ? this->menuWidget()->hide() :
            this->menuWidget()->show();
         break;
      default:
         QMainWindow::keyPressEvent(keyEvent);
         break;
   }
}


With either approach, if you can figure out the context of what Octave is doing from some other means, then you should be able to handle the key sequence in the appropriate way (i.e. based on context) in the slot/event handler.

Note that the text widgets implement the standard "copy/cut/paste" by default.  For others, you might need to roll your own.  For example, if you want to copy from a QTableView (without going to edit mode or creating a custom display widget), you might want to check out the following links:
http://www.qtcentre.org/threads/1835-Copying-contents-of-QTableView-cell-to-clipboard
http://stackoverflow.com/questions/1230222/selected-rows-line-in-qtableview-copy-to-qclipboard

I also saw some examples about using QWidget::addActions to add custom context menus to a QTableWidget.  Let me know if that's the kind of thing you're trying to do.

Pat

reply via email to

[Prev in Thread] Current Thread [Next in Thread]