int main(int argc, char *argv[]){ QApplication a(argc, argv); Widget w; QLineEdit ln(&w); w.show(); return a.exec();}
void Widget::contextMenuEvent(QContextMenuEvent *event){ QMenu menu(this); menu.addAction(QPixmap("C:\\Qt\\pro\\popupmenu-build-desktop\\debug\\ico.png"), "ShowMessage", this, SLOT(msg())); menu.addAction("About Qt", qApp, SLOT(aboutQt())); menu.addSeparator(); menu.addAction("Exit", qApp, SLOT(qApp->exit())); menu.exec(event->globalPos());}
#include <QtGui/QApplication>#include <QLineEdit>#include <QMenu>#include <QMouseEvent>class myLineEdit : public QLineEdit{public: myLineEdit(QWidget *p = 0) :QLineEdit(p) {} virtual void mousePressEvent ( QMouseEvent *event ) { if( event->button() == Qt::RightButton) { QMenu menu(this); menu.addAction("ShowMessage"); menu.addAction("About Qt"); menu.addSeparator(); menu.addAction("Exit"); menu.exec(event->globalPos()); } else QLineEdit::mousePressEvent(event); }};int main(int argc, char *argv[]){ QApplication a(argc, argv); QWidget w; QLineEdit *ln = new myLineEdit(&w); w.show(); return a.exec();}
C++ (Qt)#include <QApplication>#include <QLineEdit>#include <QAction> int main(int argc, char *argv[]){ QApplication a(argc, argv); QLineEdit le; le.show(); le.addAction(new QAction(QObject::tr("qwe"),&le)); le.addAction(new QAction(QObject::tr("asd"),&le)); le.setContextMenuPolicy(Qt::ActionsContextMenu); return a.exec();}
#include <QtGui>#include "widget.h"void Widget::sw(){ QMessageBox::information(0, "Information", "Information message text");}int main(int argc, char *argv[]){ QApplication a(argc, argv); Widget w; QLineEdit le(&w); QAction *mAct, *sAct; mAct = new QAction("qwe", &le); sAct = new QAction("asd", &le); le.addAction(mAct); le.addAction(sAct); le.setContextMenuPolicy(Qt::ActionsContextMenu); le.connect(mAct, SIGNAL(triggered()), &w, SLOT(sw())); w.show(); return a.exec();}
...if( event->button() == Qt::RightButton){ QMenu *menu = createStandardContextMenu(); menu.addAction("ShowMessage"); menu.addAction("About Qt"); menu.addSeparator(); menu.addAction("Exit"); menu.exec(event->globalPos());}....
C++ (Qt) QAction* sep = new QAction(le); sep->setSeparator(1); le->addAction(sep);
C++ (Qt) QAction *mAct, *sAct; mAct = new QAction("qwe", &le); sAct = new QAction("asd", &le); le.addAction(mAct); le.addAction(sAct); le.setContextMenuPolicy(Qt::ActionsContextMenu); le.connect(mAct, SIGNAL(triggered()), &w, SLOT(sw()));
C++ (Qt) le.addAction("qwe", &w, SLOT(sw())); le.addAction("asd"); le.setContextMenuPolicy(Qt::ActionsContextMenu);