PDA

View Full Version : Connect statement problem



Bender_Rodriguez
13th April 2013, 01:53
Hi, everybody!
Please, help me.
There is a small problem with my code.
Well, the first part of the code I took from one famous book.
So, I decided to make the second part myself.
But it doesn't work...
The problem is in the connect statement :

ui->setupUi(this);
this->setWindowTitle(QString("%1 - %2" ).arg("unnamed").arg("SDI"));

QTextEdit* docWidget = new QTextEdit( this );
setCentralWidget( docWidget );
connect( docWidget->document(), SIGNAL(modificationChanged(bool)),
this, SLOT(setWindowModified(bool)) );

QAction* cutAction = new QAction( QIcon("C:/icon/icon.png"), tr("Cu&t"), this );
cutAction->setShortcut( tr("Ctrl+X") );
cutAction->setStatusTip( tr("Cut") );
cutAction->setEnabled(false);
connect( docWidget, SIGNAL(copyAvailable(bool)),
cutAction, SLOT(setEnabled(bool)) );
connect( cutAction, SIGNAL(triggered()), docWidget, SLOT(cut()));

.......

QAction* pasteAction = new QAction(QIcon("C:/icon/paste.png"), tr("&Paste"), this);
pasteAction->setShortcut(tr("Ctrl+V"));
pasteAction->setStatusTip(tr("Paste"));
pasteAction->setEnabled(false);

connect(..., SIGNAL(...), pasteAction, SLOT(setEnabled(bool)));
connect(pasteAction, SIGNAL(triggered()), docWidget, SLOT(paste()));

I can't understand, which one object (e.g., cutAction, docWidget->document() or this-pointer) should emit which one signal (if exist :) ) to enable pasteAction?

Many thanks.

ChrisW67
13th April 2013, 07:40
I would think about using the QClipboard::dataChanged() signal to trigger a slot that enables the paste action if the clipboard data is of a suitable type.

Bender_Rodriguez
13th April 2013, 20:22
I would think about using the QClipboard::dataChanged() signal

Unfortunatley, dataChanged() signal doesn't take any bool parameter. So the signal and setEnabled(bool) aren't compatible :(

There is one interesting fact.
I found exactly the same example project in official reference documentation.
And you know what did they do to solve this problem?
*drum-roll*
Nothing!
They just ignored this situation (paste function is enabled from the beginning)! :confused:
The point is: no dead body - no case :D

ChrisW67
14th April 2013, 00:41
I didn't tell you to connect it directly to the action enabled() slot. The clipboard can contain data you cannot paste or be empty, in which case you still do not want the paste action active.

Something like this during setup:


myUpdateClipboardStateSlot(); // sets the initial state
connect(qApp->clipboard(), SIGNAL(dataChanged()), SLOT(myUpdateClipboardStateSlot())); // and follows changes

and then:


void MyObject::myUpdateClipboardStateSlot()
{
const QStringList formats = qApp->clipboard()->mimeData()->formats();
if ( [[a format I can handle is in the list]] ) // or you can call hasText(), hasHtml()...
pasteAction.setEnabled(true);
else
pasteAction.setEnabled(false);
}



It's also hinted at in the Drag and Drop docs

Bender_Rodriguez
14th April 2013, 21:34
The clipboard can contain data you cannot paste or be empty, in which case you still do not want the paste action active.

You are obviously right.


Something like this during setup:


and then:

Now I get it. Thanks.
The only way is to wright additional code.
Keep it simple (c) :)


It's also hinted at in the Drag and Drop docs

Good to know.