Qt-interest Archive, April 2007
run error
Message 1 in thread
hi, all
i made a mainwindow by referring the qt examples and compiled it
successfully. and i can't find any error in it. but when i try to run it ,
something happens and i can't run it.
the codes are available in accessories.
have any ideas?
thank you
regards
fengli
<ui version="4.0" >
<ui version="4.0" >
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow" >
<property name="geometry" >
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="font" >
<font>
<family>Arial</family>
<pointsize>10</pointsize>
</font>
</property>
<property name="windowTitle" >
<string>G-Code Editor</string>
</property>
<property name="windowIcon" >
<iconset/>
</property>
<widget class="QWidget" name="centralwidget" >
<widget class="QTextEdit" name="textEdit" >
<property name="geometry" >
<rect>
<x>140</x>
<y>120</y>
<width>104</width>
<height>64</height>
</rect>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menubar" >
<property name="geometry" >
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>23</height>
</rect>
</property>
<property name="font" >
<font>
<family>Arial</family>
</font>
</property>
<widget class="QMenu" name="editMenu" >
<property name="font" >
<font>
<family>Arial</family>
<pointsize>10</pointsize>
</font>
</property>
<property name="title" >
<string>&Edit</string>
</property>
<addaction name="cutAction" />
<addaction name="copyAction" />
<addaction name="pasteAction" />
<addaction name="separator" />
<addaction name="selectAllAction" />
</widget>
<widget class="QMenu" name="fileMenu" >
<property name="font" >
<font>
<family>Arial</family>
<pointsize>10</pointsize>
</font>
</property>
<property name="title" >
<string>&File</string>
</property>
<addaction name="newAction" />
<addaction name="openAction" />
<addaction name="saveAction" />
<addaction name="saveAsAction" />
<addaction name="separator" />
<addaction name="exitAction" />
</widget>
<widget class="QMenu" name="helpMenu" >
<property name="font" >
<font>
<family>Arial</family>
<pointsize>10</pointsize>
</font>
</property>
<property name="windowTitle" >
<string/>
</property>
<property name="windowIcon" >
<iconset/>
</property>
<property name="title" >
<string>&Help</string>
</property>
<addaction name="aboutAction" />
</widget>
<addaction name="fileMenu" />
<addaction name="editMenu" />
<addaction name="helpMenu" />
</widget>
<action name="newAction" >
<property name="icon" >
<iconset/>
</property>
<property name="text" >
<string>&New</string>
</property>
<property name="shortcut" >
<string>Ctrl+N</string>
</property>
</action>
<action name="openAction" >
<property name="icon" >
<iconset/>
</property>
<property name="text" >
<string>&Open...</string>
</property>
<property name="shortcut" >
<string>Ctrl+O</string>
</property>
</action>
<action name="saveAction" >
<property name="icon" >
<iconset/>
</property>
<property name="text" >
<string>&Save</string>
</property>
<property name="shortcut" >
<string>Ctrl+S</string>
</property>
</action>
<action name="saveAsAction" >
<property name="text" >
<string>Save &As...</string>
</property>
</action>
<action name="exitAction" >
<property name="text" >
<string>E&xit</string>
</property>
<property name="shortcut" >
<string>Ctrl+Q</string>
</property>
</action>
<action name="cutAction" >
<property name="icon" >
<iconset/>
</property>
<property name="text" >
<string>Cu&t</string>
</property>
<property name="shortcut" >
<string>Ctrl+X</string>
</property>
</action>
<action name="copyAction" >
<property name="icon" >
<iconset/>
</property>
<property name="text" >
<string>&Copy</string>
</property>
<property name="shortcut" >
<string>Ctrl+C</string>
</property>
</action>
<action name="pasteAction" >
<property name="icon" >
<iconset/>
</property>
<property name="text" >
<string>&Paste</string>
</property>
<property name="shortcut" >
<string>Ctrl+V</string>
</property>
</action>
<action name="selectAllAction" >
<property name="text" >
<string>Select &All</string>
</property>
<property name="shortcut" >
<string>Ctrl+A</string>
</property>
</action>
<action name="aboutAction" >
<property name="text" >
<string>&About</string>
</property>
</action>
</widget>
<resources>
<include location="editorresource.qrc" />
</resources>
<connections/>
</ui>
#ifndef EDITOR_H
#ifndef EDITOR_H
#define EDITOR_H
#include "ui_editor.h"
#include <QtGui>
class Editor:public QMainWindow, public Ui:: MainWindow
{
Q_OBJECT
public:
Editor();
protected:
void closeEvent(QCloseEvent *event);
private slots:
void newFile();
void open();
bool save();
bool saveAs();
void about();
void documentWasModified();
private:
void createContextMenu();
void readSettings();
void writeSettings();
bool maybeSave();
void loadFile(const QString &fileName);
bool saveFile(const QString &fileName);
void setCurrentFile(const QString &fileName);
QString strippedName(const QString &fullFileName);
QString curFile;
};
#endif#include "editor.h"
Editor::Editor()
{
createContextMenu();
readSettings();
connect(textEdit->document(), SIGNAL(contentsChanged()),
this, SLOT(documentWasModified()));
connect(newAction, SIGNAL(triggered()), this, SLOT(newFile()));
connect(openAction, SIGNAL(triggered()), this, SLOT(open()));
connect(saveAction, SIGNAL(triggered()), this, SLOT(save()));
connect(saveAsAction, SIGNAL(triggered()), this, SLOT(saveAs()));
connect(exitAction, SIGNAL(triggered()), this, SLOT(close()));
connect(cutAction, SIGNAL(triggered()), textEdit, SLOT(cut()));
connect(copyAction, SIGNAL(triggered()), textEdit, SLOT(copy()));
connect(pasteAction, SIGNAL(triggered()), textEdit, SLOT(paste()));
connect(selectAllAction, SIGNAL(triggered()), textEdit, SLOT(selectAll()));
connect(aboutAction, SIGNAL(triggered()), this, SLOT(about()));
cutAction->setEnabled(false);
copyAction->setEnabled(false);
connect(textEdit, SIGNAL(copyAvailable(bool)), cutAction, SLOT(setEnabled(bool)));
connect(textEdit, SIGNAL(copyAvailable(bool)), copyAction, SLOT(setEnabled(bool)));
setCurrentFile("");
}
void Editor::createContextMenu()
{
textEdit->addAction(cutAction);
textEdit->addAction(copyAction);
textEdit->addAction(pasteAction);
textEdit->setContextMenuPolicy(Qt::ActionsContextMenu);
}
void Editor::closeEvent(QCloseEvent *event)
{
if (maybeSave()) {
writeSettings();
event->accept();
} else {
event->ignore();
}
}
void Editor::newFile()
{
if (maybeSave()) {
textEdit->clear();
setCurrentFile("");
}
}
void Editor::open()
{
if (maybeSave()) {
QString fileName = QFileDialog::getOpenFileName(this);
if (!fileName.isEmpty())
loadFile(fileName);
}
}
bool Editor::save()
{
if (curFile.isEmpty()) {
return saveAs();
} else {
return saveFile(curFile);
}
}
bool Editor::saveAs()
{
QString fileName = QFileDialog::getSaveFileName(this);
if (fileName.isEmpty())
return false;
return saveFile(fileName);
}
void Editor::about()
{
QMessageBox::about(this, tr("About Editor"),
tr("The <b>Application</b> is an G-Code editor, "
"which is a component part of the NeopenCNC gui, "));
}
void Editor::documentWasModified()
{
setWindowModified(textEdit->document()->isModified());
}
void Editor::readSettings()
{
QSettings settings("NeopenCNC", "Application Example");
QPoint pos = settings.value("pos", QPoint(200, 200)).toPoint();
QSize size = settings.value("size", QSize(400, 300)).toSize();
resize(size);
move(pos);
}
void Editor::writeSettings()
{
QSettings settings("NeopenCNC", "Application Example");
settings.setValue("pos", pos());
settings.setValue("size", size());
}
bool Editor::maybeSave()
{
if (textEdit->document()->isModified()) {
QMessageBox::StandardButton ret;
ret = QMessageBox::warning(this, tr("Editor"),
tr("The document has been modified.\n"
"Do you want to save your changes?"),
QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
if (ret == QMessageBox::Save)
return save();
else if (ret == QMessageBox::Cancel)
return false;
}
return true;
}
void Editor::loadFile(const QString &fileName)
{
QFile file(fileName);
if (!file.open(QFile::ReadOnly | QFile::Text)) {
QMessageBox::warning(this, tr("Editor"),
tr("Cannot read file %1:\n%2.")
.arg(fileName)
.arg(file.errorString()));
return;
}
QTextStream in(&file);
QApplication::setOverrideCursor(Qt::WaitCursor);
textEdit->setPlainText(in.readAll());
QApplication::restoreOverrideCursor();
setCurrentFile(fileName);
statusBar()->showMessage(tr("File loaded"), 2000);
}
bool Editor::saveFile(const QString &fileName)
{
QFile file(fileName);
if (!file.open(QFile::WriteOnly | QFile::Text)) {
QMessageBox::warning(this, tr("Editor"),
tr("Cannot write file %1:\n%2.")
.arg(fileName)
.arg(file.errorString()));
return false;
}
QTextStream out(&file);
QApplication::setOverrideCursor(Qt::WaitCursor);
out << textEdit->toPlainText();
QApplication::restoreOverrideCursor();
setCurrentFile(fileName);
statusBar()->showMessage(tr("File saved"), 2000);
return true;
}
void Editor::setCurrentFile(const QString &fileName)
{
curFile = fileName;
textEdit->document()->setModified(false);
setWindowModified(false);
QString shownName;
if (curFile.isEmpty())
shownName = "untitled.ngc";
else
shownName = strippedName(curFile);
setWindowTitle(tr("%1[*] - %2").arg(shownName).arg(tr("Editor")));
}
QString Editor::strippedName(const QString &fullFileName)
{
return QFileInfo(fullFileName).fileName();
}
#include "editor.h"
#include "editor.h"
#include <QApplication>
int main(int argc, char **argv)
{
//Q_INIT_RESOURCE(application);
QApplication a(argc, argv);
Editor *editor = new Editor();
editor->show();
return a.exec();
}
Message 2 in thread
On 4/12/07, 张凤丽 <zhangfenglisdu@xxxxxxxxx> wrote:
> but when i try to run it ,
> something happens and i can't run it.
What, exactly, happens?
--
[ signature omitted ]
Message 3 in thread
On 4/12/07, 张凤丽 <zhangfenglisdu@xxxxxxxxx> wrote:
> hi, all
> i made a mainwindow by referring the qt examples and compiled it
> successfully. and i can't find any error in it. but when i try to run it ,
> something happens and i can't run it.
> the codes are available in accessories.
> have any ideas?
You're missing two things:
- Instantiating your base class
- Calling Ui::MainWindow::setupUi() to instantiate the UI widgets
Your c'tor should look like this:
Editor::Editor():
QMainWindow()
{
setupUi(this);
/* everything else */
}
--
[ signature omitted ]