C++ (Qt)QWebHitTestResult r = page()->mainFrame()->hitTestContent(event->pos())
m_view = new QWebView(this);m_view->setContextMenuPolicy(Qt::CustomContextMenu); connect(m_view, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(showWebViewContextMenu(QPoint)));
void Webbrowser::showWebViewContextMenu(const QPoint &point){ QWebHitTestResult r = m_view->page()->mainFrame()->hitTestContent(point); if(r.isContentEditable()) { QMenu menu(QLatin1String(""), m_view); QAction *cut_action = menu.addAction(tr("Вырезать")); QAction *copy_action = menu.addAction(tr("Копировать")); QAction *paste_action = menu.addAction(tr("Вставить")); QAction *del_action = menu.addAction(tr("Удалить")); menu.addSeparator(); QAction *selectAll_action = menu.addAction(tr("Выделить все")); if(!m_view->selectedText().isEmpty()) { cut_action->setEnabled(true); copy_action->setEnabled(true); del_action->setEnabled(true); } else { cut_action->setEnabled(false); copy_action->setEnabled(false); del_action->setEnabled(false); } if(!QApplication::clipboard()->text().isEmpty()) paste_action->setEnabled(true); else paste_action->setEnabled(false); QAction *picked_action = menu.exec(m_view->mapToGlobal(point)); if(!picked_action) return; if(picked_action == cut_action) m_view->triggerPageAction(QWebPage::Cut); if(picked_action == copy_action) m_view->triggerPageAction(QWebPage::Copy); if(picked_action == paste_action) m_view->triggerPageAction(QWebPage::Paste); if(picked_action == del_action) m_view->triggerPageAction(QWebPage::DeleteEndOfWord); if(picked_action == selectAll_action) m_view->triggerPageAction(QWebPage::SelectAll); } else if(r.isContentSelected() && !m_view->selectedText().isEmpty()) { QMenu menu(QLatin1String(""), m_view); QAction *copy_action = menu.addAction(tr("Копировать")); QAction *selectAll_action = menu.addAction(tr("Выделить все")); QAction *picked_action = menu.exec(m_view->mapToGlobal(point)); if(!picked_action) return; if(picked_action == copy_action) m_view->triggerPageAction(QWebPage::Copy); if(picked_action == selectAll_action) m_view->triggerPageAction(QWebPage::SelectAll); }}
if(!QApplication::clipboard()->text().isEmpty() && ФОКУС_НА_ЭЛЕМЕНТЕ_ВВОДА) paste_action->setEnabled(true); else paste_action->setEnabled(false);