PDA

View Full Version : menuBar() Problem



Cutey
23rd January 2007, 11:00
Hi Buddies,
In my application I need to disable and enable the "MenuBar" in some circumstance. I am facing a problem( application crash ) while enabling "MenuBar".

Here is the code snippet:


void DWCMainWindow::setMainMenu( bool setCondn ) {
menuBar()->setEnabled ( setCondn );
}
Note :
I am not facing any problem while disables the MenuBar.

Any Help...
Thanks in Advance.

aamer4yu
24th January 2007, 04:19
whats the error that you are getting ??

Cutey
24th January 2007, 10:59
It is not showing any error. Just it crashes the application.

aamer4yu
24th January 2007, 11:07
i tried enabling / disabling menubar with some button click on tool bar...
it working fine for me...
can u reproduce some code that gives your error ??

jpn
24th January 2007, 11:09
Maybe you are trying to access the menubar by the time it has not yet been constructed or has already been destructed? Add a check if QMainWindow::menuBar() happens to return NULL and/or run your application through a debugger and post the backtrace/callstack.

Cutey
25th January 2007, 06:52
Hi Aamer4yu,

Me too tried a sample code to enable/disable menubar with some button click, it worked fine.
But in my project I couldnt figure out the problem. I suspect the problem with loading back the menu item in the menubar.

Any more Idea plz send word.

Hi JPN,

As you said,I have tried the following code

void DWCMainWindow::setMainMenu( bool setCondn ) {
QMenuBar* mnu = menuBar();/*This line causes the crash, while enable menuBar() returns NULL.*/
mnu->setEnabled ( setCondn );
}
plz tell me the wrokaround. Waiting for your Help.:confused:

Thanks for your interest guys:)

aamer4yu
25th January 2007, 07:15
Dont know why your application is crashing.. I tried the menu with QMenuBar pointer too... and its still working fine for me...
also from source code, menuBar creates a menubar if it doesnt exist..


QMenuBar *QMainWindow::menuBar() const
{
QMenuBar *menuBar = qobject_cast<QMenuBar *>(d_func()->layout->menuBar());
if (!menuBar) {
QMainWindow *self = const_cast<QMainWindow *>(this);
menuBar = new QMenuBar(self);
self->setMenuBar(menuBar);
}
return menuBar;
}

I suggest one thing... catch the closEvent for your window... and trace back as to what caused it... it will give an idea why ur program is crashing...

by the way, by your crash, I guess it means ur application closes without any error ?? right ?

gri
25th January 2007, 08:16
What about building your application and Qt in debug mode and stepping through?
(hope thats not too difficult for you)

Cutey
25th January 2007, 11:27
I suggest one thing... catch the closEvent for your window... and trace back as to what caused it... it will give an idea why ur program is crashing...

by the way, by your crash, I guess it means ur application closes without any error ?? right ?

Hi Gri,

What about building your application and Qt in debug mode and stepping through?
What do you mean ? I couldnt understand ..?

Hi Aamer4yu,

My application constructs under Qt 3.3.4 on Windows XP.
While trace I got the following error.
"Unhandled exception in xxxxxx.exe: Access Violation" ://QMenuBar* mnu = menuBar(); is raising this error. menuBar() returns NULL value. How to handle that case.?

It became hectic to me.. Please help me..

aamer4yu
25th January 2007, 11:50
I am not famliar with Qt 3... :(

neways I have few doubts...
1) where are you calling setMainMenu() function ??
2) what is mainwindow derived from ?
3) are u trying to enable / disable menu from setMainMenu function, or are u tryung to create a menu ??

rest i agree with gri.... u should step thru ur code...
ie. moving line by line in ur code, while debugging it...

also can u post a minimal example showing the problem u r facing ?