好的。我知道我发布了太多代码,但错误是程序员的噩梦。(分段错误)这是我的第一个应用程序,并得到了 Qt 创建者教程的帮助。请帮忙。
notepad.h 文件:
#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
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();
}
主.cpp:
#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();
}