Beginner request
-
Hello all,
This is a very simple request, but I am not managing to make it for about 2 weeks now.
All I want yo produce is a main window with one menu item, which I know how to do (thanks to the great tutorials), but I need every thing related to menu to be stored in differnt class, so I need to produce 5 files:
main.cpp
mainwindow.h
mainwindow.cpp
menu.h
menu.cppCould anyone please point me to how I can do it, or fix the following so I would manage to build on that:
main.cpp
@
#include <QtGui>
#include <QApplication>
#include "mainwindow.h"int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MainWindow mainWin;
mainWin.show();
mainWin.showMaximized();
return app.exec();
}
@mainwindow.h
@
#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>
class MainWindow : public QMainWindow
{
Q_OBJECTpublic:
MainWindow();private slots:
private:
void createStatusBar();};
#endif
@mainwindow.cpp
@
#include <QtGui>
#include "mainwindow.h"
#include "menu.h"MainWindow::MainWindow()
{
setWindowTitle(tr("test menus"));
createStatusBar();Menus *menus;
menus->menuSimple();
}void MainWindow::createStatusBar()
{
//The connction status widget
QLabel *connection_status = new QLabel;
connection_status->setText("Status: Not connected");
statusBar()->insertPermanentWidget(1, connection_status, 20);
}
@menu.h
@
#ifndef MENU_H
#define MENU_H#include <QMainWindow>
class Menus : public QMainWindow
{
Q_OBJECTpublic:
Menus();
void menuSimple();private:
void menuFull();
QMenu *helpMenuSimple;
QAction *newAct;private slots:
void about();
};
#endif
@menu.cpp
@
#include <QtGui>
#include "menu.h"Menus::Menus()
{
menuSimple();}
void Menus::menuSimple()
{helpMenuSimple = menuBar()->addMenu(tr("&Help"));
helpMenuSimple->addAction(newAct);
newAct = new QAction(QIcon(":/images/new.png"), tr("&New"), this);}
void Menus::about()
{
QMessageBox::about(this, tr("About Application"),
tr("The <b>Application</b> example demonstrates how to "
"write modern GUI applications using Qt, with a menu bar, "
"toolbars, and a status bar."));
}
@Would somebody please be so kind and make this work for me?
Your help is greatly appreciated.
Thanks
-
Hi!
There are quite a few things wrong.
I would recommend that you take a look of some other tutorials ( not only Qt but C++ also).Other wise in MainWindow Constructor you create only pointer, that isn't showing to Menus class.
@Menus *menus = new Menus();@
Is the solution for you crashes.And as for Menus, just add them in MainWindow. No additional class is required.
-
And make your QObject/QWidget based classes take a QObject/QWidget parent pointer, as all the Qt provided widgets do too. Don't forget it to pass it to the base class constructor. Otherwise you loos the nice parent/child relationship with automagic memory cleanup.
-
Well you don't need a separate class for a menu, you just could have a function that returns a QMenuBar and set it.