加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
mainwindow.cpp 8.43 KB
一键复制 编辑 原始数据 按行查看 历史
muziliuri 提交于 2020-10-11 19:20 . 第一次提交
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMenuBar>
#include <QToolBar>
#include <QPushButton>
#include <QStatusBar>
#include <QLabel>
#include <QDockWidget>
#include <QTextEdit>
#include <QDebug>
#include <QFileDialog>
#include <QFile>
#include <QInputDialog>
#include <QToolBar>
#include <QProcess>
#include <QMessageBox>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->setWindowTitle("Python Editor");
QToolBar * toolBar = new QToolBar;
addToolBar(Qt::TopToolBarArea,toolBar);
toolBar->setAllowedAreas(Qt::TopToolBarArea);
toolBar->setMovable(false);
QAction * openAction = toolBar->addAction("打开");
toolBar->addSeparator();
QAction * saveAction = toolBar->addAction("保存");
toolBar->addSeparator();
QAction * runAction = toolBar->addAction("运行");
toolBar->addSeparator();
QAction * interpreterAction = toolBar->addAction("设置");
openAction->setShortcut(QKeySequence(QString("Ctrl+o")));
saveAction->setShortcut(QKeySequence(QString("Ctrl+s")));
runAction->setShortcut(QKeySequence(QString("Ctrl+r")));
interpreterAction->setShortcut(QKeySequence(QString("Ctrl+i")));
openAction->setIcon(QIcon(":/image/open.png"));
saveAction->setIcon(QIcon(":/image/save.png"));
runAction->setIcon(QIcon(":/image/run.png"));
interpreterAction->setIcon(QIcon(":/image/interpreter.png"));
this->setWindowIcon(QIcon(":/image/editor.png"));
this->initEditor();
//创建状态栏
QStatusBar * stBar = statusBar();
//添加到窗口中
setStatusBar(stBar);
//右侧添加控件
QLabel * filePathLabel = new QLabel(this->filePath,this);
stBar->addPermanentWidget(filePathLabel);
connect(openAction,&QAction::triggered,this,[=](){
QString filePath = QFileDialog::getOpenFileName(this,"打开一个文件","../","(*.py)");
if(filePath == "" || !filePath.endsWith(".py"))
{
return;
}
this->filePath = filePath;
filePathLabel->setText(this->filePath);
QFile file(this->filePath);
file.open(QIODevice::ReadOnly);
QByteArray content = file.readAll();
this->editor->setText(content);
file.close();
});
connect(saveAction,&QAction::triggered,this,[=](){
QString filePath;
if(this->filePath == "not saved")
{
QString dirPath = QFileDialog::getExistingDirectory(this,"选择文件夹");
if(dirPath == "")
{
return;
}
bool ok;
QString filePath;
QString fileName = QInputDialog::getText(this, "请输入文件名",
"文件名:", QLineEdit::Normal,0, &ok);
if(ok && !fileName.isEmpty())
{
if(fileName.endsWith(".py") && fileName.length() == fileName.simplified().length())
{
filePath = dirPath + "/" + fileName;
this->filePath = filePath;
filePathLabel->setText(this->filePath);
}
}
}
else{
filePath = this->filePath;
}
QFile file(this->filePath);
file.open(QIODevice::WriteOnly);
QString content = this->editor->text();
file.write(content.toUtf8());
file.close();
});
connect(runAction,&QAction::triggered,this,[=](){
QProcess p(0);
if(this->filePath == "not saved")
{
return;
}
p.start("cmd", QStringList()<<"/c"<<this->interpreterPath+" "+this->filePath);
p.waitForStarted();
p.waitForFinished();
QString strTemp=QString::fromLocal8Bit(p.readAllStandardOutput());
QDialog * consoleInfo = new QDialog(this);
consoleInfo->resize(400,700);
QTextEdit * info = new QTextEdit(consoleInfo);
consoleInfo->setWindowTitle("运行结果");
info->setText(strTemp);
info->setReadOnly(true);
info->resize(400,700);
consoleInfo->show();
});
connect(interpreterAction,&QAction::triggered,this,[=](){
QDialog *interpreterInfo = new QDialog(this);
interpreterInfo->setModal(true);
interpreterInfo->setWindowTitle("Python Interpreter Settings");
interpreterInfo->resize(380,170);
QLabel *info = new QLabel(interpreterInfo);
info->setText("当前解释器:");
info->setGeometry(30,15,200,20);
QLineEdit *currentInterpreter = new QLineEdit(interpreterInfo);
if(this->interpreterPath == "python"){
currentInterpreter->setText("默认解释器");
}else{
currentInterpreter->setText(this->interpreterPath);
}
currentInterpreter->setReadOnly(true);
currentInterpreter->setGeometry(30,50,320,20);
QPushButton * changeInterpreter = new QPushButton(interpreterInfo);
changeInterpreter->setText("更改");
changeInterpreter->setGeometry(45,100,100,40);
QPushButton * confirmInterpreter = new QPushButton(interpreterInfo);
confirmInterpreter->setText("确认");
confirmInterpreter->setGeometry(230,100,100,40);
connect(changeInterpreter,&QPushButton::clicked,this,[=](){
QString interpreterPath = QFileDialog::getOpenFileName(this,"打开一个文件","../","(python.exe)");
if(interpreterPath == ""){
return;
}
if(interpreterPath.endsWith("python.exe")){
currentInterpreter->setText(interpreterPath);
}
});
connect(confirmInterpreter,&QPushButton::clicked,this,[=](){
this->interpreterPath = currentInterpreter->text();
interpreterInfo->close();
});
interpreterInfo->show();
});
setCentralWidget(this->editor);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::addPythonKeywords(QsciAPIs *apis)
{
apis->add(QString("and"));
apis->add(QString("as"));
apis->add(QString("assert"));
apis->add(QString("break"));
apis->add(QString("class"));
apis->add(QString("continue"));
apis->add(QString("def"));
apis->add(QString("del"));
apis->add(QString("enumerate"));
apis->add(QString("elif"));
apis->add(QString("else"));
apis->add(QString("except"));
apis->add(QString("finally"));
apis->add(QString("for"));
apis->add(QString("from"));
apis->add(QString("False"));
apis->add(QString("global"));
apis->add(QString("if"));
apis->add(QString("import"));
apis->add(QString("in"));
apis->add(QString("is"));
apis->add(QString("lambda"));
apis->add(QString("nonlocal"));
apis->add(QString("not"));
apis->add(QString("None"));
apis->add(QString("or"));
apis->add(QString("pass"));
apis->add(QString("raise"));
apis->add(QString("return"));
apis->add(QString("try"));
apis->add(QString("True"));
apis->add(QString("while"));
apis->add(QString("with"));
apis->add(QString("yield"));
apis->add(QString("__name__"));
apis->add(QString("__main__"));
apis->add(QString("__init__"));
apis->add(QString("__call__"));
apis->add(QString("print"));
apis->add(QString("range"));
apis->prepare();
}
void MainWindow::initEditor()
{
this->filePath = "not saved";
this->interpreterPath = "python";
this->editor = new QsciScintilla(this);
this->editor->SendScintilla(QsciScintilla::SCI_SETCODEPAGE,QsciScintilla::SC_CP_UTF8);
this->editor->setWrapMode(QsciScintilla::WrapWord);
this->editor->setMarginType(0,QsciScintilla::NumberMargin);//设置编号为0的页边显示行号
this->editor->setMarginLineNumbers(0,true);//对该页边启用行号
this->editor->setMarginWidth(0,35);//设置页边宽度
//this->editor->setAutoIndent(true);
this->editor->setCaretLineVisible(true);
this->editor->setCaretLineBackgroundColor(Qt::lightGray);
this->editor->setFolding(QsciScintilla::BoxedTreeFoldStyle);//折叠栏
QsciLexerPython *textLexer = new QsciLexerPython;
textLexer->setDefaultPaper(QColor(200,250,200));
this->editor->setLexer(textLexer);
QsciAPIs *apis=new QsciAPIs(textLexer);
addPythonKeywords(apis);
this->editor->setAutoCompletionCaseSensitivity(true);
this->editor->setAutoCompletionSource(QsciScintilla::AcsAll);
this->editor->setAutoCompletionThreshold(1);
this->editor->setBraceMatching(QsciScintilla::SloppyBraceMatch);
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化