当前位置:网站首页>QT save qtextedit memory to Txt file

QT save qtextedit memory to Txt file

2022-06-22 04:51:00 Small soup-

01. Save button click slot function

// Avoid the file dialog box popping up twice 
//Qt::UniqueConnection Ensure that there is only one connection between the same signal and the same slot function , Avoid triggering the slot function twice after clicking the button 
connect(ui->buttonSave, SIGNAL(clicked()), this, SLOT(on_buttonSave_clicked()), Qt::UniqueConnection); 
void Widget::on_buttonSave_clicked()
{
    
    QFileDialog dlg(this);

    // Get the saving path of the content 
    QString fileName = dlg.getSaveFileName(this, tr("Save As"), "./", tr("Text File(*.txt)"));

    if( fileName == "" )
    {
    
        return;
    }

    // Save contents to path file 
    QFile file(fileName);

    // Open... As text 
    if( file.open(QIODevice::WriteOnly | QIODevice::Text) )
    {
    
        QTextStream out(&file); //IO Initialize the device object by its address 

        out << ui->textEditRead->toPlainText() << endl; // Output 

        QMessageBox::warning(this, tr("Finish"), tr("Successfully save the file!"));

        file.close();
    }
    else
    {
    
        QMessageBox::warning(this, tr("Error"), tr("File to open file!"));
    }
}

02. Running effect

 Insert picture description here

03. Reference resources

[ 1 ] Qt The opening of a text file 、 newly build 、 Save and save as
[ 2 ] Qt The reason for sending a signal to trigger the slot function twice

原网站

版权声明
本文为[Small soup-]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206220445076593.html