Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8640349
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T11:15:31+00:00 2026-06-12T11:15:31+00:00

Ok. I know I am posting way too much code, but the error is

  • 0

Ok. I know I am posting way too much code, but the error is a programmers nightmare.(The segmentation fault)
This is my first application and has assisted by the Qt creator tutorial. Please help.

the notepad.h file:

#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

The notepad.cpp file:

#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();
}

The main.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();
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T11:15:32+00:00Added an answer on June 12, 2026 at 11:15 am

    Debugger is your friend. Learn to use it.

    There is a copy-paste error:

    copyAct = new QAction(QIcon(":/images/copy.png"),tr("&Copy"),this);
    saveAsAct->setShortcuts(QKeySequence::Copy);
    saveAsAct->setStatusTip("Copy!");
    connect(saveAsAct,SIGNAL(triggered()),this,SLOT(copy()));
    

    this should be:

    copyAct = new QAction(QIcon(":/images/copy.png"),tr("&Copy"),this);
    copyAct->setShortcuts(QKeySequence::Copy);
    copyAct->setStatusTip("Copy!");
    connect(copyAct,SIGNAL(triggered()),this,SLOT(copy()));
    

    You were using an undefined pointer. It is not a nightmare a debugger cannot handle.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know this maybe simple and I know the code I am posting is
I'm posting this question because I do not know the best/correct way of doing
Know this might be rather basic, but I been trying to figure out how
I know that this sort of question has been asked here before, but still
i know this is a stupid question but i d'ont know how to do
Sorry for posting such a lowly beginner's question, but I just still don't know
I have researched this topic, but am afraid I have too little experience with
I know this type of question has been asked a lot, but I really
Hey, sorry for posting this here, I know that this question better suites into
Ok, I know this is a mess. This is why I'm posting it here.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.