Solved Enable and disable Delete and Paste actions.
-
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
-
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.
-
-
@ChajusSaib
Hi something likeconnect(QApplication::clipboard(), SIGNAL(dataChanged()), this, SLOT(processClipboardChange())); .. void MainWindow::processClipboardChange() // handle enable / disable
-
@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); }
-
Are you using different actions for both ?
-
@SGaist Well QTextEdit has it's own one implemented in its context menu. The one here I have implemented myself for the toolbar.
-
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()); }