Save und Save as

Alles rund um die Programmierung mit Qt
Antworten
aslmeh
Beiträge: 8
Registriert: 16. März 2011 16:28

Save und Save as

Beitrag 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
Christian81
Beiträge: 7319
Registriert: 26. August 2004 14:11
Wohnort: Bremen
Kontaktdaten:

Beitrag von Christian81 »

MfG Christian

'Funktioniert nicht' ist keine Fehlerbeschreibung
syni
Beiträge: 4
Registriert: 15. April 2011 14:06

Beitrag von syni »

Hier wird das mMn auch schoen verwendet: http://doc.trolltech.com/4.7/demos-textedit.html
Antworten