我有其他表单需要的 Qt4 from(WorkersList) 来传回一些值,这些表单将调用 WorkersList,然后 WorkersList 将根据进行调用的表单传回值。为了将值从 WorkersList 传回给调用者,我必须为每个表单实现一个方法,如下所示:
伪代码:
class WorkersList : public QDialog
{
Q_OBJECT
public:
explicit WorkersList(QWidget *parent = 0);
void getWorkersList();
void setWorkerForm(Ui::WorkerMod *workerMod); // The method to pass back to the caller 1
void setWorkerForm2(Ui::WorkerDel *workerDel); // The method to pass back to the caller 2
void setSuspendForm(Ui::WorkerSuspend *worker); // The method to pass back to the caller 3
void setResumeForm(Ui::WorkerResume *worker); // The method to pass back to the caller 4
~WorkersList();
private:
Ui::WorkersList *ui;
Ui::WorkerMod *workerForm;
Ui::WorkerDel *workerForm2;
Ui::WorkerSuspend *workerSuspendForm;
Ui::WorkerResume *workerResumeForm;
QStringList infoWorker;
};
有没有一种方法可以让我只需要实现一个方法,将调用者作为参数传递,然后从我向调用者进行强制转换的方法,如下所示:
class WorkersList : public QDialog
{
Q_OBJECT
public:
explicit WorkersList(QWidget *parent = 0);
void getWorkersList();
void setForm(void *from, int caller);
~WorkersList();
private:
Ui::WorkersList *ui;
Ui::WorkerMod *workerForm;
Ui::WorkerDel *workerForm2;
Ui::WorkerSuspend *workerSuspendForm;
Ui::WorkerResume *workerResumeForm;
QStringList infoWorker;
};
...
void WorkersList::setForm(void *form, int caller)
{
if(caller == 0)
workerForm = (Ui::WorkerMod *)form;
else if(caller == 1)
workerForm2 = (Ui::WorkerDel *)form;
else if(caller == 2)
workerSuspendForm = (Ui::WorkerSuspend *)form;
else if(caller == 3)
workerResumeForm = (Ui::WorkerResume *)form;
}
更新
我试过这个:
void WorkersList::setForm(QObject *obj, int form)
{
if(form == 0)
{
this->workerAbsenceForm = qobject_cast<Ui::AbsenceAdd *>(obj);
}
if(form == 1)
{
this->workerAbsenceForm3 = qobject_cast<Ui::AbsenceMod >(obj);
}
}
编译器产生此错误:
/usr/local/include/qt4/QtCore/qobject.h: In function 'T qobject_cast(QObject*) [with T = Ui::AbsenceAdd*]':
workerslist.cpp:259: instantiated from here
/usr/local/include/qt4/QtCore/qobject.h:378: error: 'class Ui::AbsenceAdd' has no member named 'qt_check_for_QOBJECT_macro'
/usr/local/include/qt4/QtCore/qobject.h:380: error: 'class Ui::AbsenceAdd' has no member named 'staticMetaObject'
我试过的另一个提示是:
void WorkersList::setForm(void *obj, int form)
{
if(form == 0)
{
this->workerAbsenceForm = (Ui::AbsenceAdd *)obj;
}
if(form == 1)
{
this->workerAbsenceForm3 = (Ui::AbsenceMod *)obj;
}
}
它确实可以编译,但 GUI 从未出现过。