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>/****************************************************************************** $Id: 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 <<a name="qscrollview.h"></a><a href="qscrollview-h.html">qscrollview.h</a>>#include <<a name="qapplication.h"></a><a href="qapplication-h.html">qapplication.h</a>>#include <<a name="qmenubar.h"></a><a href="qmenubar-h.html">qmenubar.h</a>>#include <<a name="qpopupmenu.h"></a><a href="qpopupmenu-h.html">qpopupmenu.h</a>>#include <<a name="qpushbutton.h"></a><a href="qpushbutton-h.html">qpushbutton.h</a>>#include <<a name="qpainter.h"></a><a href="qpainter-h.html">qpainter.h</a>>#include <<a name="qpixmap.h"></a><a href="qpixmap-h.html">qpixmap.h</a>>#include <<a name="qmessagebox.h"></a><a href="qmessagebox-h.html">qmessagebox.h</a>>#include <<a name="qlayout.h"></a><a href="qlayout-h.html">qlayout.h</a>>#include <<a name="qlabel.h"></a><a href="qlabel-h.html">qlabel.h</a>>#include <<a name="qmultilineedit.h"></a><a href="qmultilineedit-h.html">qmultilineedit.h</a>>#include <<a name="qsizegrip.h"></a><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 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<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 > 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))->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 > 50 ) w -= 1; if ( h > 50 ) h -= 2; resize(w,h); } void mouseReleaseEvent(<a name="QMouseEvent"></a><a href="qmouseevent.html">QMouseEvent</a>* e) { emit clicked(e-><a name="x"></a><a href="qmouseevent.html#07ad9e">x</a>(), e-><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-><a href="qmouseevent.html#07ad9e">x</a>(), e-><a href="qmouseevent.html#b63f4c">y</a>(), x, y ); dragging = new <a href="qmultilineedit.html">QMultiLineEdit</a>(viewport(),"Another"); dragging-><a name="setText"></a><a href="qmultilineedit.html#3fd8c1">setText</a>("Thanks!"); dragging-><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-><a href="qmouseevent.html#07ad9e">x</a>(), e-><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-><a href="qmultilineedit.html#3fd8c1">setText</a>(msg); dragging-><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<=bottomrow; r++) { int py=r*rowheight; for (int c=leftcol; c<=rightcol; c++) { int px=c*colwidth; p-><a name="drawPixmap"></a><a href="qpainter.html#c64b89">drawPixmap</a>(px, py, bg); } } } else { p-><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-><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<=bottomrow; r++) { int py=r*rowheight; for (int c=leftcol; c<=rightcol; c++) { int px=c*colwidth; str.<a href="qstring.html#926f67">sprintf</a>("%d,%d",c,r); p-><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<10 && toprow<5) { p-><a name="setFont"></a><a href="qpainter.html#998df2">setFont</a>(<a name="QFont"></a><a href="qfont.html">QFont</a>("Charter",30)); p-><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-><a href="qpainter.html#0f088f">drawText</a>(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-><a name="drawLine"></a><a href="qpainter.html#e3a489">drawLine</a>(229999,100000,200000,129999); // X marks the spot! p-><a href="qpainter.html#998df2">setFont</a>(<a href="qfont.html">QFont</a>("Charter",100)); p-><a href="qpainter.html#0183e4">setPen</a>(blue); p-><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 + -
显示快捷键?