1.保存文件
void Widget::on_save_clicked()
{QString filename = QFileDialog::getSaveFileName(this,"选择文件","F:\\study\\huaqing\\save","Text File(*.txt)");if(filename.isNull()){QMessageBox::information(this,"提示","取消选择文件",QMessageBox::Ok);return;}else{//实例化一个文件对象QFile file(filename);//创建并打开文件,判断是否存在if(!file.open(QFile::ReadWrite| QFile::Text))return;//读取记事本中的内容QString msg = ui->textEdit->toPlainText();//写入内容file.write(msg.toUtf8());//关闭文件file.close();}
}
2.键盘事件
void Widget::keyPressEvent(QKeyEvent *event)
{switch (event->key()){case 'W':if(ui->label->y() <= 0-ui->label->height()){ui->label->move(ui->label->x(),this->height());}ui->label->move(ui->label->x(),ui->label->y()-5);break;case 'S':if(ui->label->y() >= this->height()){ui->label->move(ui->label->x(),0-ui->label->height());}ui->label->move(ui->label->x(),ui->label->y()+5);break;case 'A':if(ui->label->x() <= 0-ui->label->width()){ui->label->move(this->width(),ui->label->y());}ui->label->move(ui->label->x()-5,ui->label->y());break;case 'D':if(ui->label->x() >= this->width()){ui->label->move(0-ui->label->width(),ui->label->y());}ui->label->move(ui->label->x()+5,ui->label->y());break;}
}
思维导图: