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;
};
1つのメソッドを実装するだけで済み、呼び出し元を引数として渡し、メソッドから次のように呼び出し元にキャストする方法はありますか?
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 は表示されません。