View Full Version : Using the menubar

27th February 2006, 11:31
in the menubar of my program i have a entry "new" and i want to use it to execute a function of mine, how would i do that?
The function will change the value of some lineedits.
i guess it will work with signals? but how i can execute my function?

Thx for your help


27th February 2006, 11:43
Make sure the function you need executed is declared as a slot, and connect the Qpopupmenu's activated(int) signal to it and check the int for being the correct item...

Better and more compatible with Qt4 is to use QAction objects which have their own signals per action...
like so

QAction *fileNewAction=new QAction("&New...", QKeySequence("Ctrl+N"),this);
QPopupMenu *fileMenu=new QPopupMenu(this);
connect(fileNewAction,SIGNAL(activated()),this,SLO T(handleFileNew()));

27th February 2006, 11:51
Some strange question...What nkind of new item you have? QAction or QPopupMenu..for first you have answer from ReilenBlaeyze

for QPopupMenu

connect(newMenu,SIGNAL(activated(int)),this,SLOT(h andleMenuActivate(int)));

27th February 2006, 12:10
First thx for the fast reply.

Yes i thought i was doing it the right at least i tried it that way making a public slot writing my function there and try to use it but doesn't work the function itself works but when i want to connect it it just doesn't do anything.

in my class i made a public slots: section with my function in it.

Any ideas what i could have done wrong?

27th February 2006, 14:05
Have you declare Q_OBJECT macros in header file?
Show your code pls

27th February 2006, 14:27
#include <QtGui/QMainWindow>
#include "ui_sudoku.h"

class Sudoku : public QMainWindow

Sudoku(QWidget *parent = 0, Qt::WFlags flags = 0);

public slots:
void news();

Ui::SudokuClass ui;


27th February 2006, 14:38
Yes its nice... but without implementation its hard give you your problem solving

27th February 2006, 15:14
What implementaion in the ui_sudoku.h i have it like
QObject::connect(actionNew_game, SIGNAL(activated()), SudokuClass, SLOT(news()));

what else you need?

27th February 2006, 15:20
Its you need :)

Code that creating and adding actions to your menu

Have you see some messages, alike "could not connect bla bla bla" in debug window?

27th February 2006, 15:52
Hm funny now it works.
Don't know what i changed really just compiled it again and now it works mh
Thanks a lot for your help!

but is it normal that i can't connect the signal to the slot in the "Signal slot editor"? It doesn't show my function in the "slot" window.

27th February 2006, 15:59
It becouse your method doesnt have the same input parameters as signal