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. Check if undo/redo stack has items
Forum Updated to NodeBB v4.3 + New Features

Check if undo/redo stack has items

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 3 Posters 2.9k Views 1 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.
  • S Offline
    S Offline
    Sikarjan
    wrote on 20 Jan 2017, 08:56 last edited by
    #1

    Hi,

    I am working on a text editor with tabs. When a tab changes I need to check if a undo or redo action is available and set my buttons accordingly. I could not find a bool value in QPlainTextEdit that would tell me if there is something that I can undo.
    I added a variable that holds if the text in the tab has been changed for other reasons and I can use that to detect if undo is available but this does not help with redo. Do I need to add my own variable to capture the redo state or is there something build in?
    Also I noticed that the undoAvailable signal is not fired when I start typing in my QPlainTextEdit. The standard short cuts cmd/crl+z work as expected. Is this a "feature" or am I doing something wrong when I connect undoAvailable with my undo action?

    Thanks.

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mostefa
      wrote on 20 Jan 2017, 09:32 last edited by mostefa
      #2

      Hello @Sikarjan

      What about :

      redoAvailable, and undoAvailable signals:

      According to Qt doc

      void QPlainTextEdit::redoAvailable ( bool available ) [signal]
      This signal is emitted whenever redo operations become available (available is true) or unavailable (available is false).

      void QPlainTextEdit::undoAvailable ( bool available ) [signal]
      This signal is emitted whenever undo operations become available (available is true) or unavailable (available is false).

      You can do something like this:

      connect(yourPlainTextEdit,SIGNAL(redoAvailable(bool),this,SLOT(onRedoAvailable(bool));
      

      and then implement onRedoAvailable(bool) slots:

      void onRedoAvailable(bool isRedoAvailable)
      {
            if(isRedoAvailable)
            {
                  //change your ui as you need
            }
      }
      
      1 Reply Last reply
      0
      • S Offline
        S Offline
        Sikarjan
        wrote on 20 Jan 2017, 09:49 last edited by
        #3

        Hi mostefa,

        this is exactly what I did but
        a) the first undoAvailable is not fired when I edit the text. I need to set it manually or do a redo action
        b) when I change my tab the undo/redo state could be different from the last tab. I need to check for the current QPlainTextEdit if there is a undo/redo item in the stack.

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mostefa
          wrote on 20 Jan 2017, 09:55 last edited by
          #4

          Is it possible to share your code with us ?

          1 Reply Last reply
          0
          • M Offline
            M Offline
            mostefa
            wrote on 20 Jan 2017, 10:28 last edited by
            #5

            @Sikarjan

            I made a test code with :

            One QPlainTextEdit,Two QPushButton (mUndoButton,mRedoButton) , and it seems to work without any problem , here is my code if this can help:

            Header file

            #ifndef MAINWINDOW_H
            #define MAINWINDOW_H
            
            #include <QMainWindow>
            
            namespace Ui {
            class MainWindow;
            }
            
            class MainWindow : public QMainWindow
            {
                Q_OBJECT
            
            public:
                explicit MainWindow(QWidget *parent = 0);
                ~MainWindow();
            
            private slots:
                void onUndoAvailable(bool isUndoAvailable);
                void onRedoAvailable(bool isRedoAvailable);
            
            private:
                Ui::MainWindow *ui;
            };
            
            #endif // MAINWINDOW_H
            

            Source File

            #include "mainwindow.h"
            #include "ui_mainwindow.h"
            
            #include <QDebug>
            
            MainWindow::MainWindow(QWidget *parent) :
                QMainWindow(parent),
                ui(new Ui::MainWindow)
            {
                ui->setupUi(this);
            
            
                connect(ui->plainTextEdit,SIGNAL(undoAvailable(bool)),this,SLOT(onUndoAvailable(bool)));
                connect(ui->plainTextEdit,SIGNAL(redoAvailable(bool)),this,SLOT(onRedoAvailable(bool)));
            
                ui->mRedoBUtton->setEnabled(false);
                ui->mUndoButton->setEnabled(false);
            }
            
            MainWindow::~MainWindow()
            {
                delete ui;
            }
            
            void MainWindow::onUndoAvailable(bool isUndoAvailable)
            {
                qDebug() << Q_FUNC_INFO << "isUndoAvailable" << isUndoAvailable;
                if(isUndoAvailable)
                {
                    ui->mUndoButton->setEnabled(true);
                }
                else
                {
                    ui->mUndoButton->setEnabled(false);
                }
            }
            
            void MainWindow::onRedoAvailable(bool isRedoAvailable)
            {
                qDebug() << Q_FUNC_INFO << "isRedoAvailable" << isRedoAvailable;
                if(isRedoAvailable)
                {
                    ui->mRedoBUtton->setEnabled(true);
                }
                else
                {
                    ui->mRedoBUtton->setEnabled(false);
                }
            }
            

            Hope this can help !

            1 Reply Last reply
            0
            • S Offline
              S Offline
              Sikarjan
              wrote on 20 Jan 2017, 10:44 last edited by Sikarjan
              #6

              Hi mostefa,

              Thanks for the example! I have a QTabView each tab holds a QPlainTextEdit. As long as I am in one tab everything works fine. I have connected the signals and slots and they work as intended (besides the first undo signal). My issue is that if I change the tab I need to change my icons as well. By changing the tab the current QPlainTextEdit will not fire undo/redoAvailable.

              How could I make it fire or how can I check the current state?

              Other than by storing the current state in new variable.

              jsulmJ 1 Reply Last reply 20 Jan 2017, 10:50
              0
              • S Sikarjan
                20 Jan 2017, 10:44

                Hi mostefa,

                Thanks for the example! I have a QTabView each tab holds a QPlainTextEdit. As long as I am in one tab everything works fine. I have connected the signals and slots and they work as intended (besides the first undo signal). My issue is that if I change the tab I need to change my icons as well. By changing the tab the current QPlainTextEdit will not fire undo/redoAvailable.

                How could I make it fire or how can I check the current state?

                Other than by storing the current state in new variable.

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on 20 Jan 2017, 10:50 last edited by
                #7

                @Sikarjan When you change the tab disconnect the previous textEdit and connect the new one

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  Sikarjan
                  wrote on 20 Jan 2017, 10:59 last edited by
                  #8

                  Not sure how I would do the disconnect. The tab changed signal only gives me the new index and I do not see a onFocusLost signal I could connect to. So how would you do the disconnect?

                  jsulmJ 1 Reply Last reply 20 Jan 2017, 12:23
                  0
                  • S Sikarjan
                    20 Jan 2017, 10:59

                    Not sure how I would do the disconnect. The tab changed signal only gives me the new index and I do not see a onFocusLost signal I could connect to. So how would you do the disconnect?

                    jsulmJ Offline
                    jsulmJ Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on 20 Jan 2017, 12:23 last edited by
                    #9

                    @Sikarjan You can store the pointer to currently active textEdit. If you switch to another tab you disconnect from the old textEdit using that stored pointer (use disconnect() function), connect the new one and store the pointer to the new textEdit.

                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                    S 1 Reply Last reply 20 Jan 2017, 13:02
                    0
                    • jsulmJ jsulm
                      20 Jan 2017, 12:23

                      @Sikarjan You can store the pointer to currently active textEdit. If you switch to another tab you disconnect from the old textEdit using that stored pointer (use disconnect() function), connect the new one and store the pointer to the new textEdit.

                      S Offline
                      S Offline
                      Sikarjan
                      wrote on 20 Jan 2017, 13:02 last edited by
                      #10

                      @jsulm Okay I see. And with connecting all signals will be fired, right?

                      Thanks for the suggestion.

                      jsulmJ 1 Reply Last reply 20 Jan 2017, 13:05
                      0
                      • S Sikarjan
                        20 Jan 2017, 13:02

                        @jsulm Okay I see. And with connecting all signals will be fired, right?

                        Thanks for the suggestion.

                        jsulmJ Offline
                        jsulmJ Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on 20 Jan 2017, 13:05 last edited by
                        #11

                        @Sikarjan said in Check if undo/redo stack has items:

                        And with connecting all signals will be fired, right?

                        What do you mean? Connected signals will be fired, that's why you connect them.

                        https://forum.qt.io/topic/113070/qt-code-of-conduct

                        1 Reply Last reply
                        0

                        1/11

                        20 Jan 2017, 08:56

                        • Login

                        • Login or register to search.
                        1 out of 11
                        • First post
                          1/11
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved