PDA

View Full Version : Dispatch menu action



ecir.hana
15th March 2013, 10:56
I have three text edits: "a", "b", "c". When I type something into any of them, I can hit keyboard shortcut for undo/redo and it works as expected.

I would like to create a menu item which when triggered would undo/redo in the currently focused text edit. Please, can someone tell me how to approach this? Do I have to create a slot (myUndo, myRedo), connect each of "a", "b", "c" to it, check manually which edit has focus and trigger undo/redo on that edit? Or are there any other, more streamlined, ways of doing this? Like connecting to parent widget and letting somehow Qt do the work?

Related problem, although this is to be expected, is that when I connect menu action "Undo" (along with its keyboard shortcut) for edit "a" and I'm editing in "b" and I hit shortcut for "Undo", "a" gets undo-ed, instead of currently focused "b". How to make it undo "a" when "a" is selected, "b" when "b", "c" when "c"? Do I have to "rebind" the menu shortcut/action all the time?

bootchk
15th March 2013, 13:00
Is that a good user interface design? Most users will expect it to work a more conventional way: it doesn't matter where (a, b, c) the changes are, Undo undoes the changes in the time order they were done. Unless a,b,c are separate "documents."

Don't you need an undo stack for each? And as you say, the menu item must dispatch: invoke the undo of the focused item.

I suppose you could put all the commands on one stack, and when dispatching, filter the stack for the topmost command that is for the focused item.

I think the title of your thread should be more descriptive, is the thread about undo or a more general question about context-sensitive menus? I don't think that context-sensitivity (or focus sensitivity) is built into Qt menu classes.

ecir.hana
15th March 2013, 13:08
Consider a phone book application: there is one QLineEdit for name, other one for surname, etc. All of the QLineEdits have their own undo stacks and when focused work woth keyboard shortcuts as expected. The problem is, that I would like to be able to trigger undo/redo from menu as well...

But maybe I misread you reply..? Thanks in any case!

d_stranz
15th March 2013, 21:44
It sounds like you have set up your actions incorrectly, so instead of it behaving the way you want it to, it's behaving the way you've told it to.

You might find QSignalMapper useful for this problem.

ecir.hana
15th March 2013, 22:56
It sounds like you have set up your actions incorrectly, so instead of it behaving the way you want it to, it's behaving the way you've told it to.

Could you please tell what I am doing wrong with this setup:

mainwindow.h:


class MainWindow : public QMainWindow
{
Q_OBJECT

public:
MainWindow(QWidget *parent = 0);
~MainWindow();
QLineEdit *title;
QLineEdit *name;
QLineEdit *surname;
};

mainwindow.cpp:


MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
QVBoxLayout *vbox = new QVBoxLayout();
title = new QLineEdit();
vbox->addWidget(title);
name = new QLineEdit();
vbox->addWidget(name);
surname = new QLineEdit();
vbox->addWidget(surname);
QWidget *w = new QWidget(this);
w->setLayout(vbox);
this->setCentralWidget(w);

QMenu *undoMenu = menuBar()->addMenu("Edit");
// undoMenu->addAction("Undo", this, SLOT(undo()), QKeySequence::Undo); // does nothing
undoMenu->addAction("Undo", title, SLOT(undo()), QKeySequence::Undo); // undoes only title
}

And thanks for the QSignalMapper link, I will have a look at it...

wysota
16th March 2013, 19:10
Do I have to create a slot (myUndo, myRedo), connect each of "a", "b", "c" to it, check manually which edit has focus and trigger undo/redo on that edit?
No. Track the widget that has focus using signals made available by QApplication and when your undo/redo is invoked, redirect it to that widget's undo/redo.

ecir.hana
18th March 2013, 00:23
This looks like exactly what I'm after, thanks!

I have one more question, though. Imagine that in addiction to those QLineEdits there are QPlainTextEdits as well. The parameters of "focusChanged()" are two QWidgets - how to call "undo()" on a QWidget? This is perhaps more general question C++ question but maybe Qt provides some ways about calling dynamically resolved methods. I'm asking because, for instance, Objetive C has this "respondsToSelector:" method which when returns YES, one can safely "call" a method of an object.

Or do I have to first "qobject_cast" a QWidget to QPlainTextEdit, and if unsuccessful then cast it to QLineEdit, ..?

wysota
18th March 2013, 01:31
Use qobject_cast or dynamic_cast.

anda_skoa
18th March 2013, 09:48
Since undo/redo methods are also slots, you also have the option of invoking them via QMetaObject::invokeMethod()



QMetaObject::invokeMethod( widgetPointer, "undo", Qt::DirectConnection );


Cheers,
_