1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
|
MyView:: MyView(QWidget* parent): QWidget(parent)
{
pEdit = new QTextEdit(this);
pEdit->setReadOnly(true);
pEdit->setAcceptRichText(true);
pEdit->setContextMenuPolicy(::Qt::CustomContextMenu);
QObject::connect(pEdit, SIGNAL(customContextMenuRequested(const QPoint&)), this, SLOT(showContextMenu(const QPoint&)));
QObject::connect(pEdit, SIGNAL(copyAvailable(bool)), this, SLOT(copyAvailable(bool)));
layout->addWidget(pEdit);
pClearAction = new QAction(icon, QString("&Clear"), this);
pClearAction->setStatusTip(tr("Clear console"));
pClearAction->setToolTip (tr("Clear console"));
pClearAction->setEnabled(true);
QObject::connect(pClearAction, SIGNAL(triggered()), this, SLOT(onClear()));
}
void MyView::showContextMenu(const QPoint& pt)
{
QMenu *menu = pEdit->createStandardContextMenu();
foreach( QAction* action, menu->actions())
{
QKeySequence shortCut = action->shortcut();
if (QKeySequence::Copy == shortCut && actionConnected == false)
{
actionConnected = connect(action, SIGNAL(triggered(bool)), pEdit, SLOT(copy()));
}
}
menu->addAction(pClearAction);
QAction * action = menu->exec(pEdit->mapToGlobal(pt));
if ( action )
{
QKeySequence shortCut = action->shortcut();
QString shLabel = action->shortcut().toString();
QString label = action->text();
if (label == "&Copy Ctrl+C" )
{
// QApplication::clipboard()->clear(QClipboard::Clipboard);
// QApplication::clipboard()->setText( pEdit->toPlainText(), QClipboard::Clipboard);
emit pEdit->copy();
}
}
delete menu;
}
void MyView::copyAvailable(bool yes)
{
// TO TEST
if ( yes )
{
QApplication::clipboard()->clear(QClipboard::Clipboard);
emit pEdit->copy();
}
// QApplication::clipboard()->setText( pEdit->toPlainText(), QClipboard::Clipboard);
}
void MyView::onClear()
{
if(pEdit != NULL)
emit pEdit->clear();
} |
Partager