国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

文件操作

2019-11-08 02:26:34
字體:
來源:轉載
供稿:網友

一、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"));
}


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 商丘市| 宜兴市| 太谷县| 通化县| 镇江市| 甘肃省| 苏尼特左旗| 遵义市| 和硕县| 文化| 天柱县| 策勒县| 中阳县| 桑植县| 长垣县| 通州市| 手机| 贵溪市| 涿州市| 崇阳县| 调兵山市| 阿荣旗| 灵石县| 萍乡市| 田林县| 麟游县| 温泉县| 裕民县| 江都市| 交口县| 班玛县| 资中县| 枣阳市| 宜昌市| 财经| 郧西县| 确山县| 东兰县| 谷城县| 苍溪县| 大余县|