How to implement the “Edit” menu with “Undo”, “Cut”, “Paste” and “Copy”?

后端 未结 4 505
野的像风
野的像风 2021-01-12 06:34

Greetings,

for one of my applications I\'m trying to implement an \"Edit\" menu. This menu usually has the standard-entries Undo, Cut, Copy

4条回答
  •  长情又很酷
    2021-01-12 07:16

    The best solution I could find for this comes from https://www.qtcentre.org/threads/10709-using-cut()-copy()-paste(). In my app, this ended up looking like this:

    connect(ui->actionCut, &QAction::triggered, []() {
        QWidget *focusWidget = QApplication::focusWidget();
        QLineEdit *lineEdit = dynamic_cast(focusWidget);
        QTextEdit *textEdit = dynamic_cast(focusWidget);
    
        if (lineEdit && lineEdit->isEnabled() && !lineEdit->isReadOnly())
            lineEdit->cut();
        else if (textEdit && textEdit->isEnabled() && !textEdit->isReadOnly())
            textEdit->cut();
    });
    

    This is really awful, and has to be done for each of the standard menu items (undo, redo, cut, copy, paste, delete, select all...), and getting the menu items to enable/disable correctly requires yet more hoops to be jumped through. This is the first time, in porting a Cocoa app to Qt, that I have felt that Qt is clearly and markedly inferior (in this case, to Cocoa's first responder mechanism, which doesn't seem to exist in Qt at all). Still, I think it's better than the solution proposed by user285740, which hard-codes particular keyboard actions. YMMV.

提交回复
热议问题