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 3.1k 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.
  • M Offline
    M Offline
    mostefa
    wrote on 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
    • SikarjanS Offline
      SikarjanS Offline
      Sikarjan
      wrote on 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 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 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
          • SikarjanS Offline
            SikarjanS Offline
            Sikarjan
            wrote on 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
            0
            • SikarjanS Sikarjan

              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 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
              • SikarjanS Offline
                SikarjanS Offline
                Sikarjan
                wrote on 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
                0
                • SikarjanS Sikarjan

                  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 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

                  SikarjanS 1 Reply Last reply
                  0
                  • jsulmJ jsulm

                    @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.

                    SikarjanS Offline
                    SikarjanS Offline
                    Sikarjan
                    wrote on 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
                    0
                    • SikarjanS Sikarjan

                      @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 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

                      • Login

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