初步可以运行命令

This commit is contained in:
2024-01-21 22:42:58 +08:00
parent 8eec84a689
commit 8090ac0f8d
6 changed files with 107 additions and 3 deletions

View File

@@ -9,10 +9,12 @@ CONFIG += c++17
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
installdeb.cpp \
main.cpp \
mainwindow.cpp
HEADERS += \
installdeb.h \
mainwindow.h
FORMS += \
@@ -27,3 +29,4 @@ CONFIG += embed_translations
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
unix:!macx: LIBS += -lqtermwidget5 # 这一行让我查了好久

44
installdeb.cpp Normal file
View File

@@ -0,0 +1,44 @@
#include "installdeb.h"
#include <QDateTime>
#include <QObject>
#include <QMessageBox>
#include <QMainWindow>
InstallDEB::InstallDEB(QTermWidget *terminal, QMainWindow *mainWindow)
{
this->terminal = terminal;
this->mainWindow = mainWindow;
}
void InstallDEB::AddCommand(QString command){
this->commandList.append(command);
}
void InstallDEB::RunCommand(){
this->terminal->setEnabled(true);
this->runStatus = true;
// 写入为 Bash 文件,方便执行
QString commandCode = "#!/bin/bash\n";
QString bashPath = "/tmp/deepin-wine-runner-aptss-installer-" + QString::number(QDateTime::currentMSecsSinceEpoch()) + ".sh";
for(int i = 0; i < this->commandList.size(); i++){
commandCode += this->commandList.at(i) + "\n";
}
commandCode += "rm -rfv '" + bashPath + "'";
QFile file(bashPath);
file.open(QFile::WriteOnly);
if(!file.isWritable()){
throw "Can't write the file!";
}
file.write(commandCode.toUtf8());
file.close();
system(("chmod +x '" + bashPath + "'").toUtf8()); // 赋予运行权限
this->terminal->setColorScheme("DarkPastels");
this->terminal->setShellProgram("/usr/bin/bash");
this->terminal->setArgs(QStringList() << bashPath);
//this->terminal->setAutoClose(1);
this->terminal->setAutoFillBackground(1);
this->terminal->startShellProgram();
QObject::connect(this->terminal, &QTermWidget::finished, this->mainWindow, [this](){
QMessageBox::information(this->mainWindow, "A", "B");
});
}

21
installdeb.h Normal file
View File

@@ -0,0 +1,21 @@
#ifndef INSTALLDEB_H
#define INSTALLDEB_H
#include <QObject>
#include <qtermwidget5/qtermwidget.h>
#include <QMainWindow>
class InstallDEB
{
public:
InstallDEB(QTermWidget *terminal, QMainWindow *mainWindow = NULL);
void AddCommand(QString command);
void RunCommand();
QStringList commandList;
private:
QTermWidget *terminal;
QMainWindow *mainWindow = NULL;
bool runStatus;
};
#endif // INSTALLDEB_H

View File

@@ -1,11 +1,15 @@
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "installdeb.h"
#include <qtermwidget5/qtermwidget.h>
#include <QMessageBox>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
@@ -13,3 +17,17 @@ MainWindow::~MainWindow()
delete ui;
}
void MainWindow::on_installPath_clicked()
{
QTermWidget *terminal = new QTermWidget(0);
terminal->setColorScheme("DarkPastels");
terminal->setShellProgram("/usr/bin/bash");
terminal->setArgs(QStringList() << "-c" << "gedit");
connect(terminal, &QTermWidget::finished, this, [&, this](){QMessageBox::information(NULL, "提示", "系统安装完成"); });
terminal->startShellProgram();
ui->gridLayout->addWidget(terminal, 1, 0);
}

View File

@@ -15,6 +15,9 @@ public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void on_installPath_clicked();
private:
Ui::MainWindow *ui;
};

View File

@@ -13,9 +13,24 @@
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget"/>
<widget class="QMenuBar" name="menubar"/>
<widget class="QStatusBar" name="statusbar"/>
<widget class="QWidget" name="centralwidget">
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QLineEdit" name="debPath"/>
</item>
<item>
<widget class="QPushButton" name="installPath">
<property name="text">
<string>安装</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
</widget>
<resources/>
<connections/>