Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Beginner request
Qt 6.11 is out! See what's new in the release blog

Beginner request

Scheduled Pinned Locked Moved General and Desktop
4 Posts 4 Posters 2.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    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.cpp

    Could 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_OBJECT

    public:
    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_OBJECT

    public:
    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

    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • ? Offline
          ? Offline
          A Former User
          wrote on last edited by
          #4

          Well you don't need a separate class for a menu, you just could have a function that returns a QMenuBar and set it.

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved