1.最近在研究太阳神三国杀源码,在三国杀的源码中采用很多函数指针的方式进行数据传递,感觉很实用,
class ConfigDialog : public FlatDialog { Q_OBJECT public: ConfigDialog(QWidget *parent = 0); ~ConfigDialog(); private: Ui::ConfigDialog *ui; typedef void (ConfigDialog::*Callback) (const QVariant &); //采用QVariant类型可以任意转换 QList<Callback> resetCallbacks; //保存函数指针 QVariantList callbackArgs; //保存参数 void doCallback(Callback callback, const QVariant &oldValue, const QVariant &newValue); void setBackground(const QVariant &path); void on_resetBgButton_clicked(); } ConfigDialog::ConfigDialog(QWidget *parent) : FlatDialog(parent, false), ui(new Ui::ConfigDialog) { ui->setupUi(this); } void ConfigDialog::doCallback(ConfigDialog::Callback callback, const QVariant &oldValue, const QVariant &newValue) { if (!resetCallbacks.contains(callback)) { resetCallbacks << callback; callbackArgs << oldValue; } (this->*callback)(newValue);//调用函数指针 } //相应信号 void ConfigDialog::on_resetBgButton_clicked() { QString fileName = "image/backdrop/bg.jpg"; if (fileName != ui->bgPathLineEdit->text()) doCallback(&ConfigDialog::setBackground, Config.BackgroundImage, fileName);//进行函数的调用传递 } void ConfigDialog::setBackground(const QVariant &path) { QString fileName = path.toString(); ui->bgPathLineEdit->setText(fileName); Config.BackgroundImage = fileName; emit bg_changed(); } //如果没有保存数据的话,就感觉函数指针的保存内容进行还原 void ConfigDialog::discardSettings() { const int n = resetCallbacks.size(); for (int i = 0; i < n; ++i) (this->*(resetCallbacks[i]))(callbackArgs.at(i)); resetCallbacks.clear(); callbackArgs.clear(); }