1. Recently in the study of the sun god three countries kill the source code, in the three countries killed in the source code to use a lot of function pointers in the way of data transfer, feel very practical,
classConfigdialog: PublicFlatdialog {q_object Public: Configdialog (Qwidget*parent =0); ~Configdialog ();Private: Ui::configdialog*UI; typedefvoid(Configdialog::* Callback) (ConstQvariant &);//can be arbitrarily converted using the Qvariant typeQlist<callback> resetcallbacks;//Saving function PointersQvariantlist Callbackargs;//Save Parameters voidDoCallback (Callback Callback,ConstQvariant &oldvalue,ConstQvariant &newvalue); voidSetBackground (ConstQvariant &path); voidon_resetbgbutton_clicked ();} Configdialog::configdialog (Qwidget*parent): Flatdialog (Parent,false), UI (NewUi::configdialog) {UI->SETUPUI ( This);}voidConfigdialog::d ocallback (Configdialog::callback Callback,ConstQvariant &oldvalue,ConstQvariant &newvalue) { if(!Resetcallbacks.contains (callback)) {Resetcallbacks<<callback; Callbackargs<<OldValue; } ( This->*callback) (NewValue);//Calling function pointers}//corresponding Signalvoidconfigdialog::on_resetbgbutton_clicked () {QString fileName="image/backdrop/bg.jpg"; if(FileName! = ui->bgpathlineedit->text ()) DoCallback (&Configdialog::setbackground, Config.backgroundimage, fileName);//function call passing}voidConfigdialog::setbackground (ConstQvariant &path) {QString FileName=path.tostring (); UI->bgpathlineedit->SetText (fileName); Config.backgroundimage=FileName; Emit bg_changed ();}//If you do not save the data, you will feel the function pointer save content to restorevoidConfigdialog::d iscardsettings () {Const intn =resetcallbacks.size (); for(inti =0; I < n; ++i) ( This->*(Resetcallbacks[i])) (callbackargs.at (i)); Resetcallbacks.clear (); Callbackargs.clear ();}
callback function use (c)