📄 progress-progress-cpp.html
字号:
file-><a href="qmenudata.html#deddb9">insertItem</a>( "Quit", qApp, SLOT(quit()) ); options = new <a href="qpopupmenu.html">QPopupMenu</a>(); CHECK_PTR( options ); menubar-><a href="qmenudata.html#deddb9">insertItem</a>( "&Options", options ); td_id = options-><a href="qmenudata.html#deddb9">insertItem</a>( "Timer driven", this, SLOT(timerDriven()) ); ld_id = options-><a href="qmenudata.html#deddb9">insertItem</a>( "Loop driven", this, SLOT(loopDriven()) ); options-><a href="qmenudata.html#e34b79">insertSeparator</a>(); dl_id = options-><a href="qmenudata.html#deddb9">insertItem</a>( "Default label", this, SLOT(defaultLabel()) ); cl_id = options-><a href="qmenudata.html#deddb9">insertItem</a>( "Custom label", this, SLOT(customLabel()) ); options-><a href="qmenudata.html#e34b79">insertSeparator</a>(); md_id = options-><a href="qmenudata.html#deddb9">insertItem</a>( "No minimum duration", this, SLOT(toggleMinimumDuration()) ); options-><a name="setCheckable"></a><a href="qpopupmenu.html#672d77">setCheckable</a>( TRUE ); loopDriven(); defaultLabel(); setFixedSize( 400, 300 ); setBackgroundColor( black ); }public slots: void doMenuItem(int id) { if (id >= first_draw_item && id <= last_draw_item) draw(drawItemRects(id)); } void stopDrawing() { got_stop = TRUE; } void timerDriven() { timer_driven = TRUE; options-><a name="setItemChecked"></a><a href="qmenudata.html#319c05">setItemChecked</a>( td_id, TRUE ); options-><a href="qmenudata.html#319c05">setItemChecked</a>( ld_id, FALSE ); } void loopDriven() { timer_driven = FALSE; options-><a href="qmenudata.html#319c05">setItemChecked</a>( ld_id, TRUE ); options-><a href="qmenudata.html#319c05">setItemChecked</a>( td_id, FALSE ); } void defaultLabel() { default_label = TRUE; options-><a href="qmenudata.html#319c05">setItemChecked</a>( dl_id, TRUE ); options-><a href="qmenudata.html#319c05">setItemChecked</a>( cl_id, FALSE ); } void customLabel() { default_label = FALSE; options-><a href="qmenudata.html#319c05">setItemChecked</a>( dl_id, FALSE ); options-><a href="qmenudata.html#319c05">setItemChecked</a>( cl_id, TRUE ); } void toggleMinimumDuration() { options-><a href="qmenudata.html#319c05">setItemChecked</a>( md_id, !options-><a name="isItemChecked"></a><a href="qmenudata.html#def7a0">isItemChecked</a>( md_id ) ); }private: void timerEvent( <a href="qtimerevent.html">QTimerEvent</a>* ) { pb-><a name="setProgress"></a><a href="qprogressdialog.html#037f30">setProgress</a>( pb-><a name="totalSteps"></a><a href="qprogressdialog.html#25744a">totalSteps</a>() - rects ); rects--; { <a href="qpainter.html">QPainter</a> p(this); int ww = width(); int wh = height(); if ( ww > 8 && wh > 8 ) { <a href="qcolor.html">QColor</a> c(rand()%255, rand()%255, rand()%255); int x = rand() % (ww-8); int y = rand() % (wh-8); int w = rand() % (ww-x); int h = rand() % (wh-y); p.<a name="fillRect"></a><a href="qpainter.html#5308a7">fillRect</a>( x, y, w, h, c ); } } if (!rects || got_stop) { pb-><a href="qprogressdialog.html#037f30">setProgress</a>( pb-><a href="qprogressdialog.html#25744a">totalSteps</a>() ); <a href="qpainter.html">QPainter</a> p(this); p.<a href="qpainter.html#5308a7">fillRect</a>(0, 0, width(), height(), backgroundColor()); enableDrawingItems(TRUE); killTimers(); delete pb; pb = 0; } } <a name="QProgressDialog"></a><a href="qprogressdialog.html">QProgressDialog</a>* newProgressDialog( const char* label, int steps, bool modal ) { <a href="qprogressdialog.html">QProgressDialog</a> *d = new <a href="qprogressdialog.html">QProgressDialog</a>(label, "Cancel", steps, this, "progress", modal); if ( options-><a href="qmenudata.html#def7a0">isItemChecked</a>( md_id ) ) d-><a name="setMinimumDuration"></a><a href="qprogressdialog.html#6c1a6c">setMinimumDuration</a>(0); if ( !default_label ) d-><a name="setLabel"></a><a href="qprogressdialog.html#077342">setLabel</a>( new AnimatedThingy(d,label) ); return d; } void enableDrawingItems(bool yes) { for (int i=first_draw_item; i<=last_draw_item; i++) { menubar-><a name="setItemEnabled"></a><a href="qmenudata.html#c9a23c">setItemEnabled</a>(i, yes); } } void draw(int n) { if ( timer_driven ) { if ( pb ) { <a name="qWarning"></a><a href="qapplication.html#290ef4">qWarning</a>("This cannot happen!"); return; } rects = n; pb = newProgressDialog("Drawing rectangles.\n" "Using timer event.", n, FALSE); pb-><a name="setCaption"></a><a href="qwidget.html#d6a291">setCaption</a>("Please Wait"); connect(pb, SIGNAL(cancelled()), this, SLOT(stopDrawing())); enableDrawingItems(FALSE); startTimer(0); got_stop = FALSE; } else { <a href="qprogressdialog.html">QProgressDialog</a>* lpb = newProgressDialog( "Drawing rectangles.\nUsing loop.", n, TRUE); lpb-><a href="qwidget.html#d6a291">setCaption</a>("Please Wait"); <a href="qpainter.html">QPainter</a> p(this); for (int i=0; i<n; i++) { lpb-><a href="qprogressdialog.html#037f30">setProgress</a>(i); if ( lpb-><a name="wasCancelled"></a><a href="qprogressdialog.html#9818b6">wasCancelled</a>() ) break; <a href="qcolor.html">QColor</a> c(rand()%255, rand()%255, rand()%255); int x = rand()%(width()-8); int y = rand()%(height()-8); int w = rand()%(width()-x); int h = rand()%(height()-y); p.<a href="qpainter.html#5308a7">fillRect</a>(x,y,w,h,c); } p.<a href="qpainter.html#5308a7">fillRect</a>(0, 0, width(), height(), backgroundColor()); delete lpb; } } <a href="qmenubar.html">QMenuBar</a>* menubar; <a href="qprogressdialog.html">QProgressDialog</a>* pb; <a href="qpopupmenu.html">QPopupMenu</a>* options; int td_id, ld_id; int dl_id, cl_id; int md_id; int rects; bool timer_driven; bool default_label; bool got_stop;};int main( int argc, char **argv ){ <a name="QApplication"></a><a href="qapplication.html">QApplication</a> a( argc, argv ); int wincount = argc > 1 ? atoi(argv[1]) : 1; for ( int i=0; i<wincount; i++ ) { CPUWaster* cpuw = new CPUWaster; if ( i == 0 ) a.<a name="setMainWidget"></a><a href="qapplication.html#7ad759">setMainWidget</a>(cpuw); cpuw-><a href="qwidget.html#d6a291">setCaption</a>("Qt Example - Progress"); cpuw-><a href="qwidget.html#200ee5">show</a>(); } return a.<a name="exec"></a><a href="qapplication.html#84c7bf">exec</a>();}#include "progress.moc"</pre><p><address><hr><div align="center"><table width="100%" cellspacing="0" border="0"><tr><td>Copyright
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -