C++编辑器-7.2
2026-02-15 14:38:44
发布于:浙江
该代码并不完整,点此进入代码框架页面
def compile_code(self):
"""编译代码(使用后台线程)"""
if not self.compiler_path:
QMessageBox.critical(self, "错误", "未找到C++编译器,无法编译代码。")
return
if not self.editor:
QMessageBox.critical(self, "错误", "编辑器未初始化,无法编译代码。")
return
# 获取代码
code = self.editor.toPlainText()
# 确定源文件和输出文件路径
if self.current_file and os.path.exists(self.current_file):
# 如果文件已保存,使用原文件路径
cpp_file = self.current_file
output_dir = os.path.dirname(cpp_file)
file_name_without_ext = os.path.splitext(os.path.basename(cpp_file))[0]
# 创建临时副本用于编译(使用我们的临时目录)
temp_cpp = os.path.join(self.temp_dir, f"{file_name_without_ext}_temp_{os.getpid()}_{int(time.time())}.cpp")
with open(temp_cpp, 'w', encoding='utf-8') as f:
f.write(code)
# 输出文件路径
if sys.platform == 'win32':
output_file = os.path.join(output_dir, f"{file_name_without_ext}.exe")
else:
output_file = os.path.join(output_dir, file_name_without_ext)
use_saved_file = True
else:
# 文件未保存,使用临时文件(使用我们的临时目录)
timestamp = int(time.time())
temp_cpp = os.path.join(self.temp_dir, f"unsaved_temp_{os.getpid()}_{timestamp}.cpp")
with open(temp_cpp, 'w', encoding='utf-8') as f:
f.write(code)
# 获取临时文件的基本名称(用于显示)
temp_basename = os.path.basename(temp_cpp)
file_name_without_ext = os.path.splitext(temp_basename)[0]
output_dir = self.temp_dir
# 输出文件路径
if sys.platform == 'win32':
output_file = os.path.join(self.temp_dir, f"{file_name_without_ext}.exe")
else:
output_file = os.path.join(self.temp_dir, file_name_without_ext)
use_saved_file = False
# 清理输出区域
if self.output:
self.output.clear()
self.output_highlighter.clear_formats()
# 创建编译工作线程
self.current_worker = CompileWorker(
self.compiler_path,
temp_cpp,
output_file,
self.include_paths,
self.temp_dir
)
# 连接信号
self.current_worker.signals.started.connect(self.on_compile_started)
self.current_worker.signals.finished.connect(self.on_compile_finished)
self.current_worker.signals.output.connect(self.on_compile_output)
self.current_worker.signals.compile_finished.connect(self.on_compile_result)
# 开始编译
self.statusBar().showMessage("正在编译...")
self.thread_pool.start(self.current_worker)
def on_compile_started(self):
"""编译开始"""
self.statusBar().showMessage("编译开始...")
def on_compile_finished(self):
"""编译完成"""
self.statusBar().showMessage("编译完成")
self.current_worker = None
def on_compile_output(self, output, output_type):
"""编译输出"""
if self.output:
# 保存当前光标位置
cursor = self.output.textCursor()
cursor.movePosition(QTextCursor.End)
# 插入文本
cursor.insertText(output)
# 根据输出类型进行高亮
if output_type == 'error':
# 错误信息高亮为红色
self.output_highlighter.highlight_text(output, QColor("#FF0000"))
elif output_type == 'success':
# 成功信息高亮为绿色
self.output_highlighter.highlight_text(output, QColor("#007F00"))
elif output_type == 'command':
# 命令信息高亮为蓝色
self.output_highlighter.highlight_text(output, QColor("#0000FF"))
# 新增:输出框自动滚动到底部
self.ensure_output_scroll_to_bottom()
def on_compile_result(self, success, message):
"""编译结果"""
if success:
# 记录编译哈希值
if hasattr(self, 'current_worker') and self.current_worker:
current_code_hash = self.get_current_code_hash()
# 注意:这里需要获取实际的输出文件路径
# 为了简化,我们假设编译成功后会设置输出文件路径
# 在实际的CompileWorker中,我们需要将输出文件路径传递回来
pass
self.statusBar().showMessage("编译成功")
else:
self.statusBar().showMessage("编译失败")
def run_code(self):
"""运行代码(使用后台线程)"""
if not self.compiler_path:
QMessageBox.critical(self, "错误", "未找到C++编译器,无法运行代码。")
return
if not self.editor:
QMessageBox.critical(self, "错误", "编辑器未初始化,无法运行代码。")
return
# 获取代码
code = self.editor.toPlainText()
# 确定源文件和输出文件路径
if self.current_file and os.path.exists(self.current_file):
# 如果文件已保存,使用原文件路径
cpp_file = self.current_file
output_dir = os.path.dirname(cpp_file)
file_name_without_ext = os.path.splitext(os.path.basename(cpp_file))[0]
# 创建临时副本用于编译(使用我们的临时目录)
temp_cpp = os.path.join(self.temp_dir, f"{file_name_without_ext}_temp_{os.getpid()}_{int(time.time())}.cpp")
with open(temp_cpp, 'w', encoding='utf-8') as f:
f.write(code)
# 输出文件路径
if sys.platform == 'win32':
output_file = os.path.join(output_dir, f"{file_name_without_ext}.exe")
else:
output_file = os.path.join(output_dir, file_name_without_ext)
use_saved_file = True
else:
# 文件未保存,使用临时文件(使用我们的临时目录)
timestamp = int(time.time())
temp_cpp = os.path.join(self.temp_dir, f"unsaved_temp_{os.getpid()}_{timestamp}.cpp")
with open(temp_cpp, 'w', encoding='utf-8') as f:
f.write(code)
# 获取临时文件的基本名称(用于显示)
temp_basename = os.path.basename(temp_cpp)
file_name_without_ext = os.path.splitext(temp_basename)[0]
output_dir = self.temp_dir
# 输出文件路径
if sys.platform == 'win32':
output_file = os.path.join(self.temp_dir, f"{file_name_without_ext}.exe")
else:
output_file = os.path.join(self.temp_dir, file_name_without_ext)
use_saved_file = False
# 清理输出区域
if self.output:
self.output.clear()
self.output_highlighter.clear_formats()
# 获取运行编码
run_encoding = self.config_manager.get('run_encoding', 'utf-8')
# 创建运行工作线程
self.current_worker = RunWorker(
self.compiler_path,
temp_cpp,
output_file,
self.include_paths,
self.temp_dir,
run_encoding,
file_name_without_ext,
use_saved_file,
output_dir,
self # 传递self作为parent参数
)
# 连接信号
self.current_worker.signals.started.connect(self.on_run_started)
self.current_worker.signals.finished.connect(self.on_run_finished)
self.current_worker.signals.output.connect(self.on_run_output)
self.current_worker.signals.compile_finished.connect(self.on_compile_result)
self.current_worker.signals.run_started.connect(self.on_program_started)
# 开始运行
self.statusBar().showMessage("正在运行...")
self.thread_pool.start(self.current_worker)
def ensure_output_scroll_to_bottom(self):
"""确保输出框自动滚动到底部"""
if self.output:
# 获取滚动条并滚动到底部
scrollbar = self.output.verticalScrollBar()
scrollbar.setValue(scrollbar.maximum())
# 确保光标在末尾
cursor = self.output.textCursor()
cursor.movePosition(QTextCursor.End)
self.output.setTextCursor(cursor)
def on_run_started(self):
"""运行开始"""
self.statusBar().showMessage("运行开始...")
def on_run_finished(self):
"""运行完成"""
self.statusBar().showMessage("运行完成")
self.current_worker = None
def on_run_output(self, output, output_type):
"""运行输出"""
if self.output:
# 保存当前光标位置
cursor = self.output.textCursor()
cursor.movePosition(QTextCursor.End)
# 插入文本
cursor.insertText(output)
# 根据输出类型进行高亮
if output_type == 'error':
# 错误信息高亮为红色
self.output_highlighter.highlight_text(output, QColor("#FF0000"))
elif output_type == 'success':
# 成功信息高亮为绿色
self.output_highlighter.highlight_text(output, QColor("#007F00"))
elif output_type == 'command':
# 命令信息高亮为蓝色
self.output_highlighter.highlight_text(output, QColor("#0000FF"))
# 新增:输出框自动滚动到底部
self.ensure_output_scroll_to_bottom()
def on_program_started(self, program_path):
"""程序已启动"""
self.statusBar().showMessage(f"程序已启动: {os.path.basename(program_path)}")
def stop_execution(self):
"""停止执行"""
if self.current_worker:
self.current_worker.stop()
self.statusBar().showMessage("正在停止执行...")
if self.output:
self.output.appendPlainText("\n正在停止执行...\n")
self.output_highlighter.highlight_text("正在停止执行...", QColor("#FF0000"))
else:
self.statusBar().showMessage("没有正在执行的任务")
def about(self):
"""关于对话框"""
QMessageBox.about(
self, "关于 C++ IDLE",
"<h2>C++ IDLE</h2>"
"<p>版本 3.14.5</p>"
"<p>一个C++集成开发环境</p>"
"<p>新功能:</p>"
"<p>- 多线程编译和运行</p>"
"<p>- 空大括号缩进处理</p>"
"<p>- 可开关的自动补全功能</p>"
"<p>- 编译输出高亮(红色错误/绿色成功)</p>"
"<p>- 中文乱码解决方案</p>"
"<p>- 临时文件管理</p>"
"<p>加强功能:</p>"
"<p>- 自定义字体、字体大小</p>"
"<p>- 支持转义字符的语法高亮</p>"
"<p>- 增强的文件编码检测</p>"
"<p>- 自动保存字体配置及INCLUDE路径</p>"
"<p>- 自动缩进(Tab键插入4个空格)</p>"
"<p>- 括号自动补全与匹配</p>"
"<p>- 智能回车缩进</p>"
""
"<p>基于C/C++开发</p>"
"<p>© 2026 C++ IDLE 项目</p>"
)
def closeEvent(self, event):
"""关闭事件"""
if self.maybe_save():
# 停止所有工作线程
if self.current_worker:
self.current_worker.stop()
# 清理临时文件
self.cleanup_temp_files()
# 保存窗口状态
self.save_window_state()
event.accept()
else:
event.ignore()
这里空空如也





















有帮助,赞一个