QT學習(三)檔案操作
一、MainWindow設定
1. 設定視窗內容
視窗包含選單欄、工具欄、主視窗內容和狀態列。
新建QTextEdit或者其他視窗內容(如 spreadsheet等),然後將之放置視窗中間。
textEdit=new QTextEdit(this);
setCentralWidget(textEdit);
setWindowTitle(tr("Text"));
setWindowIcon(QIcon(":/images/icon.png"));
在建立選單欄和工具欄之前需要建立操作選項,
newAction=new QAction(tr("new"), this);
newAction->setIcon(QIcon(":/images/new.png"));
newAction->setShortcut(QKeySequence::New);
newAction->setStatusTip("create a new file");
connect(newAction, SIGNAL(triggered()), this, SLOT(newFile()));
上述建立了一個new的操作,用於新建檔案,並且為其設定了圖示、快捷鍵以及操作狀態顯示。並且當其按下會呼叫newFile函式。
接下來建立選單欄和工具欄,
fileMenu=menuBar()->addMenu(tr("File"));
fileMenu->addAction(newAction);
menuBar為MainWindow類函式,其返回QMenuBar類指標,通過addMenu來建立一個Menu,通過addAction來新增操作選項。
fileToolBar=addToolBar(tr("file"));
fileToolBar->addAction(newAction);
上述加入ToolBar以及將newAction加入ToolBar中。
textLabel=new QLabel(tr("Status"));
textLabel->setAlignment(Qt::AlignHCenter);
textLabel->setMinimumSize(textLabel->sizeHint());
statusBar()->addWidget(textLabel);
上述為設定狀態列,可以在狀態列中加入Qwidget,例如QLabel,並且為其設定大小以及安排位置。
二、檔案操作
1. 判斷視窗是否更改
視窗更改主要為判斷視窗內容是否更改,即textEdit內容是否更改。
bool MainWindow::okToContinue(){
if(isWindowModified()){
int r=QMessageBox::warning(this, tr("Text"),
tr("the text has been modified.\n"
"Do you want to save it ?"),
QMessageBox::Yes|QMessageBox::No|QMessageBox::Cancel);
if(r==QMessageBox::Yes)
return save();
else if(r==QMessageBox::Cancel)
return false;
}
return true;
}
void MainWindow::documentModified(){
setWindowModified(textEdit->document()->isModified());
}
在documentModified函式中,利用textEdit呼叫isModified函式來判斷內容是否修改,並且將更改結果傳給主視窗。在okToContinue中通過isWindowModified來判斷主視窗是否修改。如果修改過,就會彈出訊息框,讓你進行選擇。
2. 檔案儲存和匯入
檔案讀取類似於C++中對檔案讀取,也是通過流的方式。
bool MainWindow::saveFile(const QString &fileName){
QFile file(fileName);
if(!file.open(QFile::WriteOnly)){
QMessageBox::warning(this, tr("Text"), tr("cannot open the file %1").arg(file.fileName()));
return false;
}
QTextStream out(&file);
QApplication::setOverrideCursor(Qt::WaitCursor);
out<<textEdit->toPlainText();
QApplication::restoreOverrideCursor();
setWindowModified(false);
setCurrentFile(fileName);
statusBar()->showMessage(tr("File saved"), 2000);
return true;
}
QT中有QFile用於開啟檔案,用QTextStream來講檔案轉換為資料流。上述中通過呼叫toPlainText函式獲得了textEdit中的內容,然後將之輸出到檔案中。
setOverrideCursor為對游標進行設定,在檔案到處過程中,游標是等待型別,完成後恢復原狀。
同時狀態列中顯示檔案儲存的狀態,顯示2s。
檔案的匯入也類似:
void MainWindow::loadFile(const QString &fileName){
QFile file(fileName);
if(!file.open(QFile::ReadOnly)){
QMessageBox::warning(this, tr("Text"), tr("cannot open the file %1").arg(file.fileName()));
return;
}
QTextStream in(&file);
QApplication::setOverrideCursor(Qt::WaitCursor);
textEdit->setPlainText(in.readAll());
QApplication::restoreOverrideCursor();
setWindowModified(false);
statusBar()->showMessage(tr("Loading file"), 2000);
}
3. 複製和剪下
雖然QTextEdit類中有copy和paste函式用於完成複製和剪下等操作,但是本文還是用獨立函式來完成。
void MainWindow::copy(){
QTextCursor cursor=textEdit->textCursor();
QString str=cursor.selectedText();
QApplication::clipboard()->setText(str);
}
Copy和剪下都是需要確定textEdit中選擇的內容,識別內容選擇是通過QTextCursor類實現的,其可以對選擇的文字進行操作。textCursor返回游標位置,selectedText函式返回選中內容,通過呼叫clipBoard來使用剪下板,可以將選中內容複製到剪下板,以便下一次使用。
void MainWindow::paste(){
QTextCursor cursor=textEdit->textCursor();
if(cursor.hasSelection())
cursor.clearSelection();
QString str=QApplication::clipboard()->text();
cursor.insertText(str);
}
呼叫剪下板來將之內容貼上到textEdit中。
4. 幫助選項
幫助選項直接調出資訊框即可。
void MainWindow::about(){
QMessageBox::about(this, tr(" About Text"), tr("This is my text"));
}