• 多页面记事本


    ///////以下为部分代码

    int MainWindow::showQuestionDiaglog(QString text)

    {
        QMessageBox msg(this);
    
    
        msg.setWindowTitle("提示");
        msg.setText(text);
        msg.setIcon(QMessageBox::Question);
        msg.setStandardButtons(QMessageBox::Ok | QMessageBox::No | QMessageBox::Cancel);
    
    
        return msg.exec();
    }
    
    
    void MainWindow::showError(QString message)
    {
        QMessageBox msg(this);
    
    
        msg.setWindowTitle("Error");
        msg.setText(message);
        msg.setIcon(QMessageBox::Critical);
        msg.setStandardButtons(QMessageBox::Ok);
        msg.exec();
    }
    
    
    void MainWindow::addtable(QString name, int mode)
    {
        QPlainTextEdit* textEdit = new QPlainTextEdit(m_tableWidget);
    
    
        if( m_tableWidget != NULL && textEdit != NULL )
        {
            if( mode == 1)//新建文件
            {
                for( int i=0; i<=m_tableWidget->count(); i++)
                {
                    QString  str = m_tableWidget->tabText(i);
    
    
                    if(str.startsWith("new") && !str.endsWith(".txt"))
                    {
                        name = name+QString::number(i);
                        break;
                    }
                }
    
    
                if( name == "new")
                {
                    name = name+QString::number(m_tableWidget->count()-1);
                }
    
    
                textEdit->setFrameStyle(QFrame::NoFrame);
                m_tableWidget->addTab(textEdit,name);
            }
            else if( mode == 0 ) //打开文件
            {
                m_tableWidget->addTab(textEdit,name);
            }
        }
    }
    
    
    void MainWindow::closeTab(int index)
    {
        QMessageBox msg(this);
        msg.setWindowTitle("tips");
        msg.setText("Do you want to remove it?");
        msg.setStandardButtons(QMessageBox::Ok|QMessageBox::Cancel);
    
    
        if( (m_tableWidget->count() >1) && (msg.exec() == QMessageBox::Ok) )
        {
            m_tableWidget->removeTab(index);
        }
    }
    
    
    QPlainTextEdit* MainWindow::changeType(QWidget* w, int index)
    {
        return reinterpret_cast<QPlainTextEdit*>(w);
    }
    
    
    void MainWindow::onOpenFile()
    {
        QString path = showFileDialog(QFileDialog::AcceptOpen, "open file");
        QFile file(path);
    
    
        if(file.open(QIODevice::ReadOnly | QIODevice::Text))
        {
            QFileInfo info(file);
            addtable(info.fileName(),0);
            m_filePath.append(file.fileName());
            m_changeFlag.append(false);
    
    
            if( m_tableWidget != NULL )
            {
                QStackedWidget* sw = reinterpret_cast<QStackedWidget*>(m_tableWidget->children()[0]);
    
    
                if( sw != NULL )
                {
                    QWidget* widget = sw->widget(m_tableWidget->count()-1);
    
    
                    if( widget != 0)
                    {
                        QPlainTextEdit* te = reinterpret_cast<QPlainTextEdit*>(widget);
                        te->setPlainText(file.readAll());
                    }
                }
            }
    
    
            file.close();
        }
    }
    
    
    void MainWindow::onNewFile()
    {
        addtable();
        m_filePath.append("");
        m_changeFlag.append(false);
    }
    
    
    QString MainWindow::showFileDialog(QFileDialog::AcceptMode mode, QString title)
    {
        QString ret = "";
        QFileDialog flg(this);
        QStringList filters;
        QMap<QString, QString> map;
        const char* filterArray[][2] =
        {
            {"Text Files (*.txt)", ".txt"},
            {"All Files (*)",      "*"   },
            {NULL,                 NULL  }
        };
    
    
        for(int i=0; filterArray[i][0]!=NULL; i++)
        {
            filters.append(filterArray[i][0]);
            map.insert(filterArray[i][0], filterArray[i][1]);
        }
    
    
        flg.setWindowTitle(title);
        flg.setAcceptMode(mode);
        flg.setFilters(filters);
    
    
        if( mode == QFileDialog::AcceptOpen )
        {
            flg.setFileMode(QFileDialog::ExistingFile);
        }
    
    
        if( flg.exec() == QFileDialog::Accepted )
        {
            ret = flg.selectedFiles()[0];
    
    
            if( mode == QFileDialog::AcceptSave )
            {
                QString postfix = map[flg.selectedFilter()];
    
    
                if( (postfix != "*") && !ret.endsWith(postfix) )
                {
                    ret = ret + postfix;
                }
            }
        }
    
    
        return ret;
    }
    
    
    bool MainWindow::saveCurrentData(QString path)
    {
        bool ret = false;
    
    
        if( path == "" )
        {
            path = showFileDialog(QFileDialog::AcceptSave,"save file");
        }
    
    
        if(path != "")
        {
            QFile file(path);
    
    
            if( file.open(QIODevice::WriteOnly | QIODevice::Text) )
            {
                QTextStream out(&file);
    
    
                out << (reinterpret_cast<QPlainTextEdit*>(m_tableWidget->currentWidget()))->toPlainText();
                file.close();
                int index = m_tableWidget->currentIndex();
                m_filePath[index] = path;
                m_changeFlag[index] = false;
                ret = true;
            }
            else
            {
            }
        }
    
    
        return ret;
    }
    
    
    void MainWindow::onSaveFile()
    {
        QString name = m_tableWidget->tabText(m_tableWidget->currentIndex());
        int index = m_tableWidget->currentIndex();
        if( m_filePath[index].isEmpty() || m_changeFlag[index] )
        {
            saveCurrentData();
        }
    }
    
    
    void MainWindow::onShowPrintDailog()
    {
        QPrintDialog p;
        p.setWindowTitle("print");
    
    
        p.exec();
    }
    
    
    void MainWindow::closeEvent(QCloseEvent* e)//事件处理函数
    {
    
    
        QMainWindow::closeEvent(e);
    }
    
    
    void MainWindow::closeExit()
    {
        exit(0);
    }
  • 相关阅读:
    事务的手动创建和提交
    计数器AtomicInteger
    多线程方法执行等待
    【javascript动画系列之网页白板】javascript实现的白板(兼容ff,ie,chrome,……)
    ie6,7下js动态加载图片不显示错误
    php中遇到include_path='.;C:\php5\pear'的错误
    linux下php扩展curl的安装
    【转】Linux操作系统文件系统基础知识详解
    【javascript动画之圆形运动】环绕鼠标运动作小球(兼容ie,ff,chrome,……)
    as(ActionScript)拖动实现
  • 原文地址:https://www.cnblogs.com/xiaowulang/p/10533561.html
Copyright © 2020-2023  润新知