Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Adjusting QLineEdit undo signal



  • Hey

    I'm trying to emit a signal once undo or redo slots are being used by qt. How can I do it? Initially I started with subclassing the void undo()/redo but they are not virtual so my code had no effect : -(

    TIA


  • Lifetime Qt Champion

    Hi,

    Can you explain why do you need it for ?



  • I don't want to change the native qt/ undo/redo for its qline edits but I do need to cast a signal when a value has been undone in order to notify some processes to recalculate their functions. Else the text just gets undone but nothing changes that needs to.

    Regards
    Dariusz



  • @Dariusz
    QLineEdit::undo() is a slot, not a signal. There is no signal issued for undo. As you say, you cannot sub-class it because it's not virtual (why not, I'm not sure).

    The most obvious simple solution is to check to see whether undo/redo do indeed cause QLineEdit::textChanged (not textEdited) signal to be emitted. (I would suspect they do, as undo/redo is a change of text, but I could be wrong.) Then you do your stuff in that handler slot. You won't be able to recognise an undo/redo versus any other edit, but then your code should not need that, as undo/redoes are simply the same thing as the corresponding edit anyway.



  • @JonB said in Adjusting QLineEdit undo signal:

    QLineEdit::undo() is a slot

    right, so being a slot he'll be calling it somehow. I guess that just after calling undo() a custom signal can be emitted. Pseudo-code:

    MyClass {
    ...
    myLineEdit = QLineEdit()
    ...
    signal:
    undoFired()
    ...
    MyClass::handleUndo() {
        myLineEdit.undo();
        emit undoFired();
    }
    
    SomeOtherClass {
    
    myClass = new MyClass();
    public slots:
        updateBecauseUndo();
    ...
    connect(myClass, SIGNAL(undoFired()), this, SLOT(updateBecauseUndo());
    ...
    }
    
    SomeOtherClass::updateBecauseUndo() {
        // do calculations because QLineEdit::undo() was fired somewhere
    }
    


  • @Pablo-J.-Rogina
    Yes, but this only works because you have created your own class to wrap a QLineEdit, and so can do what you like.

    Let's assume the OP (or whoever) already has an app which has hundreds of native QLineEdits in it, and author used them directly, not via proprietary sub-class.

    Let's assume that undo gets fired after user types some characters and then presses Ctrl+Z --- which I presume makes Qt fire the undo, right?

    He wants to know about that "undo". So now how?



  • @JonB said in Adjusting QLineEdit undo signal:

    an app which has hundreds of native QLineEdits in it, and author used them directly, not via proprietary sub-class.

    Whatever you have, a QLineEdit object must be contained into any other class. Simplest example I imagine is a MainWindow without any layout having the QLineEdit, so you indeed always have a "custom" class where you can add whatever methods you need, that's not a problem.

    Let's assume that undo gets fired after user types some characters and then presses Ctrl+Z --- which I presume makes Qt fire the undo, right?

    Your assumption is not valid, QLineEdit::undo is an SLOT so it won't get fired...



  • @Pablo-J.-Rogina said in Adjusting QLineEdit undo signal:

    Let's assume that undo gets fired after user types some characters and then presses Ctrl+Z --- which I presume makes Qt fire the undo, right?

    Your assumption is not valid, QLineEdit::undo is an SLOT so it won't get fired...

    I know it's a slot. I meant, my understanding is, user clicks Ctrl+Z, Qt framework fires signal UNDO, QLineEdit's undo slot gets notified from signal. Maybe that's not how it works, dunno...


  • Lifetime Qt Champion

    Hi
    QLineEdit::textChanged is triggered by undo.
    Btw i also thought undo() / redo() was signals but turns out they are for external activation.


Log in to reply