#include #include #include #include #include #include #include #include #include class Popup : public QFrame { public: Popup() //: QFrame(nullptr) // OKE : QFrame(nullptr, Qt::Popup) // NOT OKE { Setup(); } void SetInitialText(const QString& t) { txt->setText(t); } private: void ShowContextMenu(const QPoint& pt) { auto menu = txt->createStandardContextMenu(); auto custom = menu->addAction("Formatted copy"); connect(custom, &QAction::triggered, this, &Popup::CustomActionTriggered); menu->exec(this->mapToGlobal(pt)); delete menu; } void CustomActionTriggered(bool) { qInfo() << "Custom action triggered"; } void Setup() { layout = new QVBoxLayout(this); txt = new QTextEdit(this); txt->setContextMenuPolicy(Qt::CustomContextMenu); txt->setReadOnly(true); connect(txt, &QTextEdit::customContextMenuRequested, this, &Popup::ShowContextMenu); layout->addWidget(txt); } QVBoxLayout* layout; QTextEdit* txt; }; void CreatePopup() { auto popup = new Popup(); popup->setAttribute(Qt::WA_DeleteOnClose); popup->SetInitialText("Some text to display"); popup->show(); } int main(int argc, char** argv) { QApplication app(argc, argv); QWidget window; QAction* showDialog = new QAction("Show dialog", &window); QObject::connect(showDialog, &QAction::triggered, []{ CreatePopup(); }); window.addAction(showDialog); window.setContextMenuPolicy(Qt::ActionsContextMenu); window.show(); return app.exec(); }