作者:核能裸麦_536 | 来源:互联网 | 2023-05-19 04:30
EverywhereonlyjustbeforeQPaintDevicequestionsandnowhereismyerror.So,herewego.到处都是“在
Everywhere only just "before QPaintDevice" questions and nowhere is my error. So, here we go.
到处都是“在QPaintDevice之前”的问题,我的错误无处不在。所以,我们开始吧。
I need an extern QWidget to be able to get access to it from outside (because I don't know any other ways to do it). Basically, I need this: Create 2 QWidgets from 1 window, go to first window and from there hide main window and show second window created by main window (although main window is not main(), it is QWidget too).
我需要一个外部QWidget来从外部访问它(因为我不知道还有其他的方法)。基本上,我需要这个:从一个窗口创建两个QWidgets,到第一个窗口,然后从那里隐藏主窗口并显示主窗口创建的第二个窗口(虽然主窗口不是main(),它也是QWidget)。
I added
我添加了
extern QWidget *widget = new QWidget
everywhere and everyhow in possible ways, and I still got this message. I suppose, it means that I need to create my QApplication (in main.cpp) and only then declare any QWidgets. But then HOW can I access those QWidgets from another QWidgets?
无论在哪里,无论如何,我仍然得到了这个信息。我想,这意味着我需要创建我的QApplication(在main.cpp中),然后才声明任何qwidget。但是,我如何从另一个QWidgets访问这些QWidgets呢?
Code is here: https://github.com/ewancoder/game/tree/QWidget_before_QApp_problem
代码是:https://github.com/ewancoder/game/tree/QWidget_before_QApp_problem
P.S. The final goal is to be able show and hide both gamewindow.cpp and world.cpp from battle.cpp (just regular class)
最后的目标是能够显示和隐藏两个游戏窗口。cpp和世界。cpp的战斗。cpp(普通类)
And btw, adding Q_OBJECT and #include both don't work.
另外,添加Q_OBJECT和#include都不起作用。
Anyway, if I cannot use functions from one window to another, than what's the point? I can have one window in another, and then another in that one, and then one in that another... but I can't do anything from the last to the previous. After years on Delphi that seems strange to me.
不管怎样,如果我不能用函数从一个窗口到另一个窗口,这是什么意思呢?我可以有一个窗口在另一个,然后另一个在那个,然后一个在另一个…但我不能做任何事,从最后到上一个。在德尔斐呆了几年之后,我觉得很奇怪。
2 个解决方案
1
Don't use extern or otherwise static variables which lead to creation of the widget before the QApplication is created in main. The QApplication must exist before the constructor of the QWidget is executed.
在创建QApplication之前,不要使用外部或其他静态变量来创建小部件。q应用程序必须在QWidget的构造函数执行之前存在。
Instead of sharing the variable via extern, either make the other windows members of the main window, and then make the windows known to each other by passing around pointers, or keep them private in MainWindow and request the actions from the subwindows e.g. via signal/slots. As a generic rule, don't use global variables but class members.
不是通过外部共享变量,而是让主窗口的其他窗口成员,然后通过传递指针来让窗口相互了解,或者让它们在主窗口中保持私有,并从子窗口请求操作,例如通过信号/插槽。作为一个通用规则,不要使用全局变量,而是使用类成员。
In the following FirstWindow (which is supposed hide main window and secondWindow) gets the main window and the second window passed via pointers and then just calls show/hide on them directly.
在下面的FirstWindow(应该隐藏主窗口和secondWindow)中,获取主窗口,第二个窗口通过指针传递,然后直接调用show/hide。
int main(int argc, char **argv) {
QApplication app(argc, argv);
MainWindow mainWindow;
mainWindow.show();
return app.exec();
}
In main window, have two members for the two other windows, say FirstWindow and SecondWindow: class MainWindow : public QMainWindow { ... private: FirstWindow *m_firstWindow; SecondWindow *m_secondWindow; };
在主窗口中,有两个其他窗口的成员,比如FirstWindow和SecondWindow: class MainWindow: public QMainWindow{…私人:FirstWindow * m_firstWindow;SecondWindow * m_secondWindow;};
MainWindow::MainWindow(QWidget *parent) {
m_firstWindow = new FirstWindow; //not pass this as parent as you want to hide the main window while the others are visible)
m_secOndWindow= new SecondWindow;
m_firstWindow->setMainWindow(this);
m_firstWindow->setSecond(m_secondWindow);
m_firstWindow->show(); //Show first window immediately, leave second window hidden
}
MainWindow::~MainWindow() {
//Manual deletion is necessary as no parent is passed. Alternatively, use QScopedPointer
delete m_firstWindow;
delete m_secondWindow;
}
FirstWindow, inline for brevity:
FirstWindow,内联简洁:
class FirstWindow : public QWidget {
Q_OBJECT
public:
explicit FirstWindow(QWidget *parent = 0) : QWidget(parent) {}
void setMainWindow(MainWindow *mainWindow) { m_mainWindow = mainWindow); }
void setSecondWindow(SecondWindow *secondWindow) { m_secOndWindow= secondWindow; }
private Q_SLOTS:
void somethingHappened() { //e.g. some button was clicked
m_mainWindow->hide();
m_secondWindow->show();
}
private:
MainWindow* m_mainWindow;
SecondWindow* m_secondWindow;
};