文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-560924.html
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QFont>
#include <QFontDialog>
#include <QMessageBox>
#include <QColor>
#include <QColorDialog>
#include <QFileDialog>
#include <QDebug>
#include <QFile>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void on_colorbtn_clicked();
void on_openbtn_clicked();
void on_savebtn_clicked();
void on_txtbtn_clicked();
private:
Ui::MainWindow *ui;
QString filename;
};
#endif // MAINWINDOW_H
?文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-560924.html
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_colorbtn_clicked()
{
QColor c = QColorDialog::getColor(QColor(0,255,0),this,"選擇顏色");
if(c.isValid()){
ui->textEdit->setTextColor(c);
}else {
QMessageBox::information(this,"錯(cuò)誤","沒(méi)有選擇顏色");
}
}
void MainWindow::on_openbtn_clicked()
{
filename=QFileDialog::getOpenFileName(this,"打開(kāi)文件","./","text file(*.txt)");
qDebug()<<filename;
QFile file(filename);
if(!file.exists()) {
QMessageBox::information(this,"錯(cuò)誤","文件不存在");
}
if(!file.open(QIODevice::ReadWrite)){
QMessageBox::information(this,"錯(cuò)誤","文件打開(kāi)失敗");
return;
}
QByteArray msg=file.readAll();
file.close();
ui->textEdit->setText(QString::fromUtf8(msg));
}
void MainWindow::on_savebtn_clicked()
{
QString pathName = QFileDialog::getSaveFileName(this, "保存文件", "./", "TEXT(*.txt)");
//實(shí)例化文件對(duì)象
QFile file(pathName);
//打開(kāi)文件
file.open(QIODevice::WriteOnly | QIODevice::Truncate);
//寫(xiě)入數(shù)據(jù)
QString text = ui->textEdit->toPlainText();
file.write(text.toLocal8Bit());
//關(guān)閉文件
file.close();
}
void MainWindow::on_txtbtn_clicked()
{
bool ok;
QFont f = QFontDialog::getFont(&ok,QFont("宋體",10,2,false),this,"選擇字體");
if(ok){
ui->textEdit->setCurrentFont(f);
}
else{
QMessageBox::information(this,"錯(cuò)誤","沒(méi)有選擇字體");
}
}
到了這里,關(guān)于QT創(chuàng)建文本編輯窗口的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!