C++编辑器-1
2026-02-15 14:21:13
发布于:浙江
该代码并不完整,点此进入代码框架页面
class WorkerSignals(QObject):
"""工作线程的信号类"""
started = pyqtSignal() # 线程开始
finished = pyqtSignal() # 线程完成
error = pyqtSignal(str) # 错误信号
output = pyqtSignal(str, str) # 输出信号 (文本, 类型:'normal'/'error'/'success'/'command')
compile_finished = pyqtSignal(bool, str) # 编译完成信号 (成功/失败, 消息)
run_started = pyqtSignal(str) # 运行开始信号 (程序路径)
class CompileWorker(QRunnable):
"""编译工作线程"""
def __init__(self, compiler_path, temp_cpp, output_file, include_paths, temp_dir):
super().__init__()
self.compiler_path = compiler_path
self.temp_cpp = temp_cpp
self.output_file = output_file
self.include_paths = include_paths
self.temp_dir = temp_dir
self.signals = WorkerSignals()
self.is_running = True
@pyqtSlot()
def run(self):
"""运行编译任务"""
try:
self.signals.started.emit()
self.signals.output.emit(f"开始编译...\n临时目录: {self.temp_dir}\n", 'normal')
# 检查编译器类型
compiler_name = os.path.basename(self.compiler_path).lower()
# 构建编译命令
compile_cmd = []
if 'cl' in compiler_name and sys.platform == 'win32':
# MSVC编译器
compile_cmd = [self.compiler_path, self.temp_cpp, f'/Fe"{self.output_file}"', '/EHsc', '/nologo', '/utf-8']
# 添加include路径
for path in self.include_paths:
compile_cmd.append(f'/I"{path}"')
elif compiler_name in ['g++', 'gcc', 'clang++'] or 'g++' in compiler_name:
# g++、gcc、clang++等编译器
compile_cmd = [self.compiler_path, self.temp_cpp, '-o', self.output_file, '-std=c++20', '-finput-charset=UTF-8', '-fexec-charset=UTF-8']
# 添加include路径
for path in self.include_paths:
compile_cmd.append(f'-I{path}')
else:
# 默认使用g++风格
compile_cmd = [self.compiler_path, self.temp_cpp, '-o', self.output_file, '-std=c++20', '-finput-charset=UTF-8', '-fexec-charset=UTF-8']
# 添加include路径
for path in self.include_paths:
compile_cmd.append(f'-I{path}')
# 显示编译命令和INCLUDE路径
cmd_str = ' '.join(compile_cmd)
self.signals.output.emit(f"编译命令: {cmd_str}\n", 'command')
self.signals.output.emit(f"使用 {len(self.include_paths)} 个include路径:\n", 'normal')
# 显示INCLUDE路径详情
for i, path in enumerate(self.include_paths):
self.signals.output.emit(f" [{i+1}] {path}\n", 'normal')
self.signals.output.emit("正在编译,请稍候...\n", 'normal')
# 使用subprocess.Popen实时获取输出,隐藏控制台窗口
if sys.platform == 'win32':
# Windows:使用CREATE_NO_WINDOW标志隐藏控制台窗口
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
startupinfo.wShowWindow = subprocess.SW_HIDE
process = subprocess.Popen(
compile_cmd,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True,
encoding='utf-8',
errors='replace',
startupinfo=startupinfo
)
else:
# Linux/Mac:正常启动
process = subprocess.Popen(
compile_cmd,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True,
encoding='utf-8',
errors='replace'
)
# 实时读取输出
while self.is_running:
# 读取标准输出
stdout_line = process.stdout.readline()
if stdout_line:
self.signals.output.emit(stdout_line, 'normal')
# 读取标准错误
stderr_line = process.stderr.readline()
if stderr_line:
self.signals.output.emit(stderr_line, 'error')
# 检查进程是否结束
if process.poll() is not None:
# 读取剩余输出
stdout, stderr = process.communicate()
if stdout:
self.signals.output.emit(stdout, 'normal')
if stderr:
self.signals.output.emit(stderr, 'error')
break
# 获取返回码
return_code = process.returncode
if return_code == 0:
success_msg = "编译成功!\n"
self.signals.output.emit(success_msg, 'success')
self.signals.compile_finished.emit(True, success_msg)
else:
error_msg = f"编译失败!返回码: {return_code}\n"
self.signals.output.emit(error_msg, 'error')
self.signals.compile_finished.emit(False, error_msg)
except Exception as e:
error_msg = f"编译错误: {str(e)}\n"
self.signals.output.emit(error_msg, 'error')
self.signals.compile_finished.emit(False, error_msg)
finally:
# 清理临时源文件
try:
if os.path.exists(self.temp_cpp):
os.unlink(self.temp_cpp)
except:
pass
self.signals.finished.emit()
def stop(self):
"""停止编译"""
self.is_running = False
这里空空如也





















有帮助,赞一个