#include <QApplication>
#include <QWidget>
#include <QLayout>
#include <QScrollArea>
#include <QPainter>
#include <QMenu>
#include <QContextMenuEvent>
#include <QAction>
#include <iostream>
// --------------- WIDGET ---------------- //
{
Q_OBJECT
public:
Widget();
protected:
private:
bool contextM;
};
{
contextM = false;
}
{
p.
setRenderHint(QPainter::Antialiasing);
contextM ? p.
setPen(QColor(255,
0,
0)) : p.
setPen(QColor(120,
120,
120)) ;
p.
drawRoundRect(QRectF(1,
1,width
()-2,height
()-2),
2,
40);
}
{
contextM = true;
update();
contextMenu.addAction(act);
contextMenu.exec(event->globalPos());
std::cout << ": contextMenu.exec() just finished" << std::endl;
contextM = false;
update();
}
// -------------- WIDGETS ---------------- //
{
public:
Widgets();
private:
};
{
resize(600,100);
setWidget(w);
setWidgetResizable(true);
layout->addWidget(new Widget);
layout->addWidget(new Widget);
}
int main(int argc, char* argv[])
{
Widgets mw;
mw.show();
return app.exec();
#include <QApplication>
#include <QWidget>
#include <QLayout>
#include <QScrollArea>
#include <QPainter>
#include <QMenu>
#include <QContextMenuEvent>
#include <QAction>
#include <iostream>
// --------------- WIDGET ---------------- //
class Widget : public QWidget
{
Q_OBJECT
public:
Widget();
protected:
void contextMenuEvent(QContextMenuEvent *event);
void paintEvent(QPaintEvent * event);
private:
bool contextM;
};
Widget::Widget() : QWidget()
{
contextM = false;
}
void Widget::paintEvent(QPaintEvent *event)
{
QPainter p(this);
p.setRenderHint(QPainter::Antialiasing);
contextM ? p.setPen(QColor(255,0,0)) : p.setPen(QColor(120,120,120)) ;
p.drawRoundRect(QRectF(1,1,width()-2,height()-2), 2, 40);
}
void Widget::contextMenuEvent (QContextMenuEvent *event)
{
contextM = true;
update();
QMenu contextMenu;
QAction *act = new QAction( "Dummy Action" ,this);
contextMenu.addAction(act);
contextMenu.exec(event->globalPos());
std::cout << ": contextMenu.exec() just finished" << std::endl;
contextM = false;
update();
}
// -------------- WIDGETS ---------------- //
class Widgets : public QScrollArea
{
public:
Widgets();
private:
QVBoxLayout *layout;
};
Widgets::Widgets() : QScrollArea()
{
resize(600,100);
QWidget *w = new QWidget(this);
setWidget(w);
setWidgetResizable(true);
layout = new QVBoxLayout(w);
layout->addWidget(new Widget);
layout->addWidget(new Widget);
}
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
Widgets mw;
mw.show();
return app.exec();
To copy to clipboard, switch view to plain text mode
}
Bookmarks