Seite 1 von 1

Save und Save as

Verfasst: 11. April 2011 20:53
von aslmeh
Hallo, ich programmiere gerade ein HTML Editor mit Qt und Visual Studio 2008. Ich muss ein Copy Cut Paste Undo Redo Save Save As New File und Exit realisieren. Habe bisher alles geschafft bis auf Save und Save As.

Ich will dass mein Programm es abcheckt, ob eine neue Datei geöffnet wurde oder eine vorhandene Datei damit er entscheidet ob Save oder Save As.

die header datei:

Code: Alles auswählen

class MainWindow : public QMainWindow, private Ui::mainWindow
{
	Q_OBJECT
	QLabel* m_WordsLabel;
	QLabel* m_LetterLabel;
	bool m_DocumentChanged;
	QString m_FileName;


public:
	MainWindow(QWidget* parent = NULL);
	~MainWindow();
protected:
	void setupActions();
	void setupStatusBar();
public slots:
	void newFileAction();
	void fileOpenAction();
	void undoAction();
	void redoAction();
	void copyAction();
	void cutAction();
	void pasteAction();
	void textChanged();
   void aboutWindow();
	bool saveFileAsAction();
};
hier die wichtigen slots:

Code: Alles auswählen

void MainWindow::fileOpenAction() 
{
	QString s = QFileDialog::getOpenFileName ( this, tr("Open File"), tr("HTML"), "html (*.html)");
	statusbar->showMessage ( tr("Load File: ") + s, 10000 );

	QFile file( s );

	if( !file.open( QIODevice::ReadOnly | QIODevice::Text) )
		return;

	textEdit->setPlainText( QString::fromUtf8( file.readAll() ) );
}

Code: Alles auswählen

void MainWindow::newFileAction()
{
	if( saveFileAsAction() )
	{
		textEdit->clear();
	}

	statusbar->showMessage ( tr("New File"), 10000 );
}

Code: Alles auswählen

bool MainWindow::saveFileAsAction()
{
	QTextDocument* document = textEdit->document();

	if( !document )
		return false;

	if( document->isEmpty() )
		return false;

	QString fileName = QFileDialog::getSaveFileName( this, tr("Save As.."), QDir::currentPath(), "html (*.html)" );

	if( fileName.isEmpty() )
		return false;
	
	statusbar->showMessage ( tr("Save File: ") + fileName, 10000 );

	QFile file( fileName );	
	
	if( !file.open( QIODevice::WriteOnly | QIODevice::Text ) )
		return false;

	file.write( textEdit->document()->toPlainText().toUtf8() );
		return true;

}
wie kann ich das abchecken ob neues file oder vorhandenes file??
danke

Verfasst: 11. April 2011 21:57
von Christian81

Verfasst: 20. April 2011 08:12
von syni
Hier wird das mMn auch schoen verwendet: http://doc.trolltech.com/4.7/demos-textedit.html