void MyText::slot_open() { //選擇文件 QString path = QFileDialog::getOpenFileName(this,"選擇文件",":/","頭文件(*.h);;源文件(*.cpp);;所有文件(*.*)"); if(!path.isEmpty()) { //打開文件 QFile file(path); if(file.open(QIODevice::ReadOnly)) { QTextStream stream(&file); //創建字符流對象 QString str; while(!stream.atEnd()) //沒有讀到文件結尾 { //讀取文件內容到文本域 str = stream.readLine(128); this->textedit->append(str); //將字符串添加到文本域 } } file.close(); } }
void MyText::slot_save() { //獲取文本域的內容 QString content = this->textedit->toPlainText(); //獲取要保存的文件路徑 QString path = QFileDialog::getSaveFileName(this,"輸入文件名",":/","頭文件(*.h);;源文件(*.cpp)"); if(!path.isEmpty()) { //打開文件 QFile file(path); if(file.open(QIODevice::WriteOnly)) { //寫入內容 QTextStream stream(&file); stream << content; stream.flush(); } file.close(); } }
void MyText::slot_color() //字體顏色的選擇 { QColor color = QColorDialog::getColor(Qt::black,this,"選擇顏色"); if(color.isValid()) { QPalette pale = this->textedit->palette(); pale.setColor(QPalette::Text,color); this->textedit->setPalette(pale); } }