时间:2021-07-01 10:21:17 帮助过:9人阅读
2、实现文件
- #ifndef BUILDINDIALOG_H
- #define BUILDINDIALOG_H
- #include
- class buildInDialog : public QDialog
- {
- Q_OBJECT
- public:
- buildInDialog();
- private:
- QPushButton *fileBtn;
- QPushButton *colorBtn;
- QPushButton *fontBtn;
- QPushButton *saveBtn;
- QPushButton *closeBtn;
- QTextEdit *textEdit;
- private slots:
- void fileSlot();
- void colorSlot();
- void fontSlot();
- void saveSlot();
- void closeSlot();
- };
- #endif
输出流
- #include "buildInDialog.h"
- buildInDialog::buildInDialog()
- {
- fileBtn = new QPushButton("open");
- colorBtn = new QPushButton("color");
- fontBtn = new QPushButton("font");
- saveBtn = new QPushButton("save");
- closeBtn = new QPushButton("close");
- textEdit = new QTextEdit();
- //布局
- QVBoxLayout *vLay = new QVBoxLayout();
- QHBoxLayout *hLay = new QHBoxLayout();
- vLay->addWidget(fileBtn);
- vLay->addWidget(colorBtn);
- vLay->addWidget(fontBtn);
- vLay->addWidget(saveBtn);
- vLay->addWidget(closeBtn);
- hLay->addWidget(textEdit);
- hLay->addLayout(vLay);
- setLayout(hLay);
- connect(fileBtn, SIGNAL(clicked()), this, SLOT(fileSlot()));
- connect(colorBtn, SIGNAL(clicked()), this, SLOT(colorSlot()));
- connect(fontBtn, SIGNAL(clicked()), this, SLOT(fontSlot()));
- connect(saveBtn, SIGNAL(clicked()), this, SLOT(saveSlot()));
- connect(closeBtn, SIGNAL(clicked()), this, SLOT(closeSlot()));
- }
- void buildInDialog::fileSlot()
- {
- //获取文件名字
- QString str = QFileDialog::getOpenFileName(this, "打开文件", "/", "All File(*.*)");
- //打开文件
- QFile file(str);
- if(!file.open(QIODevice::ReadWrite))
- return;
- //得到输入流
- QTextStream in(&file);
- //读取数据
- while(!in.atEnd())
- {
- QString st = in.readLine();
- textEdit->append(st);
- }
- }
- void buildInDialog::colorSlot()
- {
- //获取条色板
- QPalette palette = textEdit->palette();
- //打开对话框,获取颜色
- QColor color = QColorDialog::getColor(palette.color(QPalette::Text), this);
- if(color.isValid())
- {
- //将颜色放到条色板
- palette.setColor(QPalette::Window, color);
- //加载调色板
- textEdit->setPalette(palette);
- }
- }
- void buildInDialog::fontSlot()
- {
- bool ok;
- QFont font = QFontDialog::getFont(&ok);
- if(ok)
- textEdit->setFont(font);
- }
- void buildInDialog::saveSlot()
- {
- bool ok;
- //获取输入的信息
- QString str = QInputDialog::getText(this, "输入对话框", "请输入名字", QLineEdit::Normal, "wj", &ok);
- //根据输入的名字打开文件
- QFile file(str);
- file.open(QIODevice::WriteOnly);
- //获取
- #include "buildInDialog.h"
- #include
- int main(int argc, char *argv[])
- {
- //设置编码,防止汉字出现乱码
- QTextCodec::setCodecForCStrings(QTextCodec::codecForName("utf-8"));
- QApplication app(argc, argv);
- buildInDialog dialog;
- dialog.show();
- return app.exec();
- }
http://www.bkjia.com/PHPjc/1068089.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1068089.htmlTechArticlelesson3-Qt对话框 一、QDialog类 1、对话框的概念 对话框在各种软件中都会使用到,一般用来给用户提示信息或者接收用户反馈的信息,因此对...