Welcome! Log In Create A New Profile

Advanced

Example 11.1 (mainwindow)

Posted by djbass 
Announcements Last Post
Announcement myUnisa availability 21 to 24 March 2019 03/17/2019 02:24PM
Announcement SoC Curricula 09/30/2017 01:08PM
Announcement Demarcation or scoping of examinations and assessment 02/13/2017 07:59AM
Announcement School of Computing Short Learning Programmes 11/24/2014 08:37AM
Announcement Unisa contact information 07/28/2011 01:28PM
Example 11.1 (mainwindow)
September 07, 2007 11:26AM
Hi guys

pls help! i am trying to do example 11.1, i compiled it succesfully with no errors but when i run it nothing is displayed, i only get the following message under the Outputs window:

/****************************************************************************
/
/ Running...
/ QApplication::palette() failed: please instantiate the QApplication object
/ first.
/ QWidget: Must construct a QApplication before a QPaintDevice
/
/****************************************************************************

your help will be much appreciated

thanxconfused smiley

DJ BASS
iva
Re: Example 11.1 (mainwindow)
September 07, 2007 12:14PM
silly question , but have u instantiated QApplication in your main method :

QApplication app(argc, argv);

maybe post your main method code here
Re: Example 11.1 (mainwindow)
September 07, 2007 01:21PM
here is the main program.

#include "mymainwindow.h"
#include <QApplication>

int main( int argc, char ** argv ) {
QApplication a( argc, argv );
MyMainWindow mw;
mw.show();
return a.exec();
}
iva
Re: Example 11.1 (mainwindow)
September 07, 2007 02:03PM
crikey sorry, i have no idea.. if you want mail me all your files i can try compile it when i get home
Re: Example 11.1 (mainwindow)
September 07, 2007 02:26PM
u're e-mail is hidden........here are all the files.

HEADER FILE

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

class QAction;
class QLabel;
class QMenu;
class QTextEdit;
class QStackedWidget;

/** @short customized version of QMainWindow

Window size/position saved across executions
A debug window in the bottom dock area.

*/
//start
class MyMainWindow : public QMainWindow {
Q_OBJECT

public:
MyMainWindow();
void closeEvent(QCloseEvent *event); /* overridden
from base class to capture when the user wants to
close the window */

protected slots:
virtual void newFile();
virtual void open();
virtual bool save();
//end
virtual bool saveAs();
virtual void about();
virtual void documentWasModified();
virtual QWidget* initialWidget() const;

protected:

virtual void createActions();
virtual void createMenus();
virtual void createHelpMenu();
virtual void createFileMenu();
virtual void createEditMenu();
virtual void createToolBars();
virtual void createStatusBar();
virtual void readSettings();
virtual void writeSettings();
virtual bool maybeSave();
virtual void loadFile(const QString &fileName);
virtual void saveFile(const QString &fileName);
virtual void setCurrentFile(const QString &fileName);
QString strippedName(const QString &fullFileName);
QStackedWidget *m_StackedWidget;
QTextEdit *m_TextEdit;
QTextEdit *m_DebugWindow;

QLabel *m_ModLabel;
QString m_FileName;

QMenu *m_FileMenu;
QMenu *m_EditMenu;
QMenu *m_HelpMenu;
QToolBar *m_FileToolBar;
QToolBar *m_EditToolBar;
QAction *m_NewAction;
QAction *m_OpenAction;
QAction *m_SaveAction;
QAction *m_SaveAsAction;
QAction *m_ExitAction;
QAction *m_CutAction;
QAction *m_CopyAction;
QAction *m_PasteAction;
QAction *m_AboutAction;
QAction *m_AboutQtAction;
//end
};

#endif

IMPLEMENTATION CODE

#include <QtGui>
#include "mymainwindow.h"
#include <QDockWidget>
#include <logwindow.h>
#include <QStackedWidget>

