scrollview-example.html
来自「QT 下载资料仅供参考」· HTML 代码 · 共 494 行 · 第 1/2 页
HTML
494 行
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><!-- /home/reggie/tmp/qt-3.0-reggie-5401/qt-x11-commercial-3.0.5/examples/scrollview/scrollview.doc:4 --><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Scrollview</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: #ffffff; color: black; }--></style></head><body><table border="0" cellpadding="0" cellspacing="0" width="100%"><tr bgcolor="#E5E5E5"><td valign=center> <a href="index.html"><font color="#004faf">Home</font></a> | <a href="classes.html"><font color="#004faf">All Classes</font></a> | <a href="mainclasses.html"><font color="#004faf">Main Classes</font></a> | <a href="annotated.html"><font color="#004faf">Annotated</font></a> | <a href="groups.html"><font color="#004faf">Grouped Classes</font></a> | <a href="functions.html"><font color="#004faf">Functions</font></a></td><td align="right" valign="center"><img src="logo32.png" align="right" width="64" height="32" border="0"></td></tr></table><h1 align=center>Scrollview</h1> <p> This example shows how to use Qt's scrollview. This is a widgetoptimized for very large contents. <p> <hr><p> Implementation:<p> <pre>/****************************************************************************** $Id: qt/scrollview.cpp 3.0.5 edited Oct 15 2001 $**** 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 <<a href="qscrollview-h.html">qscrollview.h</a>>#include <<a href="qapplication-h.html">qapplication.h</a>>#include <<a href="qmenubar-h.html">qmenubar.h</a>>#include <<a href="qpopupmenu-h.html">qpopupmenu.h</a>>#include <<a href="qpushbutton-h.html">qpushbutton.h</a>>#include <<a href="qpainter-h.html">qpainter.h</a>>#include <<a href="qpixmap-h.html">qpixmap.h</a>>#include <<a href="qmessagebox-h.html">qmessagebox.h</a>>#include <<a href="qlayout-h.html">qlayout.h</a>>#include <<a href="qlabel-h.html">qlabel.h</a>>#include <<a href="qmultilineedit-h.html">qmultilineedit.h</a>>#include <<a href="qsizegrip-h.html">qsizegrip.h</a>>#include <stdlib.h>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 <a href="qframe.html">QFrame</a> { <a href="metaobjects.html#Q_OBJECT">Q_OBJECT</a>public: BigShrinker(QWidget* parent) : <a href="qframe.html">QFrame</a>(parent) { setFrameStyle(QFrame::Box|QFrame::Sunken); int h=35; int b=0; for (int y=0; y<2000-h; y+=h+10) { if (y == 0) { <a href="qbutton.html">QButton</a>* q=new <a href="qpushbutton.html">QPushButton</a>("Quit", this); connect(q, SIGNAL(<a href="qbutton.html#clicked">clicked</a>()), qApp, SLOT(<a href="qapplication.html#quit">quit</a>())); } else { <a href="qstring.html">QString</a> str; if ( b > 0 ) {<a name="x743"></a> str.<a href="qstring.html#sprintf">sprintf</a>("Button %d", b++); } else { str = "I'm shrinking!"; ++b; } (new <a href="qpushbutton.html">QPushButton</a>(str, this))->move(y/2,y); } } resize(1000,2000); startTimer(250); } void timerEvent(QTimerEvent*) { int w=width(); int h=height(); if ( w > 50 ) w -= 1; if ( h > 50 ) h -= 2; resize(w,h); } void mouseReleaseEvent(QMouseEvent* e) { emit clicked(e->x(), e->y()); }signals: void clicked(int,int);};class BigMatrix : public <a href="qscrollview.html">QScrollView</a> { <a href="qmultilineedit.html">QMultiLineEdit</a> *dragging;public: BigMatrix(QWidget* parent) : <a href="qscrollview.html">QScrollView</a>(parent,"matrix", WStaticContents), bg("bg.ppm") {<a name="x727"></a> bg.<a href="qpixmap.html#load">load</a>("bg.ppm"); resizeContents(400000,300000); dragging = 0; } void viewportMousePressEvent(QMouseEvent* e) { int x, y; viewportToContents( e->x(), e->y(), x, y ); dragging = new <a href="qmultilineedit.html">QMultiLineEdit</a>(viewport(),"Another"); dragging-><a href="qtextedit.html#setText">setText</a>("Thanks!");<a name="x737"></a> dragging-><a href="qwidget.html#resize">resize</a>(100,100); addChild(dragging, x, y); showChild(dragging); } void viewportMouseReleaseEvent(QMouseEvent*) { dragging = 0; } void viewportMouseMoveEvent(QMouseEvent* e) { if ( dragging ) { int mx, my; viewportToContents( e->x(), e->y(), 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#sprintf">sprintf</a>("at (%d,%d) %d by %d",cx,cy,w,h); dragging-><a href="qtextedit.html#setText">setText</a>(msg); dragging-><a href="qwidget.html#resize">resize</a>(w,h); } }protected: void drawContents(QPainter* p, int cx, int cy, int cw, int ch) { // The Background<a name="x726"></a> if ( !bg.<a href="qpixmap.html#isNull">isNull</a>() ) {<a name="x725"></a> int rowheight=bg.<a href="qpixmap.html#height">height</a>(); int toprow=cy/rowheight; int bottomrow=(cy+ch+rowheight-1)/rowheight;<a name="x728"></a> int colwidth=bg.<a href="qpixmap.html#width">width</a>(); int leftcol=cx/colwidth; int rightcol=(cx+cw+colwidth-1)/colwidth; for (int r=toprow; r<=bottomrow; r++) { int py=r*rowheight; for (int c=leftcol; c<=rightcol; c++) { int px=c*colwidth; p->drawPixmap(px, py, bg); } } } else { p->fillRect(cx, cy, cw, ch, QColor(240,222,208)); } // The Numbers { <a href="qfontmetrics.html">QFontMetrics</a> fm=p->fontMetrics();<a name="x707"></a> int rowheight=fm.<a href="qfontmetrics.html#lineSpacing">lineSpacing</a>(); int toprow=cy/rowheight; int bottomrow=(cy+ch+rowheight-1)/rowheight;<a name="x708"></a> int colwidth=fm.<a href="qfontmetrics.html#width">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<=bottomrow; r++) { int py=r*rowheight; for (int c=leftcol; c<=rightcol; c++) { int px=c*colwidth; str.<a href="qstring.html#sprintf">sprintf</a>("%d,%d",c,r);<a name="x706"></a> p->drawText(px+3, py+fm.<a href="qfontmetrics.html#ascent">ascent</a>(), str); } } // The Big Hint if (leftcol<10 && toprow<5) { p->setFont(QFont("Charter",30)); p->setPen(red); <a href="qstring.html">QString</a> text; text.<a href="qstring.html#sprintf">sprintf</a>("HINT: Look at %d,%d",215000/colwidth,115000/rowheight); p->drawText(100,50,text); } } // The Big X { if (cx+cw>200000 && cy+ch>100000 && cx<230000 && cy<130000) { // Note that some X server cannot even handle co-ordinates // beyond about 4000, so you might not see this. p->drawLine(200000,100000,229999,129999); p->drawLine(229999,100000,200000,129999); // X marks the spot! p->setFont(QFont("Charter",100)); p->setPen(blue); p->drawText(215000-500,115000-100,1000,200,AlignCenter,"YOU WIN!!!!!"); } } }private: <a href="qpixmap.html">QPixmap</a> bg;};class ScrollViewExample : public <a href="qwidget.html">QWidget</a> { Q_OBJECTpublic: ScrollViewExample(int technique, QWidget* parent=0, const char* name=0) : <a href="qwidget.html">QWidget</a>(parent,name) { <a href="qmenubar.html">QMenuBar</a>* menubar = new <a href="qmenubar.html">QMenuBar</a>(this); <a href="qapplication.html#Q_CHECK_PTR">Q_CHECK_PTR</a>( menubar ); <a href="qpopupmenu.html">QPopupMenu</a>* file = new <a href="qpopupmenu.html">QPopupMenu</a>( menubar );
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?