scrollview-scrollview-cpp.html

来自「qtopiaphone英文帮助,用于初学者和开发人员,初学者可以用来学习,开发人」· HTML 代码 · 共 480 行 · 第 1/2 页

HTML
480 行
字号
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Qt Toolkit - scrollview/scrollview.cpp example file</title><style type="text/css"><!--h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm; }a:link { color: #004faf; text-decoration: none }a:visited { color: #672967; text-decoration: none }body { background: white; color: black; }--></style></head><body bgcolor="#ffffff"><table width="100%"><tr><td><a href="index.html"><img width="100" height="100" src="qtlogo.png"alt="Home" border="0"><img width="100"height="100" src="face.png" alt="Home" border="0"></a><td valign="top"><div align="right"><img src="dochead.png" width="472" height="27"><br><a href="classes.html"><b>Classes</b></a>- <a href="annotated.html">Annotated</a>- <a href="hierarchy.html">Tree</a>- <a href="functions.html">Functions</a>- <a href="index.html">Home</a>- <a href="topicals.html"><b>Structure</b>  <font face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular" align="center" size=32>Qte</font></a></div></table><h1 align=center>Scrollview</h1><br clear="all">  This example shows how to use Qt's Scrollview. This is a widget  which can contain a very large contents and is very optimized for  that. Also child widgets can be inserted.  <hr>  Implementation:<pre>/****************************************************************************** &#36;Id&#58; qt/examples/scrollview/scrollview.cpp   2.3.8   edited 2004-05-12 $**** Copyright (C) 1992-2000 Trolltech AS.  All rights reserved.**** This file is part of an example program for Qt.  This example** program may be used, distributed and modified without limitation.*******************************************************************************/#include &lt;<a name="qscrollview.h"></a><a href="qscrollview-h.html">qscrollview.h</a>&gt;#include &lt;<a name="qapplication.h"></a><a href="qapplication-h.html">qapplication.h</a>&gt;#include &lt;<a name="qmenubar.h"></a><a href="qmenubar-h.html">qmenubar.h</a>&gt;#include &lt;<a name="qpopupmenu.h"></a><a href="qpopupmenu-h.html">qpopupmenu.h</a>&gt;#include &lt;<a name="qpushbutton.h"></a><a href="qpushbutton-h.html">qpushbutton.h</a>&gt;#include &lt;<a name="qpainter.h"></a><a href="qpainter-h.html">qpainter.h</a>&gt;#include &lt;<a name="qpixmap.h"></a><a href="qpixmap-h.html">qpixmap.h</a>&gt;#include &lt;<a name="qmessagebox.h"></a><a href="qmessagebox-h.html">qmessagebox.h</a>&gt;#include &lt;<a name="qlayout.h"></a><a href="qlayout-h.html">qlayout.h</a>&gt;#include &lt;<a name="qlabel.h"></a><a href="qlabel-h.html">qlabel.h</a>&gt;#include &lt;<a name="qmultilineedit.h"></a><a href="qmultilineedit-h.html">qmultilineedit.h</a>&gt;#include &lt;<a name="qsizegrip.h"></a><a href="qsizegrip-h.html">qsizegrip.h</a>&gt;#include &lt;stdlib.h&gt;static const int style_id       = 0x1000;static const int lw_id          = 0x2000;static const int mlw_id         = 0x4000;static const int mw_id          = 0x8000;static const int max_lw         = 16;static const int max_mlw        = 5;static const int max_mw         = 10;class BigShrinker : public QFrame {    Q_OBJECTpublic:    BigShrinker(<a name="QWidget"></a><a href="qwidget.html">QWidget</a>* parent) :        <a name="QFrame"></a><a href="qframe.html">QFrame</a>(parent)    {        setFrameStyle(QFrame::Box|QFrame::Sunken);        int h=35;        int b=0;        for (int y=0; y&lt;2000-h; y+=h+10) {            if (y == 0) {                <a name="QButton"></a><a href="qbutton.html">QButton</a>* q=new <a name="QPushButton"></a><a href="qpushbutton.html">QPushButton</a>("Quit", this);                connect(q, SIGNAL(clicked()), qApp, SLOT(quit()));            } else {                <a name="QString"></a><a href="qstring.html">QString</a> str;                if ( b &gt; 0 ) {                    str.<a name="sprintf"></a><a href="qstring.html#926f67">sprintf</a>("Button %d", b++);                } else {                    str = "I'm shrinking!";                    ++b;                }                (new QPushButton(str, this))-&gt;move(y/2,y);            }        }        resize(1000,2000);        startTimer(250);    }    void timerEvent(<a name="QTimerEvent"></a><a href="qtimerevent.html">QTimerEvent</a>*)    {        int w=width();        int h=height();        if ( w &gt; 50 ) w -= 1;        if ( h &gt; 50 ) h -= 2;        resize(w,h);    }    void mouseReleaseEvent(<a name="QMouseEvent"></a><a href="qmouseevent.html">QMouseEvent</a>* e)    {        emit clicked(e-&gt;<a name="x"></a><a href="qmouseevent.html#07ad9e">x</a>(), e-&gt;<a name="y"></a><a href="qmouseevent.html#b63f4c">y</a>());    }signals:    void clicked(int,int);};class BigMatrix : public QScrollView {    <a name="QMultiLineEdit"></a><a href="qmultilineedit.html">QMultiLineEdit</a> *dragging;public:    BigMatrix(<a href="qwidget.html">QWidget</a>* parent) :        <a name="QScrollView"></a><a href="qscrollview.html">QScrollView</a>(parent,"matrix", WNorthWestGravity),        bg("bg.ppm")    {        bg.<a name="load"></a><a href="qpixmap.html#a728cf">load</a>("bg.ppm");        resizeContents(400000,300000);        dragging = 0;    }    void viewportMousePressEvent(<a href="qmouseevent.html">QMouseEvent</a>* e)    {        int x, y;        viewportToContents( e-&gt;<a href="qmouseevent.html#07ad9e">x</a>(),  e-&gt;<a href="qmouseevent.html#b63f4c">y</a>(), x, y );        dragging = new <a href="qmultilineedit.html">QMultiLineEdit</a>(viewport(),"Another");        dragging-&gt;<a name="setText"></a><a href="qmultilineedit.html#3fd8c1">setText</a>("Thanks!");        dragging-&gt;<a name="resize"></a><a href="qwidget.html#ff9d07">resize</a>(100,100);        addChild(dragging, x, y);        showChild(dragging);    }    void viewportMouseReleaseEvent(<a href="qmouseevent.html">QMouseEvent</a>*)    {        dragging = 0;    }    void viewportMouseMoveEvent(<a href="qmouseevent.html">QMouseEvent</a>* e)    {        if ( dragging ) {            int mx, my;            viewportToContents( e-&gt;<a href="qmouseevent.html#07ad9e">x</a>(),  e-&gt;<a href="qmouseevent.html#b63f4c">y</a>(), mx, my );            int cx = childX(dragging);            int cy = childY(dragging);            int w = mx - cx + 1;            int h = my - cy + 1;            <a href="qstring.html">QString</a> msg;            msg.<a href="qstring.html#926f67">sprintf</a>("at (%d,%d) %d by %d",cx,cy,w,h);            dragging-&gt;<a href="qmultilineedit.html#3fd8c1">setText</a>(msg);            dragging-&gt;<a href="qwidget.html#ff9d07">resize</a>(w,h);        }    }protected:    void drawContents(<a name="QPainter"></a><a href="qpainter.html">QPainter</a>* p, int cx, int cy, int cw, int ch)    {        // The Background        if ( !bg.<a name="isNull"></a><a href="qpixmap.html#7803c4">isNull</a>() ) {            int rowheight=bg.<a name="height"></a><a href="qpixmap.html#d5bb33">height</a>();            int toprow=cy/rowheight;            int bottomrow=(cy+ch+rowheight-1)/rowheight;            int colwidth=bg.<a name="width"></a><a href="qpixmap.html#86dbc5">width</a>();            int leftcol=cx/colwidth;            int rightcol=(cx+cw+colwidth-1)/colwidth;            for (int r=toprow; r&lt;=bottomrow; r++) {                int py=r*rowheight;                for (int c=leftcol; c&lt;=rightcol; c++) {                    int px=c*colwidth;                    p-&gt;<a name="drawPixmap"></a><a href="qpainter.html#c64b89">drawPixmap</a>(px, py, bg);                }            }        } else {            p-&gt;<a name="fillRect"></a><a href="qpainter.html#5308a7">fillRect</a>(cx, cy, cw, ch, QColor(240,222,208));        }        // The Numbers        {            <a name="QFontMetrics"></a><a href="qfontmetrics.html">QFontMetrics</a> fm=p-&gt;<a name="fontMetrics"></a><a href="qpainter.html#73b2e5">fontMetrics</a>();            int rowheight=fm.<a name="lineSpacing"></a><a href="qfontmetrics.html#e6e380">lineSpacing</a>();            int toprow=cy/rowheight;            int bottomrow=(cy+ch+rowheight-1)/rowheight;            int colwidth=fm.<a name="width"></a><a href="qfontmetrics.html#3b6f39">width</a>("00000,000000 ")+3;            int leftcol=cx/colwidth;            int rightcol=(cx+cw+colwidth-1)/colwidth;            <a href="qstring.html">QString</a> str;            for (int r=toprow; r&lt;=bottomrow; r++) {                int py=r*rowheight;                for (int c=leftcol; c&lt;=rightcol; c++) {                    int px=c*colwidth;                    str.<a href="qstring.html#926f67">sprintf</a>("%d,%d",c,r);                    p-&gt;<a name="drawText"></a><a href="qpainter.html#0f088f">drawText</a>(px+3, py+fm.<a name="ascent"></a><a href="qfontmetrics.html#72c9fb">ascent</a>(), str);                }            }            // The Big Hint            if (leftcol&lt;10 &amp;&amp; toprow&lt;5) {                p-&gt;<a name="setFont"></a><a href="qpainter.html#998df2">setFont</a>(<a name="QFont"></a><a href="qfont.html">QFont</a>("Charter",30));                p-&gt;<a name="setPen"></a><a href="qpainter.html#0183e4">setPen</a>(red);                <a href="qstring.html">QString</a> text;                text.<a href="qstring.html#926f67">sprintf</a>("HINT:  Look at %d,%d",215000/colwidth,115000/rowheight);                p-&gt;<a href="qpainter.html#0f088f">drawText</a>(100,50,text);            }        }        // The Big X        {            if (cx+cw&gt;200000 &amp;&amp; cy+ch&gt;100000 &amp;&amp; cx&lt;230000 &amp;&amp; cy&lt;130000) {                // Note that some X server cannot even handle co-ordinates                // beyond about 4000, so you might not see this.                p-&gt;drawLine(200000,100000,229999,129999);                p-&gt;<a name="drawLine"></a><a href="qpainter.html#e3a489">drawLine</a>(229999,100000,200000,129999);                // X marks the spot!                p-&gt;<a href="qpainter.html#998df2">setFont</a>(<a href="qfont.html">QFont</a>("Charter",100));                p-&gt;<a href="qpainter.html#0183e4">setPen</a>(blue);                p-&gt;<a href="qpainter.html#0f088f">drawText</a>(215000-500,115000-100,1000,200,AlignCenter,"YOU WIN!!!!!");            }        }    }private:    <a name="QPixmap"></a><a href="qpixmap.html">QPixmap</a> bg;};class ScrollViewExample : public QWidget {    Q_OBJECTpublic:    ScrollViewExample(int technique, QWidget* parent=0, const char* name=0) :        <a href="qwidget.html">QWidget</a>(parent,name)    {        <a name="QMenuBar"></a><a href="qmenubar.html">QMenuBar</a>* menubar = new <a href="qmenubar.html">QMenuBar</a>(this);        CHECK_PTR( menubar );

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?