PDA

View Full Version : QMenu's QAction works with QShortcut, not with button



Wasabi
4th July 2011, 16:51
I'm facing an odd problem with a code of mine. I have a menu item "Open File" which has a shortcut Ctrl+C. If I run the program and hit Ctrl+C, the program works like a charm, no problems. However, if I instead go to the menu item and click it, the function itself also works like a charm, but at the end of it, the program crashes. Here's the code:


Canvas::Canvas(const QPoint& Position, const QSize& Size,QWidget* Parent) :
QSFMLCanvas(Parent, Position, Size),
MenuBar(this)
{
setMouseTracking(true);
setAttribute(Qt::WA_DeleteOnClose);
points.reserve(3000);

EqualizeSize=0;
PointsVisible=true;

QMenu* menu = MenuBar.addMenu("File");
menu->addAction("&Open",this,SLOT(OpenFile()),QKeySequence::Open);
menu->addAction("&Save",this,SLOT(SaveFile()),QKeySequence::Save);
menu->addAction("&Quit",QCoreApplication::instance(),SLOT(quit()),QKeySeq uence::Quit);
//...

The shortcut is never redefined anywhere else in the code (the code above is, in fact, the only time the function OpenFile() is called).

Am I doing anything wrong here? Why does using "Ctrl+C" work, but using the menu-button crash, at the end of the OpenFile() function? Does the fact that I set the menu ("File") as a temporary variable have anything to do with this?

Wasabi
5th July 2011, 02:55
Any help please?

Urthas
5th July 2011, 06:11
You should post the rest of the code.

By the way, how is it that QKeySequence::Open maps to Ctrl+C for you, and not Ctrl+O per the docs?

Wasabi
5th July 2011, 17:31
Oh, wow. I meant Ctrl+O, of course. Don't know what happened there.

Here's the rest of the constructor. I can't post the remainder of the code because, well, its a few thousand lines long. The class Canvas inherits from QWidget and sf::RenderWindow, from the SFML Graphics library.


Canvas::Canvas(const QPoint& Position, const QSize& Size,QWidget* Parent) :
QSFMLCanvas(Parent, Position, Size),
MenuBar(this)
{
setMouseTracking(true);
setAttribute(Qt::WA_DeleteOnClose);
points.reserve(3000);

EqualizeSize=0;
PointsVisible=true;

QMenu* menu = MenuBar.addMenu("File");
menu->addAction("&Open",this,SLOT(OpenFile()),QKeySequence::Open);
menu->addAction("&Save",this,SLOT(SaveFile()),QKeySequence::Save);
menu->addAction("&Quit",QCoreApplication::instance(),SLOT(quit()),QKeySeq uence::Quit);
menu = MenuBar.addMenu("Edit");
menu->setEnabled(false);
connect(this,SIGNAL(FileLoaded(bool)),menu,SLOT(se tEnabled(bool)));
QAction* action = menu->addAction("&Fault Properties",this,SLOT(FaultPropertiesDlg()),QString("Ctrl+F"));
action->setEnabled(false);
connect(this,SIGNAL(FileLoaded(bool)),action,SLOT( setEnabled(bool)));
action = menu->addAction("&Equalize Fault Segments",this,SLOT(EqualizeDlg()),QString("Ctrl+E"));
action->setEnabled(false);
connect(this,SIGNAL(FileLoaded(bool)),action,SLOT( setEnabled(bool)));
menu->addSeparator();
action = menu->addAction("&Define Seafloor",this,SLOT(SeafloorDlg()),QString("Ctrl+D"));
action->setEnabled(false);
connect(this,SIGNAL(FileLoaded(bool)),action,SLOT( setEnabled(bool)));
action = menu->addAction("&New Horizon",this,SLOT(NewHorizonDlg()),QString("Ctrl+N"));
action->setEnabled(false);
connect(this,SIGNAL(SeaFloorDefined(bool)),action, SLOT(setEnabled(bool)));
action = menu->addAction("&Horizon Properties",this,SLOT(HorizonPropertiesDlg()),QString("Ctrl+H"));
action->setEnabled(false);
connect(this,SIGNAL(SeaFloorDefined(bool)),action, SLOT(setEnabled(bool)));
menu = MenuBar.addMenu("View");
menu->setEnabled(false);
connect(this,SIGNAL(FileLoaded(bool)),menu,SLOT(se tEnabled(bool)));
action = menu->addAction("&Global View",this,SLOT(ChangeView_Key()),QString("Ctrl+G"));
action->setEnabled(false);
connect(this,SIGNAL(FileLoaded(bool)),action,SLOT( setEnabled(bool)));
action = menu->addAction("&Show Sealevel",this,SLOT(ViewSealevel()));
action->setEnabled(false);
action->setCheckable(true);
action->setChecked(true);
connect(this,SIGNAL(FileLoaded(bool)),action,SLOT( setEnabled(bool)));
connect(this,SIGNAL(ViewingSealevel(bool)),action, SLOT(setChecked(bool)));
action = menu->addAction("Show &Points",this,SLOT(ViewPoints()));
action->setEnabled(false);
action->setCheckable(true);
action->setChecked(true);
connect(this,SIGNAL(FileLoaded(bool)),action,SLOT( setEnabled(bool)));
connect(this,SIGNAL(ViewingPoints(bool)),action,SL OT(setChecked(bool)));
menu = MenuBar.addMenu("Analyze");
menu->setEnabled(false);
connect(this,SIGNAL(SeaFloorDefined(bool)),menu,SL OT(setEnabled(bool)));
action = menu->addAction("Estimate Maximum PPI",this,SLOT(PPIDlg()),QString("Ctrl+A"));
action->setEnabled(false);
connect(this,SIGNAL(SeaFloorDefined(bool)),action, SLOT(setEnabled(bool)));
action = menu->addAction("View Geostatic Ratio",this,SLOT(ViewRatio()),QString("Ctrl+R"));
action->setEnabled(false);
action->setCheckable(true);
connect(this,SIGNAL(RatioCalculated(bool)),action, SLOT(setEnabled(bool)));
connect(this,SIGNAL(ViewingRatio(bool)),action,SLO T(setChecked(bool)));
}