There are several objects in my main window (QMenus, QLabels, QLayouts, the central widget, etc.) that I am realizing don’t need to be declared in the main window’s header file. Instead, it is fine to declare them in the main window’s constructor.
E.g., before:
in mainwindow.h
private:
QMenu *fileMenu;
// etc.
in mainwindow.cpp
MainWindow::MainWindow()
{
fileMenu = menuBar()->addMenu("File");
// etc.
}
vs.
in mainwindow.cpp
MainWindow::MainWindow()
{
QMenu *fileMenu = menuBar()->addMenu("File");
// etc.
}
If scope isn’t an issue (e.g., I don’t need access to fileMenu anywhere other than when I create it in mainwindow’s constructor), does it matter where I declare it? Is there a “best practice” for such situations?
I’m relatively new to Qt/C++, and I realize this might be more of a C++ question than a Qt question.
It is common rule to use smallest scope possible for every variable you declare.
So, you’d better do not make them class members (not declare them in header file).