QT创建Windows下AI对话界面
本文最后更新于 34 天前,其中的信息可能已经有所发展或是发生改变。

创建的Cmake工程

信号与槽机制

我认为这个类似于单片机的事件中断

声明信号函数和槽函数

连接信号到槽

槽函数编写

调用Python程序

    QProcess process;
    process.setProgram("E:\\Anaconda\\envs\\py310\\python.exe");
    QStringList arguments;
    arguments << "C:\\Users\\1\\Desktop\\AI_CHAT\\chat\\main.py" << buffer;

    process.setArguments(arguments);
    process.start();
    //读取输出
    QString output = QString::fromLocal8Bit(process.readAllStandardOutput().data());

拖动界面

 ui->setupUi(this);
    setWindowFlags(windowFlags() | Qt::FramelessWindowHint);  // 设置无边框窗口
    this->setAttribute(Qt::WA_TranslucentBackground, true);  // 设置背景透明

 CustomTextEdit *customTextEdit = new CustomTextEdit(this);
    customTextEdit->setReadOnly(true);
    customTextEdit->setStyleSheet(ui->text1->styleSheet());

    // 获取父布局并移除原来的 QTextEdit
    QLayoutItem *item = ui->verticalLayout->takeAt(0); // 获取第一个布局项(text1)
    delete item->widget(); // 删除原来的 QTextEdit
    delete item; // 删除布局项

    // 将 CustomTextEdit 添加到布局中
    ui->verticalLayout->insertWidget(0,customTextEdit);

    ui->text1 = customTextEdit; // 现在 ui->text1 是 CustomTextEdit 类型


    // 连接信号到槽
    connect(ui->text1, SIGNAL(moveWindow(QMouseEvent*)), this, SLOT(onText1MoveWindow(QMouseEvent*)));

    connect(&process, &QProcess::readyReadStandardOutput, this, &MainWindow::onProcessRead);



void MainWindow::onText1MoveWindow(QMouseEvent *event)
{
    // 窗口移动逻辑
    if (event->type() == QEvent::MouseButtonPress) {
        if (event->button() == Qt::LeftButton) {
            mouse_press = true;
            mousePoint = event->globalPos() - this->pos();
        }
    } else if (event->type() == QEvent::MouseMove) {
        if (mouse_press) {
            move(event->globalPos() - mousePoint);
        }
    } else if (event->type() == QEvent::MouseButtonRelease) {
        mouse_press = false;
    }
}


void MainWindow::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton) {
        mouse_press = true;
        mousePoint = event->globalPos() - this->pos();  // 记录鼠标按下时的偏移量
    } else if (event->button() == Qt::RightButton) {
        this->close();  // 右键关闭窗口
    }
}

void MainWindow::mouseMoveEvent(QMouseEvent *event)
{
    if (mouse_press) {
        move(event->globalPos() - mousePoint);  // 根据鼠标移动调整窗口位置
    }
}

void MainWindow::mouseReleaseEvent(QMouseEvent *event)
{
    mouse_press = false;  // 释放鼠标时取消拖动状态
}

改控件CSS

    QString edit1_style ="background-color: rgba(0, 0, 0, 0.3);";
    edit1_style += "color: rgb(255, 255, 255);";
    edit1_style += "border: 1px solid #000000;";
    edit1_style += "border-radius: 5px;";

    ui->edit1->setStyleSheet(edit1_style);

    ui->button1->setStyleSheet("QPushButton {"
                               "background-color: rgba(0, 0, 0, 0.3);"
                               "color: rgb(255, 255, 255);"
                               "border: 1px solid #000000;"
                               "border-radius: 5px;"
                               "padding: 10px 20px;"
                               "font-size: 16px;"
                               "transition: background-color 0.3s, color 0.3s, border-color 0.3s;"
                               "}"
                               "QPushButton:hover {"
                               "color: #FFFFFF;"
                               "background-color: #718093;"
                               "border-color: #2f3640;"
                               "}"
                               "QPushButton:pressed {"
                               "color: #000;"
                               "background-color: #5c6bc0;"
                               "border-color: #2f3640;"
                               "}");

QTextEdit显示文本内容

    QString output = QString::fromLocal8Bit(process.readAllStandardOutput().data());
    qDebug() <<output;
    //
    ui->text1->setAlignment(Qt::AlignLeft);
    ui->text1->insertPlainText("  A : \n    "+output+"\n\n");

    ui->text1->moveCursor(QTextCursor::End);

暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