QApplication app(argc, argv); QWidget win; QPainter pip;pip.begin(&win);pip.setBrush(QBrush(Qt::red,Qt::DiagCrossPattern));pip.setPen(QPen(Qt::blue,3,Qt::DashLine));pip.drawRect((QRect(10,10,30,30)));pip.end();win.show();
class Window : public QLabel {Q_OBJECTprivate: QPoint m_ptPosition;protected: virtual void Window::mousePressEvent(QMouseEvent* pe) { m_ptPosition = pe->pos(); } virtual void Window::mouseMoveEvent(QMouseEvent* pe) { move(pe->globalPos() - m_ptPosition); } virtual void keyPressEvent(QKeyEvent* e);public: Window(QWidget* pwgt = 0) : QLabel(pwgt,Qt::FramelessWindowHint | Qt::Window) { }void paintEvent( QPaintEvent * );};void Window::paintEvent( QPaintEvent * e){ QPainter pip; pip.begin(this); pip.drawPixmap(0,0,QPixmap("img/screen.png")); pip.setBrush(QBrush(Qt::red,Qt::DiagCrossPattern)); pip.setPen(QPen(Qt::blue,3,Qt::DashLine)); pip.drawRect((QRect(10,10,30,30))); pip.end();}void Window::keyPressEvent(QKeyEvent* e);{ if(e->key()==Key_Return) exit(0);}
C++ (Qt)void Window::keyPressEvent(QKeyEvent* e);
{ QPainter pip; pip.begin(this);//... pip.end();
{ QPainter pip(this);//...
#include <qapplication.h>#include<qpixmap.h>#include <qlabel.h>#include <QMouseEvent>#include<qwidget.h>#include<qbitmap>#include <qpainter.h>class Window : public QLabel {Q_OBJECTprivate: QPoint m_ptPosition;protected: virtual void Window::mousePressEvent(QMouseEvent* pe) { m_ptPosition = pe->pos(); } virtual void Window::mouseMoveEvent(QMouseEvent* pe) { move(pe->globalPos() - m_ptPosition); } public: Window(QWidget* pwgt = 0) : QLabel(pwgt,Qt::FramelessWindowHint | Qt::Window) { }void paintEvent( QPaintEvent * );void keyPressEvent(QKeyEvent* e);};void Window::paintEvent( QPaintEvent * ){ QPainter pip; pip.begin(this); pip.drawPixmap(0,0,QPixmap("img/screen.png")); pip.setBrush(QBrush(Qt::red,Qt::DiagCrossPattern)); pip.setPen(QPen(Qt::blue,3,Qt::DashLine)); pip.drawRect((QRect(10,10,30,30))); pip.end();}void Window::keyPressEvent(QKeyEvent* e){ if(e->key()==Qt::Key_Return) exit(0);}// ----------------------------------------------------------------------int main(int argc, char** argv){ QApplication app(argc, argv); Window win; QPixmap pix("img/screen.png"); win.setPixmap(pix); win.setMask(pix.mask()); win.show(); return app.exec();}
C++ (Qt)#include "moc_xxxxx.cpp"