qmag-example.html
来自「QT 下载资料仅供参考」· HTML 代码 · 共 447 行 · 第 1/2 页
HTML
447 行
{ r = index; <a href="qobject.html#killTimers">killTimers</a>(); if (index && !grabbing) <a href="qobject.html#startTimer">startTimer</a>( timer[r] );}void <a name="f471"></a>MagWidget::save(){<a name="x1638"></a> if ( !p.<a href="qpixmap.html#isNull">isNull</a>() ) { <a href="qobject.html#killTimers">killTimers</a>();<a name="x1628"></a> <a href="qstring.html">QString</a> fn = QFileDialog::<a href="qfiledialog.html#getSaveFileName">getSaveFileName</a>();<a name="x1645"></a> if ( !fn.<a href="qstring.html#isEmpty">isEmpty</a>() )<a name="x1639"></a> p.<a href="qpixmap.html#save">save</a>( fn, "BMP" ); if ( r ) <a href="qobject.html#startTimer">startTimer</a>( timer[r] ); }}void <a name="f472"></a>MagWidget::multiSave(){ if ( !p.<a href="qpixmap.html#isNull">isNull</a>() ) { multifn = ""; // stops saving multifn = QFileDialog::<a href="qfiledialog.html#getSaveFileName">getSaveFileName</a>(); if ( multifn.<a href="qstring.html#isEmpty">isEmpty</a>() )<a name="x1642"></a> multiSaveButton-><a href="qpushbutton.html#setOn">setOn</a>(FALSE); if ( !r ) p.<a href="qpixmap.html#save">save</a>( multifn, "BMP" ); } else { multiSaveButton-><a href="qpushbutton.html#setOn">setOn</a>(FALSE); }}void <a name="f473"></a>MagWidget::grab(){ if ( !isVisible() ) return; // don't eat resources when iconified if ( grabx < 0 || graby < 0 ) return; // don't grab until the user has said to int x,y, w,h; w = (<a href="qwidget.html#width">width</a>()+z-1)/z; h = (<a href="qwidget.html#height">height</a>()+z-1-yoffset)/z; if ( w<1 || h<1 ) return; // don't ask too much from the window system :) x = grabx-w/2; // find a suitable position to grab from y = graby-h/2; if ( x + w > QApplication::<a href="qapplication.html#desktop">desktop</a>()->width() ) x = QApplication::<a href="qapplication.html#desktop">desktop</a>()->width()-w; else if ( x < 0 ) x = 0; if ( y + h > QApplication::<a href="qapplication.html#desktop">desktop</a>()->height() ) y = QApplication::<a href="qapplication.html#desktop">desktop</a>()->height()-h; else if ( y < 0 ) y = 0;<a name="x1637"></a> p = QPixmap::<a href="qpixmap.html#grabWindow">grabWindow</a>( QApplication::<a href="qapplication.html#desktop">desktop</a>()->winId(), x, y, w, h );<a name="x1636"></a> image = p.<a href="qpixmap.html#convertToImage">convertToImage</a>(); <a href="qwmatrix.html">QWMatrix</a> m; // after getting it, scale it<a name="x1662"></a> m.<a href="qwmatrix.html#scale">scale</a>( (double)z, (double)z );<a name="x1640"></a> pm = p.<a href="qpixmap.html#xForm">xForm</a>( m );<a name="x1623"></a> if ( !multiSaveButton || !multiSaveButton-><a href="qbutton.html#isOn">isOn</a>() ) <a href="qwidget.html#repaint">repaint</a>( FALSE ); // and finally repaint, flicker-free}<a name="x1656"></a>void MagWidget::<a href="qwidget.html#paintEvent">paintEvent</a>( <a href="qpaintevent.html">QPaintEvent</a> * ){ if ( !pm.<a href="qpixmap.html#isNull">isNull</a>() ) { <a href="qpainter.html">QPainter</a> paint( this ); paint.<a href="qpainter.html#drawPixmap">drawPixmap</a>( 0, zoom ? zoom-><a href="qwidget.html#height">height</a>()+4 : 0, pm, 0,0, width(), height()-yoffset ); }}<a name="x1654"></a>void MagWidget::<a href="qwidget.html#mousePressEvent">mousePressEvent</a>( <a href="qmouseevent.html">QMouseEvent</a> *e ){ if ( !grabbing ) { // prepare to grab... grabbing = TRUE; <a href="qobject.html#killTimers">killTimers</a>(); <a href="qwidget.html#grabMouse">grabMouse</a>( crossCursor ); grabx = -1; graby = -1; } else { // REALLY prepare to grab<a name="x1633"></a> grabx = <a href="qwidget.html#mapToGlobal">mapToGlobal</a>(e-><a href="qmouseevent.html#pos">pos</a>()).x(); graby = <a href="qwidget.html#mapToGlobal">mapToGlobal</a>(e-><a href="qmouseevent.html#pos">pos</a>()).y(); }}<a name="x1655"></a>void MagWidget::<a href="qwidget.html#mouseReleaseEvent">mouseReleaseEvent</a>( <a href="qmouseevent.html">QMouseEvent</a> * e ){ if ( grabbing && grabx >= 0 && graby >= 0 ) { grabbing = FALSE; grabAround(e-><a href="qmouseevent.html#pos">pos</a>()); <a href="qwidget.html#releaseMouse">releaseMouse</a>(); }}void <a name="f474"></a>MagWidget::grabAround(QPoint pos){ int rx, ry; rx = <a href="qwidget.html#mapToGlobal">mapToGlobal</a>(pos).x(); ry = <a href="qwidget.html#mapToGlobal">mapToGlobal</a>(pos).y(); int w = QABS(rx-grabx); int h = QABS(ry-graby); if ( w > 10 && h > 10 ) { int pz; pz = 1; while ( w*pz*h*pz < width()*(<a href="qwidget.html#height">height</a>()-yoffset) && w*pz < QApplication::<a href="qapplication.html#desktop">desktop</a>()->width() && h*pz < QApplication::<a href="qapplication.html#desktop">desktop</a>()->height() ) pz++; if ( (w*pz*h*pz - width()*(<a href="qwidget.html#height">height</a>()-yoffset)) > (<a href="qwidget.html#width">width</a>()*(<a href="qwidget.html#height">height</a>()-yoffset) - w*(pz-1)*h*(pz-1)) ) pz--; if ( pz < 1 ) pz = 1; if ( pz > 8 ) pz = 8; if ( zoom )<a name="x1627"></a> zoom-><a href="qcombobox.html#setCurrentItem">setCurrentItem</a>( pz-1 ); z = pz; grabx = QMIN(rx, grabx) + w/2; graby = QMIN(ry, graby) + h/2; <a href="qwidget.html#resize">resize</a>( w*z, h*z+yoffset ); } grab(); if ( r ) <a href="qobject.html#startTimer">startTimer</a>( timer[r] );}<a name="x1653"></a>void MagWidget::<a href="qwidget.html#mouseMoveEvent">mouseMoveEvent</a>( <a href="qmouseevent.html">QMouseEvent</a> *e ){ if ( grabbing || pm.<a href="qpixmap.html#isNull">isNull</a>() || e-><a href="qmouseevent.html#pos">pos</a>().y() > height() - (zoom ? zoom-><a href="qwidget.html#fontMetrics">fontMetrics</a>().height() - 4 : 0) || e-><a href="qmouseevent.html#pos">pos</a>().y() < (zoom ? zoom-><a href="qwidget.html#height">height</a>()+4 : 4) ) { rgb-><a href="qlabel.html#setText">setText</a>( "" ); } else { int x,y; x = e-><a href="qmouseevent.html#pos">pos</a>().x() / z; y = (e-><a href="qmouseevent.html#pos">pos</a>().y() - ( zoom ? zoom-><a href="qwidget.html#height">height</a>() : 0 ) - 4) / z; <a href="qstring.html">QString</a> pixelinfo;<a name="x1630"></a> if ( image.<a href="qimage.html#valid">valid</a>(x,y) ) {<a name="x1629"></a> QRgb px = image.<a href="qimage.html#pixel">pixel</a>(x,y);<a name="x1648"></a> pixelinfo.<a href="qstring.html#sprintf">sprintf</a>(" %3d,%3d,%3d #%02x%02x%02x", <a href="qcolor.html#qRed">qRed</a>(px), qGreen(px), qBlue(px), <a href="qcolor.html#qRed">qRed</a>(px), qGreen(px), qBlue(px)); } <a href="qstring.html">QString</a> label; label.<a href="qstring.html#sprintf">sprintf</a>( "x=%d, y=%d %s", x+grabx, y+graby, (const char*)pixelinfo ); rgb-><a href="qlabel.html#setText">setText</a>( label ); }}<a name="x1649"></a>void MagWidget::<a href="qwidget.html#focusOutEvent">focusOutEvent</a>( <a href="qfocusevent.html">QFocusEvent</a> * ){ rgb-><a href="qlabel.html#setText">setText</a>( "" );}<a name="x1634"></a>void MagWidget::<a href="qobject.html#timerEvent">timerEvent</a>( <a href="qtimerevent.html">QTimerEvent</a> * ){ grab();/* if ( multiSaveButton-><a href="qbutton.html#isOn">isOn</a>() && !multifn.<a href="qstring.html#isEmpty">isEmpty</a>() ) { <a href="qregexp.html">QRegExp</a> num("[0-9][0-9]*"); int start; int len;<a name="x1644"></a> if ((start=num.<a href="qregexp.html#match">match</a>(multifn,0,&len))>=0)<a name="x1647"></a> multifn.<a href="qstring.html#replace">replace</a>(num,<a name="x1646"></a> QString().setNum(multifn.<a href="qstring.html#mid">mid</a>(start,len).toInt()+1) ); p.<a href="qpixmap.html#save">save</a>( multifn, "BMP" ); }*/}<a name="x1659"></a>void MagWidget::<a href="qwidget.html#resizeEvent">resizeEvent</a>( <a href="qresizeevent.html">QResizeEvent</a> * ){ rgb-><a href="qwidget.html#setGeometry">setGeometry</a>( 0, height() - rgb-><a href="qwidget.html#height">height</a>(), width(), rgb-><a href="qwidget.html#height">height</a>() ); grab();}#include "qmag.moc"int main( int argc, char **argv ){ <a href="qapplication.html">QApplication</a> a( argc, argv ); MagWidget m; a.<a href="qapplication.html#setMainWidget">setMainWidget</a>( &m ); m.<a href="qwidget.html#show">show</a>(); return a.<a href="qapplication.html#exec">exec</a>();}</pre><p>See also <a href="examples.html">Examples</a>.<!-- eof --><p><address><hr><div align=center><table width=100% cellspacing=0 border=0><tr><td>Copyright © 2002 <a href="http://www.trolltech.com">Trolltech</a><td><a href="http://www.trolltech.com/trademarks.html">Trademarks</a><td align=right><div align=right>Qt version 3.0.5</div></table></div></address></body></html>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?