在我的应用程序中我有三个小部件,我在main()
函数中为所有小部件创建了对象,但我不知道如何在其他小部件中调用创建的对象,请指导我,我创建这样的对象:
#include <QtGui/QApplication>
#include "widget.h"
#include "one.h"
#include "two.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget *w = new Widget();
One *one = new One();
Two *two = new Two();
w->show();
return a.exec();
}
创建的对象如何调用其他小部件?
答案 0 :(得分:2)
你不应该打电话给&#39;他们,但通过Qt signal slot mechanism:
连接他们class One : public QObject {... boilerplate omitted
public slots:
void slotWithVoid(){ emit slotWithInt(1); }
signals:
void signalWithInt(int); // filled in by Qt moc
};
// note: give your widgets an owner
auto *w = new QButton(&app);
auto *one = new One(&app);
auto *two = new Two(&app);
connect(w, &QButton::click,
one, &One::slotWithVoid);
connect(one, &One::signalWithInt,
two, &Two::slotWithInt);
现在当发生某些事情时(例如按钮点击),Qt事件系统会注意以正确的顺序,正确的线程,安全等方式调用对象......