View Full Version : Using the menubar
Steve
27th February 2006, 12:31
Hi,
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
Steve
ReilenBlaeyze
27th February 2006, 12: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);
fileNewAction->addTo(fileMenu);
connect(fileNewAction,SIGNAL(activated()),this,SLO T(handleFileNew()));
zlatko
27th February 2006, 12: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)));
Steve
27th February 2006, 13: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?
zlatko
27th February 2006, 15:05
Have you declare Q_OBJECT macros in header file?
Show your code pls
Steve
27th February 2006, 15:27
#include <QtGui/QMainWindow>
#include "ui_sudoku.h"
class Sudoku : public QMainWindow
{
Q_OBJECT
public:
Sudoku(QWidget *parent = 0, Qt::WFlags flags = 0);
~Sudoku();
public slots:
void news();
private:
Ui::SudokuClass ui;
};
zlatko
27th February 2006, 15:38
Yes its nice... but without implementation its hard give you your problem solving
Steve
27th February 2006, 16: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?
zlatko
27th February 2006, 16: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?
Steve
27th February 2006, 16: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.
zlatko
27th February 2006, 16:59
It becouse your method doesnt have the same input parameters as signal
Powered by vBulletin® Version 4.2.5 Copyright © 2023 vBulletin Solutions Inc. All rights reserved.