我使用Qt来创建一个窗口,我有以下代码(这多少有些伪代码):指向的QMainWindow更改
class MyInterface {
virtual void doupdate() = 0;
}
class InterfaceHandler {
InterfaceHandler(MyInterface *i) {
the_int = i;
start_thread(&mainloop);
}
void mainloop() {
while(1) the_int->doupdate();
}
MyInterface *the_int;
}
class console : public QMainWindow, public MyInterface {
console() {
InterfaceHandler x(this);
}
void doupdate() {
//code to modify the gui
}
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
console w(argc, argv);
w.show();
return a.exec();
}
我的问题是,当the_int->doupdate()
被称为mainloop()
中,参考the_int
是错误的。我认为这与console
继承QMainWindow
的事实有关,但我不确定解决方案是什么。
MyInterface
并不总是被QObject
继承。我试图将doupdate()
从console
拆分成另一个类,它在构造函数中通过对console
的引用,但获得相同的结果。
任何想法?
'the_int'应该被称为'the_iface'或其他东西,因为在一个C++问题中,我们通常假设'the_int'是一个整数。但无论如何......这是否只发生在与线程交互(例如可以在InterfaceHandler构造函数中调用'doupdate()',但以后变坏?) – HostileFork
如何分配控制台对象?这不是在堆栈上吗? –
'the_int'就在这个例子中。是的,这似乎是线程问题。当我在构造函数中调用它时,我没有任何问题。我在创建它之后(在构造函数中)打印出指针的位置,然后在我调用'mainloop()'时再次打印指针的位置,但它们不同。上面已经添加了我的'console'分配。 – zsalzbank