MyMainWindow::MyMainWindow() {

/* The main window is a stacked widget, inside a dock widget.
This gives us the ability to dock and un-dock, resize, as well
as change the contents (toggle, or animate) */

m_StackedWidget = new QStackedWidget();
m_TextEdit = new QTextEdit(m_StackedWidget);
QDockWidget* mainDock = new QDockWidget("Text Editor"winking smiley;
mainDock->setWidget(m_TextEdit);
setCentralWidget(mainDock);

LogWindow *lw = new LogWindow();
lw->log("debug"winking smiley;
m_DebugWindow = lw;

QDockWidget *debugdock = new QDockWidget("Debug Window"winking smiley;
debugdock->setWidget(m_DebugWindow);
addDockWidget(Qt::BottomDockWidgetArea, debugdock);

qDebug() << "This is a debug message" ;

createActions();
createMenus();
createToolBars();
createStatusBar();
readSettings();

connect(m_TextEdit->document(), SIGNAL(contentsChanged()),
this, SLOT(documentWasModified()));

setWindowTitle(tr("Application"winking smiley);
}

void MyMainWindow::closeEvent(QCloseEvent *event) {
if (maybeSave()) {
writeSettings();
event->accept();
} else {
event->ignore();
}
}

void MyMainWindow::newFile() {
if (maybeSave()) {
m_TextEdit->clear();
setCurrentFile(""winking smiley;
}
}

void MyMainWindoweye popping smileypen() {
if (maybeSave()) {
QString fileName = QFileDialog::getOpenFileName(this);
if (!fileName.isEmpty())
loadFile(fileName);
}
}

bool MyMainWindow::save() {
if (m_FileName.isEmpty()) {
return saveAs();
} else {
saveFile(m_FileName);
return true;
}
}

bool MyMainWindow::saveAs() {
QString fileName = QFileDialog::getSaveFileName(this);
if (fileName.isEmpty())
return false;

if (QFile::exists(fileName)) {
int ret = QMessageBox::warning(this, tr("Application"winking smiley,
tr("File %1 already exists.\n"
"Do you want to overwrite it?"winking smiley
.arg(QDir::convertSeparators(fileName)),
QMessageBox::Yes | QMessageBox:grinning smileyefault,
QMessageBox::No | QMessageBox::Escape);
if (ret == QMessageBox::No)
return true;
}
saveFile(fileName);
return true;
}

void MyMainWindow::about() {
QMessageBox::about(this, tr("About Application"winking smiley,
tr("The <b>Application</b> example demonstrates how to "
"write modern GUI applications using Qt, with a menu bar, "
"dock widgets, toolbars, and a status bar."winking smiley);
}

void MyMainWindow::documentWasModified() {
m_ModLabel->setText(tr("MOD"winking smiley);
}
/* TODO - show how to get some of the standard icons from QApplication->style() */
//start id="action"
void MyMainWindow::createActions() {
m_NewAction = new QAction(QIcon(":/icons/new.png"winking smiley, tr("&New"winking smiley, this);
m_NewAction->setShortcut(tr("Ctrl+N"winking smiley);
m_NewAction->setStatusTip(tr("Create a new file"winking smiley);
connect(m_NewAction, SIGNAL(triggered()), this, SLOT(newFile()));

m_OpenAction = new QAction(QIcon(":/icons/open.png"winking smiley, tr("&Open..."winking smiley, this);
m_OpenAction->setShortcut(tr("Ctrl+O"winking smiley);
m_OpenAction->setStatusTip(tr("Open an existing file"winking smiley);
connect(m_OpenAction, SIGNAL(triggered()), this, SLOT(open()));
//end
m_SaveAction = new QAction(QIcon(":/icons/save.png"winking smiley, tr("&Save"winking smiley, this);
m_SaveAction->setShortcut(tr("Ctrl+S"winking smiley);
m_SaveAction->setStatusTip(tr("Save the document to disk"winking smiley);
connect(m_SaveAction, SIGNAL(triggered()), this, SLOT(save()));

m_SaveAsAction = new QAction(tr("Save &As..."winking smiley, this);
m_SaveAsAction->setStatusTip(tr("Save the document under a new name"winking smiley);
connect(m_SaveAsAction, SIGNAL(triggered()), this, SLOT(saveAs()));

m_ExitAction = new QAction(tr("E&xit"winking smiley, this);
m_ExitAction->setShortcut(tr("Ctrl+Q"winking smiley);
m_ExitAction->setStatusTip(tr("Exit the application"winking smiley);
connect(m_ExitAction, SIGNAL(triggered()), this, SLOT(close()));

m_CutAction = new QAction(QIcon(":/icons/cut.png"winking smiley, tr("Cu&t"winking smiley, this);
m_CutAction->setShortcut(tr("Ctrl+X"winking smiley);
m_CutAction->setStatusTip(tr("Cut the current selection's contents to the "
"clipboard"winking smiley);
connect(m_CutAction, SIGNAL(triggered()), m_TextEdit, SLOT(cut()));

m_CopyAction = new QAction(QIcon(":/icons/copy.png"winking smiley, tr("&Copy"winking smiley, this);
m_CopyAction->setShortcut(tr("Ctrl+C"winking smiley);
m_CopyAction->setStatusTip(tr("Copy the current selection's contents to the "
"clipboard"winking smiley);
connect(m_CopyAction, SIGNAL(triggered()), m_TextEdit, SLOT(copy()));

m_PasteAction = new QAction(QIcon(":/icons/paste.png"winking smiley, tr("&Paste"winking smiley, this);
m_PasteAction->setShortcut(tr("Ctrl+V"winking smiley);
m_PasteAction->setStatusTip(tr("Paste the clipboard's contents into the current "
"selection"winking smiley);
connect(m_PasteAction, SIGNAL(triggered()), m_TextEdit, SLOT(paste()));

m_AboutAction = new QAction(tr("&About"winking smiley, this);
m_AboutAction->setStatusTip(tr("Show the application's About box"winking smiley);
connect(m_AboutAction, SIGNAL(triggered()), this, SLOT(about()));

m_AboutQtAction = new QAction(tr("About &Qt"winking smiley, this);
m_AboutQtAction->setStatusTip(tr("Show the Qt library's About box"winking smiley);
connect(m_AboutQtAction, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
}
//start id="action"
void MyMainWindow::createFileMenu() {
m_FileMenu = menuBar()->addMenu(tr("&File"winking smiley);
m_FileMenu->addAction(m_NewAction);
m_FileMenu->addAction(m_OpenAction);
m_FileMenu->addAction(m_SaveAction);
m_FileMenu->addAction(m_SaveAsAction);
m_FileMenu->addSeparator();
m_FileMenu->addAction(m_ExitAction);
}

//end
void MyMainWindow::createMenus() {
createFileMenu();
createEditMenu();
createHelpMenu();
}


void MyMainWindow::createEditMenu() {

m_EditMenu = menuBar()->addMenu(tr("&Edit"winking smiley);
m_EditMenu->addAction(m_CutAction);
m_EditMenu->addAction(m_CopyAction);
m_EditMenu->addAction(m_PasteAction);
}

void MyMainWindow::createHelpMenu() {
menuBar()->addSeparator();
m_HelpMenu = menuBar()->addMenu(tr("&Help"winking smiley);
m_HelpMenu->addAction(m_AboutAction);
m_HelpMenu->addAction(m_AboutQtAction);
}

void MyMainWindow::createToolBars() {
m_FileToolBar = addToolBar(tr("File"winking smiley);
m_FileToolBar->addAction(m_NewAction);
m_FileToolBar->addAction(m_OpenAction);
m_FileToolBar->addAction(m_SaveAction);

m_EditToolBar = addToolBar(tr("Edit"winking smiley);
m_EditToolBar->addAction(m_CutAction);
m_EditToolBar->addAction(m_CopyAction);
m_EditToolBar->addAction(m_PasteAction);
}

void MyMainWindow::createStatusBar() {
m_ModLabel = new QLabel(tr(" MOD "winking smiley);
m_ModLabel->setAlignment(Qt::AlignHCenter);
m_ModLabel->setMinimumSize(m_ModLabel->sizeHint());
m_ModLabel->clear();

statusBar()->addWidget(m_ModLabel, 0);
}

//start id="settings"
void MyMainWindow::readSettings() {
QSettings settings("objectlearning.net", "Qt4 Sample Main"winking smiley; /* The
constructor takes the organization name and the app name as arguments.*/
QPoint pos = settings.value("pos", QPoint(200, 200)).toPoint();
QSize size = settings.value("size", QSize(400, 400)).toSize();
QByteArray state = settings.value("state", QByteArray()).toByteArray();
restoreState(state);
resize(size);
move(pos);
}

void MyMainWindow::writeSettings() {
/* Save postion/size of main window */

QSettings settings("objectlearning.net", "Qt4 Sample Main"winking smiley;
settings.setValue("pos", pos());
settings.setValue("size", size());
settings.setValue("state", saveState());
}
//end

bool MyMainWindow::maybeSave() {
if (m_TextEdit->document()->isModified()) {
int ret = QMessageBox::warning(this, tr("Application"winking smiley,
tr("The document has been modified.\n"
"Do you want to save your changes?"winking smiley,
QMessageBox::Yes | QMessageBox:grinning smileyefault,
QMessageBox::No,
QMessageBox::Cancel | QMessageBox::Escape);
if (ret == QMessageBox::Yes)
return save();
else if (ret == QMessageBox::Cancel)
return false;
}
return true;
}

void MyMainWindow::loadFile(const QString &fileName) {
QFile file(fileName);
if (!file.open(QFile::ReadOnly | QFile::Text)) {
QMessageBox::warning(this, tr("Application"winking smiley,
tr("Cannot read file %1:\n%2."winking smiley
.arg(fileName).arg(file.errorString()));
return;
}

QTextStream in(&file);
QApplication::setOverrideCursor(Qt::WaitCursor);
m_TextEdit->setPlainText(in.readAll());
QApplication::restoreOverrideCursor();

setCurrentFile(fileName);
statusBar()->showMessage(tr("File loaded"winking smiley, 2000);
}

void MyMainWindow::saveFile(const QString &fileName) {
QFile file(fileName);
if (!file.open(QFile::WriteOnly | QFile::Text)) {
QMessageBox::warning(this, tr("Application"winking smiley,
tr("Cannot write file %1:\n%2."winking smiley
.arg(fileName)
.arg(file.errorString()));
return;
}

QTextStream out(&file);
QApplication::setOverrideCursor(Qt::WaitCursor);
out << m_TextEdit->toPlainText();
QApplication::restoreOverrideCursor();

setCurrentFile(fileName);
statusBar()->showMessage(tr("File saved"winking smiley, 2000);
}

void MyMainWindow::setCurrentFile(const QString &fileName) {
m_FileName = fileName;
m_ModLabel->clear();
m_TextEdit->document()->setModified(false);

if (m_FileName.isEmpty())
setWindowTitle(tr("Application"winking smiley);
else
setWindowTitle(tr("%1 - %2"winking smiley.arg(strippedName(m_FileName))
.arg(tr("Application"winking smiley));
}

QString MyMainWindow::strippedName(const QString &fullFileName) {
return QFileInfo(fullFileName).fileName();
}

QWidget* MyMainWindow::initialWidget() const {
return m_StackedWidget->widget(1);
}

MAIN PROGRAM

#include "mymainwindow.h"
#include <QApplication>

int main( int argc, char ** argv ) {
QApplication a( argc, argv );
MyMainWindow mw;
mw.show();
return a.exec();
}
Re: Example 11.1 (mainwindow)
September 10, 2007 07:07PM
Djbass,

I checked your code and it compiles and executes well.

Omilo
Sorry, only registered users may post in this forum.

Click here to login