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. Enable and disable Delete and Paste actions.
Forum Updated to NodeBB v4.3 + New Features

Enable and disable Delete and Paste actions.

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 3 Posters 4.5k Views 3 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.
  • C Offline
    C Offline
    ChajusSaib
    wrote on last edited by ChajusSaib
    #1

    Hello there, so I need a way to enable my Paste and Delete actions which work on a QTextEdit widget just like QTextEdit's context menu. I have already figured a way to disable the other actions(copy, cut, select All...)

        connect(ui->textEdit, SIGNAL(undoAvailable(bool)), ui->actionUndo, SLOT(setEnabled(bool)));
        connect(ui->textEdit, SIGNAL(redoAvailable(bool)), ui->actionRedo, SLOT(setEnabled(bool)));
        connect(ui->textEdit, SIGNAL(copyAvailable(bool)), ui->actionCopy, SLOT(setEnabled(bool)));
        connect(ui->textEdit, SIGNAL(copyAvailable(bool)), ui->actionCut, SLOT(setEnabled(bool)));
    

    Thank you

    EDIT: All of the source code: https://github.com/ChajusSaib/Text--

    As you can see I am using the MIT license, is that allowed? Do I have to include the LGPL license as well? Thank you

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      You can use QClipboard to know when there's something to past and use the textChanged signal to know when the content of your text edit has changed and enable the delete action accordingly.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      C 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        You can use QClipboard to know when there's something to past and use the textChanged signal to know when the content of your text edit has changed and enable the delete action accordingly.

        C Offline
        C Offline
        ChajusSaib
        wrote on last edited by
        #3

        @SGaist The Delete button is enabled on a selection, not when there is text. As you can see in these two images.

        Could you give me an example on how you would enable the Paste button because I don't know quite what to do.

        mrjjM 1 Reply Last reply
        0
        • C ChajusSaib

          @SGaist The Delete button is enabled on a selection, not when there is text. As you can see in these two images.

          Could you give me an example on how you would enable the Paste button because I don't know quite what to do.

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @ChajusSaib
          Hi something like

            connect(QApplication::clipboard(), SIGNAL(dataChanged()),   this, SLOT(processClipboardChange()));
          ..
          
          void MainWindow::processClipboardChange()
          // handle enable / disable
          
          C 1 Reply Last reply
          1
          • mrjjM mrjj

            @ChajusSaib
            Hi something like

              connect(QApplication::clipboard(), SIGNAL(dataChanged()),   this, SLOT(processClipboardChange()));
            ..
            
            void MainWindow::processClipboardChange()
            // handle enable / disable
            
            C Offline
            C Offline
            ChajusSaib
            wrote on last edited by
            #5

            @mrjj I've done that and it works for the most part. When I clear the clipboard for some reason the paste button still remains enabled while the paste button from QTextEdit's menu disables.

            
            void TextEditor::processPaste()
            {
                if (QApplication::clipboard()->text().isEmpty())
                    ui->actionPaste->setEnabled(false);
                else
                    ui->actionPaste->setEnabled(true);
            }
            
            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Are you using different actions for both ?

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              C 1 Reply Last reply
              0
              • SGaistS SGaist

                Are you using different actions for both ?

                C Offline
                C Offline
                ChajusSaib
                wrote on last edited by
                #7

                @SGaist Well QTextEdit has it's own one implemented in its context menu. The one here I have implemented myself for the toolbar.

                1 Reply Last reply
                0
                • C Offline
                  C Offline
                  ChajusSaib
                  wrote on last edited by
                  #8

                  Got some help from the stackoverflow lads so here it is, solved. Thanks for the help lads.

                  TextEditor::TextEditor(QWidget *parent) :
                      ...
                  {
                      ....
                  
                      connect(ui->textEdit, SIGNAL(textChanged()), this, SLOT(textModified()));
                      connect(ui->actionQuit, SIGNAL(triggered()), this, SLOT(close()));
                      connect(ui->textEdit, SIGNAL(undoAvailable(bool)), ui->actionUndo, SLOT(setEnabled(bool)));
                      connect(ui->textEdit, SIGNAL(redoAvailable(bool)), ui->actionRedo, SLOT(setEnabled(bool)));
                      connect(ui->textEdit, SIGNAL(copyAvailable(bool)), ui->actionCopy, SLOT(setEnabled(bool)));
                      connect(ui->textEdit, SIGNAL(copyAvailable(bool)), ui->actionCut, SLOT(setEnabled(bool)));
                      connect(ui->menuEdit, SIGNAL(aboutToShow()), this, SLOT(processPaste()));
                      connect(ui->textEdit, SIGNAL(selectionChanged()), this, SLOT(processDelete()));
                      connect(ui->actionAbout_Qt, SIGNAL(triggered(bool)), qApp, SLOT(aboutQt()));
                  
                     ....
                  }
                  ...
                  void TextEditor::processPaste()
                  {
                      if (const QMimeData *md = QApplication::clipboard()->mimeData())
                          ui->actionPaste->setEnabled(md->hasText());
                  }
                  
                  void TextEditor::processDelete()
                  {
                      ui->actionDelete->setEnabled(!ui->textEdit->textCursor().selectedText().isEmpty());
                  }
                  
                  1 Reply Last reply
                  1

                  • Login

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