ChajusSaib
ChajusSaib

Reputation: 167

Enable and disable paste action

How would I enable and disable the paste action to act just like QTextEdit's context menu paste action? So when the clipboard has something, the paste button should be enabled and when I clear the clipboard the paste button should be disabled. This however does not happen to my action that I have created.

Here is what I have so far:

TextEditor::TextEditor(QWidget *parent) :
    ...
{
    ...
    connect(QApplication::clipboard(), SIGNAL(dataChanged()), this, SLOT(processPaste()));
    ....
}

    void TextEditor::processPaste()
{
    if (const QMimeData *md = QApplication::clipboard()->mimeData())
            ui->actionPaste->setEnabled(md->hasText());
}

When the program launches and the clipboard is empty then the action is disabled just like expected, when I copy something the action enables, again just like expected but when I clear the clipboard the paste action is still enabled when it should be disabled like the QTextMenu's paste action.

Thank you!

Upvotes: 0

Views: 946

Answers (1)

Mehrdad
Mehrdad

Reputation: 1256

I think you should do this differently, instead of waiting for changed signal, connect your slot to menu's aboutToShow signal. And check for clipboard's state then. This way your code will be called when menu is about to show and you can update the action's state accordingly.

Also note that Qt doc mentions that in OS X clipboard's dataChanged() signal won't work if your application isn't active. Another reason to not relying on that.

Upvotes: 1

Related Questions