qt5 里面怎么没有qqwtplotdirectpainterr类

代码下载链接:& /s/1hsc41Ek 密码: 5hdg
显示效果如下:
代码附有详细注释(代码如下)
* 先新建QMainWindow, 项目名称: DrawWidget 基类选择: QMainWindow,
* 类名默认, 然后在DrawWidget项目名上新建c++class文件, 选择基类: QWidget
5 //先完成绘图区的实现
6 //如下为: drawwidget.h
7 #ifndef DRAWWIDGET_H
8 #define DRAWWIDGET_H
10 #include &QWidget&
11 #include &QtGui&
12 #include &QMouseEvent&
13 #include &QPaintEvent&
14 #include &QResizeEvent&
15 #include &QColor&
16 #include &QPixmap&
17 #include &QPoint&
18 #include &QPainter&
19 #include &QPalette&
21 class DrawWidget : public QWidget
24 public:
explicit DrawWidget(QWidget *parent = 0);
//鼠标事件重定义
void mousePressEvent (QMouseEvent *);
void mouseMoveEvent (QMouseEvent *);
//重画事件重定义
void paintEvent (QPaintEvent *);
//尺寸变化事件重定义
void resizeEvent (QResizeEvent *);
33 signals:
34 public slots:
void setStyle (int);
void setWidth (int);
void setColor (QColor);
void clear ();
39 private:
QPoint startP
QPoint endP
48 #endif // DRAWWIDGET_H
1 //drawwidget.cpp
2 //DrawWidget构造函数完成对窗体参数及部分功能的初始化工作
3 #include "drawwidget.h"
4 #include &QtGui&
5 #include &QPen&
7 DrawWidget::DrawWidget(QWidget *parent) : QWidget(parent)
setAutoFillBackground (true);
//对窗体背景色的设置
setPalette (QPalette(Qt::white));
//背景色为白
pix = new QPixmap(size());
//此QPixmap对象用来准备随时接受绘制的内容
pix-&fill (Qt::white);
//填充背景色为白色
setMinimumSize (600, 400);
//设置绘制区窗体的最小尺寸
16 //接受主窗体传来的线型风格参数
17 void DrawWidget::setStyle (int s)
22 //setWidth()接受主窗体传来的线宽参数值
23 void DrawWidget::setWidth (int w)
28 //接受主窗体传来的画笔颜色值
29 void DrawWidget::setColor (QColor c)
34 //重定义鼠标按下事件--按下鼠标时,记录当前鼠标位置值startPos
35 void DrawWidget::mousePressEvent (QMouseEvent *e)
startPos = e-&pos ();
40 //重定义鼠标移动事件--默认情况下,在鼠标按下的同时拖曳鼠标时被触发.
41 //mouseTracking事件,可以通过设置setMouseTracking(bool enable)为true,
42 //则无论是否有鼠标键按下,只要鼠标移动,就会触发mouseMoveEvent()
43 //在此函数中,完成向QPixmap对象中绘图的工作.
44 void DrawWidget::mouseMoveEvent (QMouseEvent *e)
QPainter *painter = new QP
//新建一个QPainter对象
//新建一个QPen对象
//设置画笔的线型,style表示当前选择的线型是Qt::PenStyle枚举数据中的第几个元素
pen.setStyle ((Qt::PenStyle)style);
pen.setWidth (weight);
//设置画笔的线宽值
pen.setColor (color);
//设置画笔的颜色
* 以QPixmap对象为QPaintDevice参数绘制,构造一个QPainter对象,
* 就立即开始对绘画设备进行绘制,此构造QPainter对象是短期的
* 由于当一个QPainter对象的初始化失败时构造函数不能提供反馈信息,
* 所以在绘制 外部设备时 应使用begin()和end()(Ps:如打印机外部设备)
painter-&begin (pix);
painter-&setPen (pen);
//将QPen对象应用到绘制对象当中
//绘制从startPos到鼠标当前位置的直线
painter-&drawLine (startPos, e-&pos ());
painter-&end ();
//绘制成功返回true
startPos = e-&pos ();
//更新鼠标的当前位置,为下次绘制做准备
update ();
//重绘绘制区窗体
* 重画函数paintEvent()完成绘制区窗体的更新工作,只需要调用drawPixmap()函数将用于接收图形绘制的
* 的QPixmap对象绘制在绘制区窗体控件上即可.
71 void DrawWidget::paintEvent (QPaintEvent *)
QPainter painter(this);
painter.drawPixmap (QPoint(0,0), *pix);
* 调整绘制区大小函数resizeEvent():
* 当窗体大小改变是,实际能够绘制的区域仍然没有改变,因为绘图的大小没有改变
* 所以窗体尺寸变化时,应及时调整用于绘制的QPixmap对象的尺寸大小
82 void DrawWidget::resizeEvent (QResizeEvent *event)
//判断改变后的窗体长或宽是否大于原窗体的长和宽;
//若大于则进行相应调整;
if (height () & pix-&height () || width () & pix-&width ())
QPixmap *newPix = new QPixmap(size());
//创建一个新的QPixmap对象
newPix-&fill (Qt::white);
//填充新QPixmap对象newPix的颜色为白色背景色
QPainter p(newPix);
p.drawPixmap (QPoint(0, 0), *pix);
//在newPix中绘制原pix中内容
pix = newP
//将newPix赋值给Pix作为新的绘制图形接收对象
//否则直接调用QWidget的resizeEvent()函数返回
QWidget::resizeEvent (event);
//完成其余工作
* clear()函数完成绘制区的清除工作,只需要一个新的,干净的QPixmap对象代替pix,并调用update()重绘即可
102 void DrawWidget::clear ()
QPixmap *clearPix = new QPixmap(size());
clearPix-&fill (Qt::white);
pix = clearP
update ();
1 //以上为能够响应鼠标事件进行绘图功能的窗体类实现
2 //主窗口的实现
3 //mainwindow.h
4 #ifndef MAINWINDOW_H
5 #define MAINWINDOW_H
7 #include &QMainWindow&
8 #include &QToolButton&
9 #include &QLabel&
10 #include &QComboBox&
//下拉列表框
11 #include &QSpinBox&
12 #include "drawwidget.h"
14 class MainWindow : public QMainWindow
18 public:
MainWindow(QWidget *parent = 0);
~MainWindow();
void createToolBar();
//创建工具栏
22 public slots:
void ShowStyle();
//进行选择线型风格的槽函数
void ShowColor();
//选择颜色的槽函数
25 private:
DrawWidget *drawW
//创建能够响应鼠标事件进行绘图功能的窗体类
QLabel *styleL
QComboBox *styleComboB
QLabel *widthL
QSpinBox *widthSpinB
//线宽自旋框
QToolButton *colorB
//颜色工具
QToolButton *clearB
//清除按钮
35 #endif // MAINWINDOW_H
1 //mainwindow.cpp
2 #include "mainwindow.h"
3 #include &QToolBar&
4 #include &QColorDialog&
6 MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
drawWidget = new DrawW
//新建一个DrawWidget对象--能够响应鼠标事件进行绘图功能的窗体类
setCentralWidget (drawWidget);
//新建的DrawWidget对象作为主窗口的中央窗体
createToolBar ();
//实现一个工具栏
setMinimumSize (600, 400);
//设置主窗口的最小尺寸
ShowStyle ();
//初始化线型,设置控件中的当前值作为初始值
drawWidget-&setWidth (widthSpinBox-&value ());
//初始化线宽
drawWidget-&setColor (Qt::black);
//初始化颜色
18 //工具栏创建
19 void MainWindow::createToolBar ()
QToolBar *toolBar = addToolBar ("Tool");
//为主窗口新建一个工具栏对象
styleLabel = new QLabel(tr("线型风格: "));
//创建线性选择控件
styleComboBox = new QComboB
styleComboBox-&addItem (tr("SolidLine"),
static_cast&int&(Qt::SolidLine));
styleComboBox-&addItem (tr("DashLine"),
static_cast&int&(Qt::DashLine));
styleComboBox-&addItem (tr("DotLine"),
static_cast&int&(Qt::DotLine));
styleComboBox-&addItem (tr("DashDotLine"),
static_cast&int&(Qt::DashDotLine));
styleComboBox-&addItem (tr("DashDotDotLine"),
static_cast&int&(Qt::DashDotDotLine));
connect (styleComboBox, SIGNAL(activated(int)), this, SLOT(ShowStyle()));
//关联相应的槽函数
widthLabel = new QLabel(tr("线宽: "));
//创建线宽选择控件
widthSpinBox = new QSpinB
connect (widthSpinBox, SIGNAL(valueChanged(int)), drawWidget, SLOT(setWidth(int)));
colorBtn = new QToolB
//创建颜色选择控件
QPixmap pixmap(20, 20);
//颜色选择按钮控件上的图像
pixmap.fill (Qt::black);
//填充黑色
colorBtn-&setIcon (QIcon(pixmap));
//设置按钮图像
connect (colorBtn, SIGNAL(clicked(bool)), this, SLOT(ShowColor()));
clearBtn = new QToolButton();
//创建清除按钮
clearBtn-&setText (tr("清除"));
connect (clearBtn, SIGNAL(clicked(bool)), drawWidget, SLOT(clear()));
toolBar-&addWidget (styleLabel);
toolBar-&addWidget (styleComboBox);
toolBar-&addWidget (widthLabel);
toolBar-&addWidget (widthSpinBox);
toolBar-&addWidget (colorBtn);
toolBar-&addWidget (clearBtn);
57 //ShowStyle(),通过调用DrawWidget类的setStyle()函数将当前线型选择控件中的线型参数传给绘制区;
58 void MainWindow::ShowStyle ()
drawWidget-&setStyle (styleComboBox-&itemData (styleComboBox-&currentIndex (),
Qt::UserRole).toInt ());
64 //ShowColor(),通过DrawWidget类的setColor()函数将用户在标准颜色对话框中选择的颜色值传给绘制区
65 void MainWindow::ShowColor ()
QColor color = QColorDialog::getColor (static_cast&int&(Qt::black));
//默认为黑(static_cast&int&转换成int节省内存
//使用标准颜色对话框QColorDialog获得一个颜色值
if (color.isValid ())
//先将新选择的颜色传给绘制区,用于改变画笔的颜色值
drawWidget-&setColor (color);
//改变按钮图案
QPixmap p(20, 20);
//设置图像大小
p.fill (color);
//填充颜色
colorBtn-&setIcon (QIcon(p));
//设置颜色按钮图案
81 MainWindow::~MainWindow()
1 //main.cpp
2 #include "mainwindow.h"
3 #include &QApplication&
4 #include &QFont&
6 int main(int argc, char *argv[])
QApplication a(argc, argv);
QFont font("ZYSong18030", 12);
a.setFont (font);
return a.exec();
阅读(...) 评论()为了在嵌入式上解决界面刷新闪烁问题,QT提出了两种解决方案:
1、对控件设置Qt::WA_PaintOnScreen
that the widget wants to draw directly onto the screen. Widgets with this attribute set do not participate in composition management, i.e. they cannot be semi-transparent or shine through semi-transparent overlapping widgets.&Note:&This
flag is only supported on X11 and it disables double buffering. On Qt for Embedded Linux, the flag only works when set on a top-level widget and it relies on support from the active screen driver. This flag is set or cleared by the widget's author. To render
outside of Qt's paint system, e.g., if you require native painting primitives, you need to reimplement&() to return 0 and set this flag.
2、使用QDirectPainter直接在Framebuffer上绘图
QDirectPainter支持3种模式,一般我们使用NonReserved即可:
QDirectPainter mPainter(this)
QDirectPainter::lock();
mPainter.setRegion(requsteRegion);
QRegion allocatedregion=mPainter.allocatedRegion();
mPainter.startPainting();
//结合allocatedregion在QDirectPainter::framebuffer()指向的缓存中直接绘图。
mPainter.endPainting();
QDirectPainter::unlock();
需要注意allocatedRegion返回值
&QDirectPainter::allocatedRegion&()
Returns the currently reserved region.
Note that if the&&flag is set, the region returned by this function will always be equivalent to the region returned by the&()
function. Otherwise they might differ (see&&for details).
对于LCD分辨率640*480的显示器来说,居中有一个大小为100*100的window,当setRegion一般传入640*480后,allcatedRegion会返回一个QRegion,这个QRegion包括4个Rect,用于表明除中间100*100外的LCD其他区域。 (前提条件是window必须有focus)
在项目的应用中,我需要一个窗口显示摄像头采集到的视频图像,并且尽快实时刷新,此窗口有标题栏用于显示状态。同时,一定条件下,需要在视频上弹出一个对话框但不能影响不被对话框遮挡的区域的视频刷新。方法如下:
建立一个Window和Dialog,Window作为主UI,并建立定时器。
定时器实时获取AllocatedRegion后,将摄像头的数据直接刷新到FrameBuffer上。
当Dialog没有显示时,视频的刷新区域是除Window外的所有屏幕区域。
当Dialog显示时,视频的刷新区域还要减去Dialog的显示区域。
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:56394次
积分:1263
积分:1263
排名:千里之外
原创:63篇
转载:17篇
(2)(1)(3)(6)(1)(4)(3)(5)(9)(8)(5)(13)(3)(1)(2)(2)(3)(4)(3)(2)(1)(1)您的访问出错了(404错误)
很抱歉,您要访问的页面不存在。
1、请检查您输入的地址是否正确。
进行查找。
3、感谢您使用本站,3秒后自动跳转至网站首页QStylePainter Class | Qt Widgets 5.9
QStylePainter
QStylePainter Class
class is a convenience class for drawing
elements inside a widget.
Header: #include &QStylePainter&
qmake: QT += widgets Inherits:
Public Functions
(QWidget *widget)
(QPaintDevice *pd, QWidget *widget)
bool (QWidget *widget)
bool (QPaintDevice *pd, QWidget *widget)
void (QStyle::ComplexControl cc, const QStyleOptionComplex &option)
void (QStyle::ControlElement ce, const QStyleOption &option)
void (const QRect &rect, int flags, const QPixmap &pixmap)
void (const QRect &rect, int flags, const QPalette &pal, bool enabled, const QString &text, QPalette::ColorRole textRole = QPalette::NoRole)
void (QStyle::PrimitiveElement pe, const QStyleOption &option)
QStyle *() const
187 public functions inherited from
Detailed Description
class is a convenience class for drawing
elements inside a widget.
with a set of high-level draw...() functions implemented on top of 's API. The advantage of using
is that the parameter lists get considerably shorter. Whereas a
object must be able to draw on any widget using any painter (because the application normally has one
object shared by all widget), a
is initialized with a widget, eliminating the need to specify the , the , and the
for every function call.
Example using
void MyWidget::paintEvent( * )
painter(this);
option.initFrom(this);
option.backgroundColor = palette().color(::Background);
style()-&drawPrimitive(::PE_FrameFocusRect, &option, &painter, this);
Example using :
void MyWidget::paintEvent( * )
painter(this);
option.initFrom(this);
option.backgroundColor = palette().color(::Background);
painter.drawPrimitive(::PE_FrameFocusRect, option);
Member Function Documentation
QStylePainter::QStylePainter()
Constructs a .
QStylePainter::QStylePainter( *widget)
Construct a
using widget widget for its paint device.
QStylePainter::QStylePainter( *pd,
Construct a
using pd for its paint device, and attributes from widget.
bool QStylePainter::begin( *widget)
Begin painting operations on the specified widget. Returns true if the pai otherwise returns false.
This is automatically called by the constructor that takes a .
bool QStylePainter::begin( *pd,
This is an overloaded function.
Begin painting operations on paint device pd as if it was widget.
This is automatically called by the constructor that takes a
void QStylePainter::drawComplexControl( cc, const
Use the widget's style to draw a complex control cc specified by the
See also ().
void QStylePainter::drawControl( ce, const
Use the widget's style to draw a control element ce specified by
See also ().
void QStylePainter::drawItemPixmap(const
&rect, int flags, const
Draws the pixmap in rectangle rect. The pixmap is aligned according to flags.
See also () and .
void QStylePainter::drawItemText(const
&rect, int flags, const
&pal, bool enabled, const
textRole = QPalette::NoRole)
Draws the text in rectangle rect and palette pal. The text is aligned and wrapped according to flags.
The pen color is specified with textRole. The enabled bool indicates whether or not when reimplementing this bool should influence how the item is drawn.
See also () and .
void QStylePainter::drawPrimitive( pe, const
Use the widget's style to draw a primitive element pe specified by
See also ().
*QStylePainter::style() const
Return the current style used by the .
(C) 2017 The Qt Company Ltd.
Documentation contributions included herein are the copyrights of
their respective owners.
The documentation provided herein is licensed under the terms of the
as published by the Free Software Foundation.
Qt and respective logos are trademarks of The Qt Company Ltd.
in Finland and/or other countries worldwide. All other trademarks are property
of their respective owners.关于Qt中的写屏类 QDirectPainter,该怎么解决 - QT开发当前位置:& &&&关于Qt中的写屏类 QDirectPainter,该怎么解决关于Qt中的写屏类 QDirectPainter,该怎么解决&&网友分享于:&&浏览:34次关于Qt中的写屏类 QDirectPainter小弟想用Qt直接写屏,快速画波形,又可以管理窗口,QDirectPainter类可以实现。但我把类中的标志改成QDirectPainter::NonReserved的话,就总写不了屏,用QDirectPainter::Reserved是可以在指定区域静态写屏的。但我的目的是写屏的区域可以被其它窗体盖上去的,不要因为这写屏而把在这区域的窗体都擦掉了,用了raise(),和lower()也是没有用的。
&&&&QFrame&*frame&=new&QF
&&&&frame-&setGeometry(0,0,640,480);
&&&//&QDirectPainter&pain(frame,QDirectPainter::NonReserved);
&&&&QDirectPainter&pain(frame,QDirectPainter::NonReserved);//&Reserved&ReservedSynchronous
&&//&pain.lower();
&&&&pain.startPainting();
&&&&pain.setRegion(QRegion(0,0,100,300));
&&&&uchar&*fb&=&pain.frameBuffer();
&&&&qint32&i=0;
&&&&&for(i=0;i&256000;i++)
&&&&&&&&*(fb+i)&=&65535;
&&&&&&&//&printf("fdsfsf\n");
&//&&pain.flush(pain.allocatedRegion());
&&&&pain.endPainting(pain.allocatedRegion());
请问,这类应该怎么用呢?------解决方案--------------------这不矛盾么?&你又直接写屏,&又想Qt的组件跑你前面------解决方案--------------------画一个图片,作桌面背景
12345678910
12345678910
12345678910 上一篇:下一篇:文章评论相关解决方案 12345678910 Copyright & &&版权所有}

我要回帖

更多关于 qt painter 清除 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信