我在 Qt 项目中遇到 EXC_BAD_ACCESS 问题。调试器显示它无法访问 menuBar 的内存(Ui 标题行 78)。我不明白的是,如果我在菜单栏布局中只使用 6 个 QAction 项目,它不会给出这个错误。仅当有超过 6 个 QAction 项(在本例中为 actionA7)时才会发生这种情况。任何人都有类似的经验,如果有任何解决方案,那就太好了。谢谢。
ui头文件:
/********************************************************************************
** Form generated from reading UI file 'spreadsheet_mainwindow.ui'
**
** Created: Thu Aug 23 10:22:10 2012
** by: Qt User Interface Compiler version 4.8.1
**
** WARNING! All changes made in this file will be lost when recompiling UI file!
********************************************************************************/
#ifndef UI_SPREADSHEET_MAINWINDOW_H
#define UI_SPREADSHEET_MAINWINDOW_H
#include <QtCore/QVariant>
#include <QtGui/QAction>
#include <QtGui/QApplication>
#include <QtGui/QButtonGroup>
#include <QtGui/QHeaderView>
#include <QtGui/QMainWindow>
#include <QtGui/QMenu>
#include <QtGui/QMenuBar>
#include <QtGui/QStatusBar>
#include <QtGui/QToolBar>
#include <QtGui/QWidget>
QT_BEGIN_NAMESPACE
class Ui_SpreadSheet_MainWindow
{
public:
QAction *actionA1;
QAction *actionA2;
QAction *actionA3;
QAction *actionA4;
QAction *actionA5;
QAction *actionA6;
QAction *actionA7;
QWidget *centralWidget;
QToolBar *mainToolBar;
QStatusBar *statusBar;
QMenuBar *menuBar;
QMenu *menuFile;
void setupUi(QMainWindow *SpreadSheet_MainWindow)
{
if (SpreadSheet_MainWindow->objectName().isEmpty())
SpreadSheet_MainWindow->setObjectName(QString::fromUtf8("SpreadSheet_MainWindow"));
SpreadSheet_MainWindow->resize(400, 300);
actionA1 = new QAction(SpreadSheet_MainWindow);
actionA1->setObjectName(QString::fromUtf8("actionA1"));
actionA2 = new QAction(SpreadSheet_MainWindow);
actionA2->setObjectName(QString::fromUtf8("actionA2"));
actionA3 = new QAction(SpreadSheet_MainWindow);
actionA3->setObjectName(QString::fromUtf8("actionA3"));
actionA4 = new QAction(SpreadSheet_MainWindow);
actionA4->setObjectName(QString::fromUtf8("actionA4"));
actionA5 = new QAction(SpreadSheet_MainWindow);
actionA5->setObjectName(QString::fromUtf8("actionA5"));
actionA6 = new QAction(SpreadSheet_MainWindow);
actionA6->setObjectName(QString::fromUtf8("actionA6"));
actionA7 = new QAction(SpreadSheet_MainWindow);
actionA7->setObjectName(QString::fromUtf8("actionA7"));
centralWidget = new QWidget(SpreadSheet_MainWindow);
centralWidget->setObjectName(QString::fromUtf8("centralWidget"));
SpreadSheet_MainWindow->setCentralWidget(centralWidget);
mainToolBar = new QToolBar(SpreadSheet_MainWindow);
mainToolBar->setObjectName(QString::fromUtf8("mainToolBar"));
SpreadSheet_MainWindow->addToolBar(Qt::TopToolBarArea, mainToolBar);
statusBar = new QStatusBar(SpreadSheet_MainWindow);
statusBar->setObjectName(QString::fromUtf8("statusBar"));
SpreadSheet_MainWindow->setStatusBar(statusBar);
menuBar = new QMenuBar(SpreadSheet_MainWindow);
menuBar->setObjectName(QString::fromUtf8("menuBar"));
menuBar->setGeometry(QRect(0, 0, 400, 22));
menuFile = new QMenu(menuBar);
menuFile->setObjectName(QString::fromUtf8("menuFile"));
SpreadSheet_MainWindow->setMenuBar(menuBar);
menuBar->addAction(menuFile->menuAction());
menuFile->addAction(actionA1);
menuFile->addAction(actionA2);
menuFile->addAction(actionA3);
menuFile->addAction(actionA4);
menuFile->addAction(actionA5);
menuFile->addAction(actionA6);
menuFile->addAction(actionA7);
retranslateUi(SpreadSheet_MainWindow);
QMetaObject::connectSlotsByName(SpreadSheet_MainWindow);
} // setupUi
void retranslateUi(QMainWindow *SpreadSheet_MainWindow)
{
SpreadSheet_MainWindow->setWindowTitle(QApplication::translate("SpreadSheet_MainWindow", "SpreadSheet_MainWindow", 0, QApplication::UnicodeUTF8));
actionA1->setText(QApplication::translate("SpreadSheet_MainWindow", "A1", 0, QApplication::UnicodeUTF8));
actionA2->setText(QApplication::translate("SpreadSheet_MainWindow", "A2", 0, QApplication::UnicodeUTF8));
actionA3->setText(QApplication::translate("SpreadSheet_MainWindow", "A3", 0, QApplication::UnicodeUTF8));
actionA4->setText(QApplication::translate("SpreadSheet_MainWindow", "A4", 0, QApplication::UnicodeUTF8));
actionA5->setText(QApplication::translate("SpreadSheet_MainWindow", "A5", 0, QApplication::UnicodeUTF8));
actionA6->setText(QApplication::translate("SpreadSheet_MainWindow", "A6", 0, QApplication::UnicodeUTF8));
actionA7->setText(QApplication::translate("SpreadSheet_MainWindow", "A7", 0, QApplication::UnicodeUTF8));
menuFile->setTitle(QApplication::translate("SpreadSheet_MainWindow", "File", 0, QApplication::UnicodeUTF8));
} // retranslateUi
};
namespace Ui {
class SpreadSheet_MainWindow: public Ui_SpreadSheet_MainWindow {};
} // namespace Ui
QT_END_NAMESPACE
#endif // UI_SPREADSHEET_MAINWINDOW_H
头文件:
#ifndef SPREADSHEET_MAINWINDOW_H
#define SPREADSHEET_MAINWINDOW_H
#include <ui_spreadsheet_mainwindow.h>
class SpreadSheet_MainWindow : public QMainWindow , public Ui_SpreadSheet_MainWindow
{
Q_OBJECT
public:
SpreadSheet_MainWindow(QWidget *parent = 0);
};
#endif // SPREADSHEET_MAINWINDOW_H
cp文件:
#include <QtGui>
#include "spreadsheet_mainwindow.h"
SpreadSheet_MainWindow::SpreadSheet_MainWindow(QWidget *parent) :
QMainWindow(parent)
{
setupUi(this);
}
主cpp文件:
#include <QtGui/QApplication>
#include "spreadsheet_mainwindow.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
SpreadSheet_MainWindow w;
w.show();
return a.exec();
}