Qt 记事本应用程序中的分段错误
嗯。我知道我发布的代码太多了,但这个错误是程序员的噩梦。(分段错误) 这是我的第一个应用程序,并得到了Qt creator教程的帮助。请帮忙 notepad.h文件:Qt 记事本应用程序中的分段错误,qt,qt4,qt-creator,Qt,Qt4,Qt Creator,嗯。我知道我发布的代码太多了,但这个错误是程序员的噩梦。(分段错误) 这是我的第一个应用程序,并得到了Qt creator教程的帮助。请帮忙 notepad.h文件: #ifndef NOTEPAD_H #define NOTEPAD_H #include <QMainWindow> #include <QPlainTextEdit> class Notepad : public QMainWindow { Q_OBJECT public: Not
#ifndef NOTEPAD_H
#define NOTEPAD_H
#include <QMainWindow>
#include <QPlainTextEdit>
class Notepad : public QMainWindow
{
Q_OBJECT
public:
Notepad();
protected:
void closeEvent(QCloseEvent *closeEvent);
private slots:
void open();
bool save();
bool saveAs();
void newFile();
void documentWasModified();
private:
QPlainTextEdit *textField;
QString curFile;
QMenu *fileMenu;
QMenu *editMenu;
QMenu *helpMenu;
QToolBar *fileToolBar;
QToolBar *editToolBar;
QAction *newAct;
QAction *loadAct;
QAction *saveAct;
QAction *saveAsAct;
QAction *copyAct;
QAction *pasteAct;
QAction *cutAct;
QAction *exitAct;
void createMenu();
void showStatusBar();
bool maybeSave();
bool saveFile(const QString &fileName);
void loadFile(const QString &fileName);
void setCurrentFile(const QString &fileName);
void createActions();
void createToolBars();
QString strippedName(const QString &fullFileName);
};
#endif
\ifndef记事本
#定义记事本
#包括
#包括
类记事本:公共QMainWindow
{
Q_对象
公众:
记事本();
受保护的:
无效关闭事件(QCloseEvent*closeEvent);
专用插槽:
空开();
bool save();
bool saveAs();
void newFile();
void documentwasdommodified();
私人:
QPlainTextEdit*文本字段;
QString-curFile;
QMenu*文件菜单;
QMenu*编辑菜单;
QMenu*帮助菜单;
QToolBar*文件工具栏;
QToolBar*编辑工具栏;
QAction*新法案;
QAction*loadAct;
行动*拯救行动;
QAction*saveAsAct;
行动*模仿;
行动*行动;
行动*行动;
QAction*exitAct;
void createMenu();
void showStatusBar();
bool-maybeSave();
bool保存文件(常量QString和文件名);
无效加载文件(常量QString和文件名);
void setCurrentFile(常量QString和文件名);
void createActions();
void createToolBars();
QString strippedName(常量QString&fullFileName);
};
#恩迪夫
notepad.cpp文件:
#include <QtGui>
#include <QMainWindow>
#include "notepad.h"
Notepad::Notepad()
{
textField = new QPlainTextEdit;
setCentralWidget(textField);
setWindowTitle(tr("The building of a notepad...."));
createActions();
createMenu();
createToolBars();
showStatusBar();
connect(textField->document(),SIGNAL(contentsChanged()),this,SLOT(documentWasModified()));
setCurrentFile("");
setUnifiedTitleAndToolBarOnMac(true);
}
void Notepad::createActions()
{
newAct = new QAction(QIcon(":/images/new_File.png"),tr("&New"),this);
newAct->setShortcuts(QKeySequence::New);
newAct->setStatusTip("Create A new File!");
connect(newAct,SIGNAL(triggered()),this,SLOT(newFile()));
loadAct = new QAction(QIcon(":/images/open_File.gif"),tr("&Load"),this);
loadAct->setShortcuts(QKeySequence::Open);
loadAct->setStatusTip("Open an existing file!");
connect(loadAct,SIGNAL(triggered()),this,SLOT(open()));
saveAct = new QAction(QIcon(":/images/save_File.png"),tr("&Save"),this);
saveAct->setShortcuts(QKeySequence::Save);
saveAct->setStatusTip("Save the file!");
connect(saveAct,SIGNAL(triggered()),this,SLOT(save()));
copyAct = new QAction(QIcon(":/images/copy.png"),tr("&Copy"),this);
saveAsAct->setShortcuts(QKeySequence::Copy);
saveAsAct->setStatusTip("Copy!");
connect(saveAsAct,SIGNAL(triggered()),this,SLOT(copy()));
saveAsAct = new QAction(QIcon(":/images/save_As.png"),tr("&Save As"),this);
saveAsAct->setShortcuts(QKeySequence::SaveAs);
saveAsAct->setStatusTip("Save As!");
connect(saveAsAct,SIGNAL(triggered()),this,SLOT(saveAs()));
exitAct = new QAction(QIcon(":/images/exit.png"),tr("&Exit"),this);
exitAct->setShortcuts(QKeySequence::Close);
exitAct->setStatusTip("Exit!");
connect(exitAct,SIGNAL(triggered()),this,SLOT(close()));
pasteAct = new QAction(QIcon(":/images/paste.gif"),tr("&Paste"),this);
pasteAct->setShortcuts(QKeySequence::Paste);
pasteAct->setStatusTip("Paste Text!");
connect(pasteAct,SIGNAL(triggered()),textField,SLOT(paste()));
cutAct = new QAction(QIcon(":/images/cut.png"),tr("&Cut"),this);
cutAct->setShortcuts(QKeySequence::Cut);
cutAct->setStatusTip("Cut Text!");
connect(cutAct,SIGNAL(triggered()),textField,SLOT(cut()));
copyAct = new QAction(QIcon(":/images/copy.copy"),tr("&Copy"),this);
copyAct->setShortcuts(QKeySequence::Copy);
copyAct->setStatusTip("copy Text!");
connect(copyAct,SIGNAL(triggered()),textField,SLOT(copy()));
copyAct->setEnabled(false);
cutAct->setEnabled(false);
connect(textField,SIGNAL(copyAvailable(bool)),copyAct,SLOT(setEnabled(bool)));
connect(textField,SIGNAL(copyAvailable(bool)),cutAct,SLOT(setEnabled(bool)));
}
void Notepad::createMenu()
{
fileMenu = menuBar()->addMenu(tr("&File"));
fileMenu->addAction(newAct);
fileMenu->addAction(loadAct);
fileMenu->addAction(saveAct);
fileMenu->addAction(saveAsAct);
fileMenu->addAction(exitAct);
editMenu = menuBar()->addMenu(tr("&Edit"));
editMenu->addAction(cutAct);
editMenu->addAction(copyAct);
editMenu->addAction(pasteAct);
menuBar()->addSeparator();
helpMenu = menuBar()->addMenu(tr("&Help"));
}
void Notepad::createToolBars()
{
fileToolBar = addToolBar(tr("File"));
fileToolBar->addAction(newAct);
fileToolBar->addAction(loadAct);
fileToolBar->addAction(saveAct);
fileToolBar->addAction(saveAsAct);
editToolBar = addToolBar(tr("Edit"));
editToolBar->addAction(cutAct);
editToolBar->addAction(copyAct);
editToolBar->addAction(pasteAct);
}
void Notepad::showStatusBar()
{
statusBar()->showMessage(tr("Ready!"));
}
void Notepad::closeEvent(QCloseEvent *event)
{
if(maybeSave())
event->accept();
else
event->ignore();
}
void Notepad::documentWasModified()
{
setWindowModified(textField->document()->isModified());
}
bool Notepad::saveAs()
{
QString fileName = QFileDialog::getSaveFileName(this);
if(fileName.isEmpty())
return false;
return saveFile(fileName);
}
bool Notepad::save()
{
if(curFile.isEmpty())
return saveAs();
else
return saveFile(curFile);
}
void Notepad::open()
{
if(maybeSave())
{
QString fileName = QFileDialog::getOpenFileName(this);
if(!fileName.isEmpty())
loadFile(fileName);
}
}
void Notepad::newFile()
{
if(maybeSave())
{
textField->clear();
setCurrentFile("");
}
}
bool Notepad::maybeSave()
{
if(textField->document()->isModified())
{
QMessageBox::StandardButton ret;
ret = QMessageBox::warning(this,
tr("Warning!"),
tr("Do you want to save the changes?"),
QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel
);
if(ret==QMessageBox::Cancel)
return false;
if(ret==QMessageBox::Save)
return save();
}
return true;
}
bool Notepad::saveFile(const QString &fileName)
{
QFile file(fileName);
if(!file.open(QFile::WriteOnly | QFile::Text))
{
QMessageBox::warning(this,
tr("Error!"),
tr("%1 file cannot be saved.\nError:").arg(fileName).arg(file.errorString())
);
return false;
}
QTextStream out(&file);
#ifndef QT_NO_CURSOR
QApplication::setOverrideCursor(Qt::WaitCursor);
#endif
out << textField->toPlainText();
#ifndef QT_NO_CURSOR
QApplication::restoreOverrideCursor();
#endif
setCurrentFile(fileName);
statusBar()->showMessage(tr("File Saved.:)"),3000);
return true;
}
void Notepad::loadFile(const QString &fileName)
{
QFile file(fileName);
if(!file.open(QFile::ReadOnly | QFile::Text))
{
QMessageBox::warning(this,
tr("error"),
tr("error loading %1 file.\nError:%2").arg(fileName).arg(file.errorString())
);
return;
}
QTextStream in(&file);
#ifndef QT_NO_CURSOR
QApplication::setOverrideCursor(Qt::WaitCursor);
#endif
textField->setPlainText(in.readAll());
#ifndef QT_NO_CURSOR
QApplication::restoreOverrideCursor();
#endif
setCurrentFile(fileName);
statusBar()->showMessage(tr("File Loaded!"),3000);
}
void Notepad::setCurrentFile(const QString &fileName)
{
QString curFile = fileName;
textField->document()->setModified(false);
setWindowModified(false);
QString shownName = curFile;
if(curFile.isEmpty())
shownName = "untitled.txt";
setWindowFilePath(shownName);
}
QString Notepad::strippedName(const QString &fullFilename)
{
return QFileInfo(fullFilename).fileName();
}
#包括
#包括
#包括“notepad.h”
记事本::记事本()
{
textField=新的QPlainTextEdit;
setCentralWidget(文本字段);
setWindowTitle(tr(“记事本的构建…));
createActions();
createMenu();
创建工具栏();
showStatusBar();
连接(textField->document(),信号(contentsChanged()),此,插槽(documentwasdommodified());
setCurrentFile(“”);
SetUnifiedTitle和ToolBaronMac(true);
}
void Notepad::createActions()
{
newAct=newqaction(QIcon(:/images/new_File.png),tr(&new),this);
newAct->setShortcuts(QKeySequence::New);
newAct->setstatusip(“创建一个新文件!”);
连接(newAct,信号(triggered()),此,插槽(newFile());
loadAct=newqaction(QIcon(:/images/open_File.gif),tr(&Load),this);
loadAct->setShortcuts(QKeySequence::Open);
loadAct->SetStatusIP(“打开现有文件!”);
连接(loadAct,信号(已触发()),此,插槽(打开());
saveAct=newqaction(QIcon(:/images/save_File.png),tr(&save),this);
saveAct->setShortcuts(QKeySequence::Save);
saveAct->setStatusTip(“保存文件!”);
连接(saveAct,信号(triggered()),此,插槽(save());
copyAct=newqaction(QIcon(:/images/copy.png),tr(©),this);
saveAsAct->setShortcuts(QKeySequence::Copy);
saveAsAct->SetStatusIP(“复制!”);
连接(saveAsAct,信号(已触发()),此,插槽(复制());
saveAsAct=newqaction(QIcon(:/images/save_As.png),tr(&save As),this);
saveAsAct->setShortcuts(QKeySequence::SaveAs);
saveAsAct->SetStatusIP(“另存为!”);
连接(saveAsAct,信号(已触发()),此,插槽(saveAs());
exitAct=newqaction(QIcon(:/images/exit.png),tr(&exit),this);
exitAct->setShortcuts(QKeySequence::Close);
exitAct->setStatusTip(“退出!”);
连接(exitAct,信号(已触发()),此,插槽(close());
pasteAct=新的QAction(QIcon(:/images/paste.gif),tr(“&paste”),this);
pasteAct->setShortcuts(QKeySequence::Paste);
pasteAct->SetStatusIP(“粘贴文本!”);
连接(粘贴、信号(触发())、文本字段、插槽(粘贴());
cutAct=新的QAction(QIcon(:/images/cut.png),tr(“&cut”),this);
cutAct->setShortcuts(QKeySequence::Cut);
cutAct->setstatusip(“剪切文本!”);
连接(切割、信号(触发())、文本字段、插槽(切割());
copyAct=newqaction(QIcon(:/images/copy.copy)),tr(“©”),this);
copyAct->setShortcuts(QKeySequence::Copy);
copyAct->SetStatusIP(“复制文本!”);
连接(复制、信号(触发())、文本字段、插槽(复制());
copyAct->setEnabled(错误);
cutAct->setEnabled(假);
连接(文本字段、信号(copyAvailable(bool))、copyAct、插槽(setEnabled(bool));
连接(文本字段、信号(copyAvailable(bool))、剪切、插槽(setEnabled(bool)));
}
作废记事本::createMenu()
{
fileMenu=menuBar()->addMenu(tr(“&File”);
文件菜单->添加操作(新建操作);
文件菜单->添加操作(加载操作);
文件菜单->添加操作(保存操作);
文件菜单->添加操作(saveAsAct);
文件菜单->添加操作(exitAct);
editMenu=menuBar()->addMenu(tr(“&Edit”);
编辑菜单->添加操作(剪切操作);
编辑菜单->添加操作(复制操作);
编辑菜单->添加操作(粘贴操作);
菜单栏()->addSeparator();
helpMenu=menuBar()->addMenu(tr(“&Help”);
}
作废记事本::createToolBars()
{
fileToolBar=addToolBar(tr(“文件”));
文件工具栏->添加操作(新建操作);
文件工具栏->添加操作(加载操作);
文件工具栏->添加操作(保存操作);
文件工具栏->添加操作(saveAsAct);
editToolBar=addToolBar(tr(“编辑”));
编辑工具栏->添加操作(剪切操作);
编辑工具栏->添加操作(复制操作);
编辑工具栏->添加操作(粘贴操作);
}
作废记事本::showStatusBar()
{
状态栏()->showMessage(tr(“Ready!”);
}
作废记事本::closeEvent(QCloseEvent*事件)
{
if(maybeSave())
事件->接受();
其他的
事件->忽略();
}
作废记事本::documentWasModified()
{
setWindowModified(文本字段->文档()->isModified());
}
bool记事本::saveAs()
{
QString fileName=QFileDialog::getSaveFileName(此文件);
if(fileName.isEmpty())
返回false;
返回保存文件(文件名);
}
bool记事本::save()
{
if(curFile.isEmpty())
返回saveAs();
其他的
返回保存文件(curFile);
}
void记事本::open()
{
if(maybeSave())
{
QString fileName=QFileDialog::getOpenFileName(此文件);
如果(!fileName.isEmpty())
加载文件(文件名);
}
}
void Notepad::newFile()
{
if(maybeSave())
{
textField->clear();
setCurrentFile(“”);
}
}
bool记事本::maybeSave()
{
如果(textField->document()->isModified())
{
QMessageBox::StandardButton-ret;
ret=QMessageBox::警告(此,
#include <QApplication>
#include <QtGui>
#include "notepad.h"
int main(int argc, char **argv)
{
//Q_INIT_RESOURCE(application);
QApplication app(argc, argv);
//app.setStyle("motif"); //possible styles: windows,plastique,cde,motif etc.
app.setOrganizationName("Trolltech");
app.setApplicationName("Application Example");
Notepad mainWin;
#if defined(Q_OS_SYMBIAN)
mainWin.showMaximized();
#else
mainWin.show();
#endif
return app.exec();
}
copyAct = new QAction(QIcon(":/images/copy.png"),tr("&Copy"),this);
saveAsAct->setShortcuts(QKeySequence::Copy);
saveAsAct->setStatusTip("Copy!");
connect(saveAsAct,SIGNAL(triggered()),this,SLOT(copy()));
copyAct = new QAction(QIcon(":/images/copy.png"),tr("&Copy"),this);
copyAct->setShortcuts(QKeySequence::Copy);
copyAct->setStatusTip("Copy!");
connect(copyAct,SIGNAL(triggered()),this,SLOT(copy()));