paint->setBrush(QBrush(Qt::greelsefor(m=1;m<=k;m )

温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!&&|&&
LOFTER精选
&&在myListview()中: m_InformationPositionModel = new InformationPositionModel(infoItem); this-&setModel(m_InformationPositionModel);
this-&setMouseTracking(true); this-&setFocusPolicy(Qt::NoFocus); this-&setGridStyle(Qt::NoPen);& this-&setSelectionBehavior(QAbstractItemView::SelectItems); this-&setSelectionMode(QAbstractItemView::SingleSelection); QHeaderView* headerView = new&InformationPositionSubHeaderview(Qt::Horizontal,this); ////=====/////// headerView-&setSectionsClickable(true); this-&setHorizontalHeader(headerView); this-&horizontalHeader()-&setStretchLastSection(true); this-&verticalHeader()-&setVisible(false);
this-&setEditTriggers(QAbstractItemView::NoEditTriggers); this-&setContextMenuPolicy(Qt::CustomContextMenu);
&& this-&resizeColumnsToContents(); this-&resizeRowsToContents();其:InformationPositionSubHeaderview::InformationPositionSubHeaderview(Qt::Orientation orientation,QWidget* parent) : QHeaderView(orientation,parent){ this-&setSectionsClickable(false); this-&m_IsHided =}InformationPositionSubHeaderview::~InformationPositionSubHeaderview(){}void InformationPositionSubHeaderview::paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const{& & painter-&save(); QHeaderView::paintSection(painter, rect, logicalIndex);& painter-&restore(); if(logicalIndex==1 && m_IsHided==true) {
QRect leftRect = QRect(rect.x(),rect.y(),HIDEDCOLORBTNWIDTH,rect.height());
painter-&setBrush(QBrush(Qt::blue));
painter-&drawRect(leftRect); }}void InformationPositionSubHeaderview::mousePressEvent(QMouseEvent * event){ if(event-&button()==Qt::LeftButton)& {
int index = logicalIndexAt(event-&pos());
if(index==0 && m_IsHided==false)
setSectionHidden(index,true);
m_IsHided =
else if(index==1 && m_IsHided==true && event-&pos().x()&=HIDEDCOLORBTNWIDTH)
setSectionHidden(index-1,false);
m_IsHided =
} } QHeaderView::mousePressEvent(event);}
阅读(988)|
用微信&&“扫一扫”
将文章分享到朋友圈。
用易信&&“扫一扫”
将文章分享到朋友圈。
历史上的今天
loftPermalink:'',
id:'fks_',
blogTitle:'Qt---设置QTableView的QHeaderView的某section(列)隐藏',
blogAbstract:'示意图:&&在myListview()中:\tm_InformationPositionModel = new InformationPositionModel(infoItem);\tthis-&setModel(m_InformationPositionModel);',
blogTag:'隐藏列,qheaderview,section',
blogUrl:'blog/static/',
isPublished:1,
istop:false,
modifyTime:7,
publishTime:7,
permalink:'blog/static/',
commentCount:0,
mainCommentCount:0,
recommendCount:0,
bsrk:-100,
publisherId:0,
recomBlogHome:false,
currentRecomBlog:false,
attachmentsFileIds:[],
groupInfo:{},
friendstatus:'none',
followstatus:'unFollow',
pubSucc:'',
visitorProvince:'',
visitorCity:'',
visitorNewUser:false,
postAddInfo:{},
mset:'000',
remindgoodnightblog:false,
isBlackVisitor:false,
isShowYodaoAd:false,
hostIntro:'',
hmcon:'1',
selfRecomBlogCount:'0',
lofter_single:''
{list a as x}
{if x.moveFrom=='wap'}
{elseif x.moveFrom=='iphone'}
{elseif x.moveFrom=='android'}
{elseif x.moveFrom=='mobile'}
${a.selfIntro|escape}{if great260}${suplement}{/if}
{list a as x}
推荐过这篇日志的人:
{list a as x}
{if !!b&&b.length>0}
他们还推荐了:
{list b as y}
转载记录:
{list d as x}
{list a as x}
{list a as x}
{list a as x}
{list a as x}
{if x_index>4}{break}{/if}
${fn2(x.publishTime,'yyyy-MM-dd HH:mm:ss')}
{list a as x}
{if !!(blogDetail.preBlogPermalink)}
{if !!(blogDetail.nextBlogPermalink)}
{list a as x}
{if defined('newslist')&&newslist.length>0}
{list newslist as x}
{if x_index>7}{break}{/if}
{list a as x}
{var first_option =}
{list x.voteDetailList as voteToOption}
{if voteToOption==1}
{if first_option==false},{/if}&&“${b[voteToOption_index]}”&&
{if (x.role!="-1") },“我是${c[x.role]}”&&{/if}
&&&&&&&&${fn1(x.voteTime)}
{if x.userName==''}{/if}
网易公司版权所有&&
{list x.l as y}
{if defined('wl')}
{list wl as x}{/list}QBrush Class | Qt GUI 5.4
QBrush Class
class defines the fill pattern of shapes drawn by .
Header: #include &QBrush&
qmake: QT += gui
Public Functions
(Qt::BrushStyle style)
(const QColor & color, Qt::BrushStyle style = Qt::SolidPattern)
(Qt::GlobalColor color, Qt::BrushStyle style = Qt::SolidPattern)
(const QColor & color, const QPixmap & pixmap)
(Qt::GlobalColor color, const QPixmap & pixmap)
(const QPixmap & pixmap)
(const QImage & image)
(const QBrush & other)
(const QGradient & gradient)
const QColor & () const
const QGradient * () const
bool () const
const QMatrix & () const
void (const QColor & color)
void (Qt::GlobalColor color)
void (const QMatrix & matrix)
void (Qt::BrushStyle style)
void (const QPixmap & pixmap)
void (const QImage & image)
void (const QTransform & matrix)
Qt::BrushStyle () const
void (QBrush & other)
QPixmap () const
QImage () const
QTransform () const
bool (const QBrush & brush) const
QBrush & (const QBrush & brush)
QBrush & (QBrush && other)
bool (const QBrush & brush) const
Related Non-Members
QDataStream & (QDataStream & stream, const QBrush & brush)
QDataStream & (QDataStream & stream, QBrush & brush)
Detailed Description
class defines the fill pattern of shapes drawn by .
A brush has a style, a color, a gradient and a texture.
The brush () defines the fill pattern using the
enum. The default brush style is
(depending on how you construct a brush). This style tells the painter to not fill shapes. The standard style for filling is . The style can be set when the brush is created using the appropriate constructor, and in addition the () function provides means for altering the style once the brush is constructed.
The brush () defines the color of the fill pattern. The color can either be one of Qt's predefined colors, , or any other custom . The currently set color can be retrieved and altered using the () and () functions, respectively.
The () defines the gradient fill used when the current style is either ,
or . Gradient brushes are created by giving a
as a constructor argument when creating the . Qt provides three different gradients: , , and
- all of which inherit .
gradient(50, 50, 50, 50, 50);
gradient.setColorAt(0, ::fromRgbF(0, 1, 0, 1));
gradient.setColorAt(1, ::fromRgbF(0, 0, 0, 0));
brush(gradient);
The () defines the pixmap used when the current style is . You can create a brush with a texture by providing the pixmap when the brush is created or by using ().
Note that applying () makes () == , regardless of previous style settings. Also, calling () will not make a difference if the style is a gradient. The same is the case if the style is
style unless the current texture is a .
The () function returns true if the brush is fully opaque otherwise false. A brush is considered opaque if:
The alpha component of the () is 255.
Its () does not have an alpha channel and is not a .
The colors in the () all have an alpha component that is 255.
To specify the style and color of lines and outlines, use the 's
combined with
and : painter(this);
painter.setBrush(::cyan);
painter.setPen(::darkCyan);
painter.drawRect(0, 0, 100,100);
painter.setBrush(::NoBrush);
painter.setPen(::darkGreen);
painter.drawRect(40, 40, 100, 100);
Note that, by default,
renders the outline (using the currently set pen) when drawing shapes. Use
to disable this behavior.
For more information about painting in general, see the .
See also , , and .
Member Function Documentation
QBrush::​QBrush()
Constructs a default black brush with the style
(i.e. this brush will not fill shapes).
QBrush::​QBrush( style)
Constructs a black brush with the given style.
See also ().
QBrush::​QBrush(const
style = Qt::SolidPattern)
Constructs a brush with the given color and style.
See also () and ().
QBrush::​QBrush( color,
style = Qt::SolidPattern)
Constructs a brush with the given color and style.
See also () and ().
QBrush::​QBrush(const
& color, const
Constructs a brush with the given color and the custom pattern stored in pixmap.
The style is set to . The color will only have an effect for QBitmaps.
See also () and ().
QBrush::​QBrush( color, const
Constructs a brush with the given color and the custom pattern stored in pixmap.
The style is set to . The color will only have an effect for QBitmaps.
See also () and ().
QBrush::​QBrush(const
Constructs a brush with a black color and a texture set to the given pixmap. The style is set to .
See also ().
QBrush::​QBrush(const
Constructs a brush with a black color and a texture set to the given image. The style is set to .
See also ().
QBrush::​QBrush(const
Constructs a copy of other.
QBrush::​QBrush(const
& gradient)
Constructs a brush based on the given gradient.
The brush style is set to the corresponding gradient style (either ,
QBrush::​~QBrush()
Destroys the brush.
& QBrush::​color() const
Returns the brush color.
See also ().
* QBrush::​gradient() const
Returns the gradient describing this brush.
bool QBrush::​isOpaque() const
Returns true if the brush is fully opaque otherwise false. A brush is considered opaque if:
The alpha component of the () is 255.
Its () does not have an alpha channel and is not a .
The colors in the () all have an alpha component that is 255.
It is an extended radial gradient.
& QBrush::​matrix() const
Returns the current transformation matrix for the brush.
This function was introduced in
See also ().
void QBrush::​setColor(const
Sets the brush color to the given color.
Note that calling setColor() will not make a difference if the style is a gradient. The same is the case if the style is
style unless the current texture is a .
See also ().
void QBrush::​setColor( color)
This is an overloaded function.
Sets the brush color to the given color.
void QBrush::​setMatrix(const
Sets matrix as an explicit transformation matrix on the current brush. The brush transformation matrix is merged with
transformation matrix to produce the final result.
This function was introduced in
See also ().
void QBrush::​setStyle( style)
Sets the brush style to style.
See also ().
void QBrush::​setTexture(const
Sets the brush pixmap to pixmap. The style is set to .
The current brush color will only have an effect for monochrome pixmaps, i.e. for () == 1 ().
See also ().
void QBrush::​setTextureImage(const
Sets the brush image to image. The style is set to .
Note the current brush color will not have any affect on monochrome images, as opposed to calling () with a . If you want to change the color of monochrome image brushes, either convert the image to
with QBitmap::fromImage() and set the resulting
as a texture, or change the entries in the color table for the image.
This function was introduced in
See also () and ().
void QBrush::​setTransform(const
Sets matrix as an explicit transformation matrix on the current brush. The brush transformation matrix is merged with
transformation matrix to produce the final result.
This function was introduced in
See also ().
QBrush::​style() const
Returns the brush style.
See also ().
void QBrush::​swap( & other)
Swaps brush other with this brush. This operation is very fast and never fails.
This function was introduced in
QBrush::​texture() const
Returns the custom brush pattern, or a null pixmap if no custom brush pattern has been set.
See also ().
QBrush::​textureImage() const
Returns the custom brush pattern, or a null image if no custom brush pattern has been set.
If the texture was set as a
it will be converted to a .
This function was introduced in
See also ().
QBrush::​transform() const
Returns the current transformation matrix for the brush.
This function was introduced in
See also ().
QBrush::​operator QVariant() const
Returns the brush as a
bool QBrush::​operator!=(const
& brush) const
Returns true if the brush is different from the given brush; otherwise returns false.
Two brushes are different if they have different styles, colors or transforms or different pixmaps or gradients depending on the style.
See also ().
& QBrush::​operator=(const
Assigns the given brush to this brush and returns a reference to this brush.
& QBrush::​operator=( && other)
Move-assigns other to this
This function was introduced in
bool QBrush::​operator==(const
& brush) const
Returns true if the brush is equal to the given brush; otherwise returns false.
Two brushes are equal if they have equal styles, colors and transforms and equal pixmaps or gradients depending on the style.
See also ().
Related Non-Members
& operator&&( & stream, const
Writes the given brush to the given stream and returns a reference to the stream.
See also .
& operator&&( & stream,
Reads the given brush from the given stream and returns a reference to the stream.
See also .
& 2015 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.扫扫二维码,随身浏览文档
手机或平板扫扫即可继续访问
C++ GUI Qt4 编程(第二版) Chapter 8.ppt
举报该文档为侵权文档。
举报该文档含有违规或不良信息。
反馈该文档无法正常浏览。
举报该文档为重复文档。
推荐理由:
将文档分享至:
分享完整地址
文档地址:
粘贴到BBS或博客
flash地址:
支持嵌入FLASH地址的网站使用
html代码:
&embed src='/DocinViewer-4.swf' width='100%' height='600' type=application/x-shockwave-flash ALLOWFULLSCREEN='true' ALLOWSCRIPTACCESS='always'&&/embed&
450px*300px480px*400px650px*490px
支持嵌入HTML代码的网站使用
您的内容已经提交成功
您所提交的内容需要审核后才能发布,请您等待!
3秒自动关闭窗口2101人阅读
qt中简单的打印过程
1、新建一个qprintviewdialog对象p。
2、connect对象p的 printrequisition 信号到自己的一个函数如doprint(*printer)
3、实现doprint函数,就是在里面使用 qpainter(print)新建一个painter后直接在里面画就好了。
4、p.exec()
这种方式是显示qt提供的打印预览对话框,该对话框需要你提供printrequisition信号的实际实现。当p需要预览或打印时就会发出该信号,你只要实现好doprint函数即可,别的qt都为你做好了。
需要注意的是:
1、在第三步中当调用painter.begin后默认就会创建一个空白页,所以一开始不需要 newpage,直接画就是了;
2、在一页画完以后直接调用print.newpage创建新页面
3、直到所有页面画完后才可以调用 painter.end().
4、qt提供的qprintviewdialog对话框已经包含了所有有关打印设置的功能,如需定制可以在新建p之前调用printdialog对话框来获取打印机,将其传给p的构造函数即可。
5、printer对象的pagerect返回的是去掉页边距后的矩形;paperrect返回的是纸张大小(单位可以设置)
6、这种方式不能适合多种打印机,也就是说你设计好了能工作了可能换到针式打印机就不一样了。因为不同打印机支持的纸张及打印分辨率不同。
解决方法推荐:
建议首先在A4纸张和300或600分辨率下设计,程序中设置一个变量deltaW=程序运行时打印机中的纸张宽度*程序运行时打印机分辨率/(A4纸宽度*300或600),然后在所有表示坐标.x或大小.w的数字上乘以deltaW即可。同样的也可以设置垂直缩放比例。
注意是所有的,包括字体大小、及绘图中的各种坐标。这样不管用什么纸张在什么打印机下都可以完全显示了(打出来会是整体放大或缩小)。如果你能预先知道客户机上的打印环境的话,就不必这一步了。
================================
在Qt中,打印与在 QWidget,QPixmap或者QImage绘图很相似,一般步骤如下:
1、创建绘图设备的QPrinter;
2、弹出打印对话框,QPrintDialog,允许用户选择打印机,设置属性等;
3、创建一个QPrinter的QPainter;
4、用QPainter绘制一页;
5、调用QPrinter::newPage(),然后绘制下一页;
6、重复步骤4,5,直到打印完所有页。
在Windows和Mac OS X平台,QPrinter使用系统的打印驱动程序。在Unix上,QPrinter生成脚本并把脚本发送给lp或者lpr(或者发送给程序,打印程序有函 数QPrinter::setPrintProgram())。调用QPrinter::setOutputFormat (QPrinter::PdfFormat)QPrinter也可以生成PDF文件。
Figure 8.12. Printing a QImage
首先看一个简单的例子,打印一个QImage到一页纸上。
void PrintWindow::printImage(const QImage &image)
&&& QPrintDialog printDialog(&printer, this);
&&& if (printDialog.exec()) {
&&&&&&& QPainter painter(&printer);
&&&&&&& QRect rect = painter.viewport();
&&&&&&& QSize size = image.size();
&&&&&&& size.scale(rect.size(), Qt::KeepAspectRatio);
&&&&&&& painter.setViewport(rect.x(), rect.y(),
&&&&&&&&&&&&&&&&&&&&&&&&&&& size.width(), size.height());
&&&&&&& painter.setWindow(image.rect());
&&&&&&& painter.drawImage(0, 0, image);
这里,我们假设了在PrintWindow类有一个QPrinter类型的成员变量printer。当然在printImage()函数的堆上我们也可以创建一个QPrinter,但是这样不能记录用户进行打印时的设置
创建QPrintDialog,调用exec()显示出来,如果用户点击了OK返回true,否则返回false。调用exec()后,QPrinter 对象就可以使用了。(也可以不显示QPrintDialog,直接调用QPrinter的成员函数进行复制也可以)
然后,我们创建QPainter,绘图设备为QPrinter。设置窗口为所显示图形的矩形,视口也同样比例,然后在(0,0)绘制图像。
通常,QPainter的窗口自动进行了初始化,打印机和屏幕有着一致的分辨率(一英寸有72到100个点),使控件的打印代码能够重用。在上面的函数中,我们自己设置来QPainter的窗口。
在一页中进行打印很简单,但是,很多应用程序需要打印多页。这时我们一次打印一页,然后调用newPage()打印另一页。这里需要解决定一个问题是要确定一页打印多少内容。在Qt中有两种方法处理多页的打印文档:
1、我们可以把数据转换为HTML格式,使用QTextDocument描述他们,QTextDocument是Qt的多文本引擎。
2、手动进行分页
下面我们来分别看一下这两种方法。第一个例子,我们想打印一个花卉的指导:一列为花的名字,另一列为文本描述。每一条的文本格式存储为:“名称:描述”。 例如:Miltonopsis santanae: A most dangerous orchid species.
由于每一种花卉的数据都可以用一个字符串表示,我们可以用QStringList表示所有花卉的数据。下面的代码为使用Qt的多文本引擎进行打印的例子:
void PrintWindow::printFlowerGuide(const QStringList &entries)
&&& foreach (QString entry, entries) {
&&&&&&& QStringList fields = entry.split(&: &);
&&&&&&& QString title = Qt::escape(fields[0]);
&&&&&&& QString body = Qt::escape(fields[1]);
&&&&&&& html += &&table width=/&100%/& border=1 cellspacing=0&/n&
&&&&&&&&&&&&&&& &&tr&&td bgcolor=/&lightgray/&&&font size=/&+1/&&&
&&&&&&&&&&&&&&& &&b&&i&& + title + &&/i&&/b&&/font&/n&tr&&td&& + body
&&&&&&&&&&&&&&& + &/n&/table&/n&br&/n&;
&&& printHtml(html);
首先把QStringList转换为HTML。每一种花卉为HTML表格中的一行,调用Qt::escapte()将特殊字符 ’&’,’&’,’&’等用相应的HTML字符表示(’amp’,’&gt’,’&lt’),然后调用 printHtml()打印文本:
void PrintWindow::printHtml(const QString &html)
&&& QPrintDialog printDialog(&printer, this);
&&& if (printDialog.exec()) {
&&&&&&& QPainter painter(&printer);
&&&&&&& QTextDocument textD
&&&&&&& textDocument.setHtml(html);
&&&&&&& textDocument.print(&printer);
函数printHtml()弹出QPrintDialog对话框,负责打印一个HTML文档。这些代码可以在所有Qt的应用程序中打印任意HTML文档。
Figure 8.13. Printing a flower guide using QTextdocument
目前,把文本转换为HTML文档用QTextDocument打印是最方便的一个方法。如果需要更多的设置,就需要我们自己进行页面布局和绘制。下面的方法就是用人工干预的方式打印花卉指南。首先看一下printFlowerGuide()函数:
void PrintWindow::printFlowerGuide(const QStringList &entries)
&&& QPrintDialog printDialog(&printer, this);
&&& if (printDialog.exec()) {
&&&&&&& QPainter painter(&printer);
&&&&&&& QList&QStringList&
&&&&&&& paginate(&painter, &pages, entries);
&&&&&&& printPages(&painter, pages);
在创建QPainter,设置好打印机以后,调用函数paginate()确定那些项目在那一页。执行这个函数的结果是得到一个QStringList的列表,每一个QStringList在一页里显示,把这个结果传递给printPages()进行打印。
例如:需要打印的花卉指南有6个条目:A,B,C,D,E,F。其中A和B在第一页,C,D,E打印在第二页,F在第三页打印。
void PrintWindow::paginate(QPainter *painter, QList&QStringList& *pages,
&&&&&&&&&&&&&&&&&&&&&&&&&& const QStringList &entries)
&&& QStringList currentP
&&& int pageHeight = painter-&window().height() - 2 * LargeG
&&& int y = 0;
&&& foreach (QString entry, entries) {
&&&&&&& int height = entryHeight(painter, entry);
&&&&&&& if (y + height & pageHeight && !currentPage.empty()) {
&&&&&&&&&&& pages-&append(currentPage);
&&&&&&&&&&& currentPage.clear();
&&&&&&&&&&& y = 0;
&&&&&&& currentPage.append(entry);
&&&&&&& y += height + MediumG
&&& if (!currentPage.empty())
&&&&&&& pages-&append(currentPage);
函数paginate()把花会指南条目分页。根据entryHeight()计算每一个条目的高度。同时考虑页面顶端和底端的垂直距离 LargeGap。
遍历所有的条目,如果这个条目可以放在当前页,就把这个条目放到当前页的列表里面。当前页排满后,把当前页放到页的列表中,开始新的一页。
int PrintWindow::entryHeight(QPainter *painter, const QString &entry)
&&& QStringList fields = entry.split(&: &);
&&& QString title = fields[0];
&&& QString body = fields[1];
&&& int textWidth = painter-&window().width() - 2 * SmallG
&&& int maxHeight = painter-&window().height();
&&& painter-&setFont(titleFont);
&&& QRect titleRect = painter-&boundingRect(0, 0, textWidth, maxHeight,
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& Qt::TextWordWrap, title);
&&& painter-&setFont(bodyFont);
&&& QRect bodyRect = painter-&boundingRect(0, 0, textWidth, maxHeight,
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& Qt::TextWordWrap, body);
&&& return titleRect.height() + bodyRect.height() + 4 * SmallG
函数entryHeight()根据QPainter::boundingRect()计算每一个条目的垂直距离,图8.4表明了条目的布局和 SmallGap还MediumGap的含义:
The enTRyHeight() function uses QPainter::boundingRect() to compute the vertical space needed by one entry. Figure 8.14 shows the layout of a flower entry and the meaning of the SmallGap and MediumGap constants.
Figure 8.14. A flower entry's layout
void PrintWindow::printPages(QPainter *painter,
&&&&&&&&&&&&&&&&&&&&&&&&&&&& const QList&QStringList& &pages)
&&& int firstPage = printer.fromPage() - 1;
&&& if (firstPage &= pages.size())
&&& if (firstPage == -1)
&&&&&&& firstPage = 0;
&&& int lastPage = printer.toPage() - 1;
&&& if (lastPage == -1 || lastPage &= pages.size())
&&&&&&& lastPage = pages.size() - 1;
&&& int numPages = lastPage - firstPage + 1;
&&& for (int i = 0; i & printer.numCopies(); ++i) {
&&&&&&& for (int j = 0; j & numP ++j) {
&&&&&&&&&&& if (i != 0 || j != 0)
&&&&&&&&&&&&&&& printer.newPage();
&&&&&&&&&&&
&&&&&&&&&&& if (printer.pageOrder() == QPrinter::FirstPageFirst) {
&&&&&&&&&&&&&&& index = firstPage +
&&&&&&&&&&& } else {
&&&&&&&&&&&&&&& index = lastPage -
&&&&&&&&&&& }
&&&&&&&&&&& printPage(painter, pages[index], index + 1);
函数printPages()的作用是调用printPage()按照顺序和打印份数打印每一页。通过QPrintDialog,用户可能需要打印多份,设置了打印范围,或者要求逆序打印。我们需要在程序中考虑这些需求
首先确定打印范围。QPrinter::fromPage()和toPage()返回用户选择的页面范围。如果没有选择,返回为0。我们进行了减1操作是 因为我们的页面索引是从0开始的。如果用户没有选定范围,则打印全部,firstPage和lastPage包含量所有的页面。
然后我们打印每一页。最外层循环为用户设定的打印的份数。对于那些支持多份打印的打印机,QPrinter::numCopies()总是返回1。如果打 印机驱动程序不支持多份打印,numCopies()返回到是用户指定的打印份数,有应用程序实现多份打印。(在这一节的QImage例子中,为了简单起 见,我们没有考虑多份打印。)
Figure 8.15. Printing a flower guide using QPainter
内层循环遍历打印的页数。如果页数不是第一页,调用newPage()清楚原来的页面开始填充新页面。调用printPage()打印每一页。
void PrintWindow::printPage(QPainter *painter,
&&&&&&&&&&&&&&&&&&&&&&&&&&& const QStringList &entries, int pageNumber)
&&& painter-&save();
&&& painter-&translate(0, LargeGap);
&&& foreach (QString entry, entries) {
&&&&&&& QStringList fields = entry.split(&: &);
&&&&&&& QString title = fields[0];
&&&&&&& QString body = fields[1];
&&&&&&& printBox(painter, title, titleFont, Qt::lightGray);
&&&&&&& printBox(painter, body, bodyFont, Qt::white);
&&&&&&& painter-&translate(0, MediumGap);
&&& painter-&restore();
&&& painter-&setFont(footerFont);
&&& painter-&drawText(painter-&window(),
&&&&&&&&&&&&&&&&&&&&& Qt::AlignHCenter | Qt::AlignBottom,
&&&&&&&&&&&&&&&&&&&&& QString::number(pageNumber));
函数printPage()打印页面中的每一个条目。首先用printBox()打印标题,然后用printBox()打印描述。在每一页的底端打印页码。
Figure 8.16. The flower guide's page layout
void PrintWindow::printBox(QPainter *painter, const QString &str,
&&&&&&&&&&&&&&&&&&&&&&&&&& const QFont &font, const QBrush &brush)
&&& painter-&setFont(font);
&&& int boxWidth = painter-&window().width();
&&& int textWidth = boxWidth - 2 * SmallG
&&& int maxHeight = painter-&window().height();
&&& QRect textRect = painter-&boundingRect(SmallGap, SmallGap,
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& textWidth, maxHeight,
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& Qt::TextWordWrap, str);
&&& int boxHeight = textRect.height() + 2 * SmallG
&&& painter-&setPen(QPen(Qt::black, 2, Qt::SolidLine));
&&& painter-&setBrush(brush);
&&& painter-&drawRect(0, 0, boxWidth, boxHeight);
&&& painter-&drawText(textRect, Qt::TextWordWrap, str);
&&& painter-&translate(0, boxHeight);
printBox()首先绘制一个矩形框,然后在矩形框中绘制文本。
打印预览:
void Widget::on_pushButton_6_clicked()
QPrinter printer(QPrinter::HighResolution);
QPrintPreviewDialog preview(&printer,this);
connect(&preview,SIGNAL(paintRequested(QPrinter*)),this,SLOT(printView(QPrinter*)));
preview.exec();
void Widget::printView(QPrinter *printer)
ui-&textBrowser-&print(printer);
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:37408次
排名:千里之外
原创:19篇
转载:30篇
(2)(1)(3)(3)(7)(4)(3)(1)(1)(13)(11)

我要回帖

更多关于 jquery lt gt 的文章

 

随机推荐