PDA

View Full Version : Menu bar not appearing



GUIman
22nd February 2011, 22:54
Just started using Qt and am wondering why the menu bar doesn't show up, when I run the app. Thank you in advance for helping... Here is the code

mainwindow.h

#ifndef LINES_H
#define LINES_H

#include <QWidget>
#include <qmainwindow.h>
#include <QMainWindow>

class QMenu;
class QAction;
class QLabel;

class Lines : public QMainWindow
{
Q_OBJECT

public:
Lines();

private:
void createActions();
void updateActions();
void createMenus();
QMenu *fileMenu;
//QMenu *moveMenu;
QAction *exitAct;

protected:
void paintEvent(QPaintEvent *event);
};

#endif


lines.cpp

#include <QtGui>
#include "mainwindow.h"
#include <QApplication>
#include <QPainter>


Lines::Lines()
{
createActions();
createMenus();

setWindowTitle(tr("Tic Tac Toe"));
resize(270, 270);
}

void Lines::paintEvent(QPaintEvent *event)
{
QPen pen(Qt::black, 2, Qt::SolidLine);

QPainter painter(this);

painter.setPen(pen);
painter.drawLine(93, 20, 93, 250);
painter.drawLine(177, 20, 177, 250);
painter.drawLine(20, 93, 250, 93);
painter.drawLine(20, 177, 250, 177);
//Draw test x
painter.drawLine(30, 30, 80, 80);
painter.drawLine(80, 30, 30, 80);
}

void Lines::createActions()
{
exitAct = new QAction(tr("E&xit"), this);
exitAct->setShortcut(tr("Ctrl+Q"));
connect(exitAct, SIGNAL(triggered()), this, SLOT(close()));
}

void Lines::createMenus()
{
fileMenu = new QMenu(tr("&File"), this);
fileMenu->addAction(exitAct);
}


main.cpp

#include "mainwindow.h"
#include <QDesktopWidget>
#include <QApplication>

int main(int argc, char *argv[])
{
QApplication app(argc, argv);
Lines Lines;
Lines.show();
return app.exec();
}

ChrisW67
23rd February 2011, 04:00
QMainWindow::menuBar() and QMenuBar::addMenu()

GUIman
23rd February 2011, 05:17
I am completely new to using the qt framework. could you please explain how I could modify my code to implement your fixes?
Thank you for putting up with me.

BalaQT
23rd February 2011, 05:56
I am completely new to using the qt framework. could you please explain how I could modify my code to implement your fixes?
Thank you for putting up with me.
hi,
welcome to QtCentre.
As Chris said, after creating the menus
Just add ,


Lines::Lines()
{
createActions();
createMenus();
menuBar()->addMenu(fileMenu);
...............
...............
}

in class Lines constructor.

hope it helps.
Bala

ChrisW67
23rd February 2011, 06:23
I'd put it in createMenus() myself. Seems the logical place for it.

GUIman
23rd February 2011, 07:00
Thank you so much
I got it now!