Qt(5.8.0)-运行框升级版

发布时间 2023-07-23 10:33:03作者: So_noGeirf

Qt编程实现简单的命令行窗口

本文介绍如何使用Qt框架编写一个简单的命令行窗口应用程序。通过该程序,用户可以在窗口中输入命令,并通过点击按钮执行命令。

首先,我们需要创建一个名为"Widget"的类,继承自QWidget,用于创建应用程序的主窗口。以下是widget.h头文件中的代码:

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QProcess>
#include <QMessageBox>

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = 0);
    ~Widget();

private slots:
    void on_commitButton_clicked();
    void on_cancelButton_clicked();

private:
    Ui::Widget *ui;
};

#endif // WIDGET_H

为了实现功能,我们在主窗口中放置了一些控件,如标签(QLabel)、文本输入框(QLineEdit)和按钮(QPushButton)等。参考以下是widget.ui文件中的XML代码:

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>Widget</class>
 <widget class="QWidget" name="Widget">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>400</width>
    <height>300</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Widget</string>
  </property>
  <widget class="QLineEdit" name="cmdLineEdit">
   <property name="geometry">
    <rect>
     <x>120</x>
     <y>120</y>
     <width>221</width>
     <height>41</height>
    </rect>
   </property>
  </widget>
  <widget class="QLabel" name="label">
   <property name="geometry">
    <rect>
     <x>130</x>
     <y>90</y>
     <width>181</width>
     <height>31</height>
    </rect>
   </property>
   <property name="font">
    <font>
     <family>宋体</family>
     <pointsize>12</pointsize>
    </font>
   </property>
   <property name="text">
    <string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p align=&quot;center&quot;&gt;&lt;span style=&quot; font-weight:600;&quot;&gt;请输入命令:&lt;/span&gt;&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
   </property>
  </widget>
  <widget class="QLabel" name="label_2">
   <property name="geometry">
    <rect>
     <x>60</x>
     <y>120</y>
     <width>51</width>
     <height>51</height>
    </rect>
   </property>
   <property name="font">
    <font>
     <family>宋体</family>
     <pointsize>9</pointsize>
    </font>
   </property>
   <property name="text">
    <string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p align=&quot;center&quot;&gt;&lt;span style=&quot; font-size:12pt; font-weight:600;&quot;&gt;打开:&lt;/span&gt;&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
   </property>
  </widget>
  <widget class="QWidget" name="layoutWidget">
   <property name="geometry">
    <rect>
     <x>100</x>
     <y>170</y>
     <width>250</width>
     <height>28</height>
    </rect>
   </property>
   <layout class="QHBoxLayout" name="horizontalLayout">
    <item>
     <widget class="QPushButton" name="commitButton">
      <property name="font">
       <font>
        <family>宋体</family>
        <pointsize>12</pointsize>
       </font>
      </property>
      <property name="text">
       <string>确定</string>
      </property>
     </widget>
    </item>
    <item>
     <widget class="QPushButton" name="cancelButton">
      <property name="font">
       <font>
        <family>宋体</family>
        <pointsize>12</pointsize>
       </font>
      </property>
      <property name="text">
       <string>取消</string>
      </property>
     </widget>
    </item>
    <item>
     <widget class="QPushButton" name="browseButton">
      <property name="font">
       <font>
        <family>宋体</family>
        <pointsize>12</pointsize>
       </font>
      </property>
      <property name="text">
       <string>浏览</string>
      </property>
     </widget>
    </item>
   </layout>
  </widget>
 </widget>
 <layoutdefault spacing="6" margin="11"/>
 <resources/>
 <connections/>
</ui>

widget.cpp文件中,我们实现了主窗口类的构造函数、析构函数和按钮点击事件处理函数等。以下是widget.cpp文件中的部分代码:

#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);

    // 连接信号与槽
    connect(ui->cmdLineEdit, SIGNAL(returnPressed()), this, SLOT(on_commitButton_clicked()));
    connect(ui->cancelButton, &QPushButton::clicked, this, &Widget::on_cancelButton_clicked);

    connect(ui->browseButton, &QPushButton::clicked, [this]() {
        QMessageBox::information(this, "信息", "点击浏览");
    });
}

void Widget::on_commitButton_clicked()
{
    QString program = ui->cmdLineEdit->text();

    QProcess *myProcess = new QProcess(this);
    myProcess->start(program);
}

void Widget::on_cancelButton_clicked()
{
    this->close();
}

在该代码中,我们通过信号与槽机制将按钮的点击事件(clicked信号)与对应的槽函数进行连接。当提交按钮(commitButton)被点击时,从命令输入框(cmdLineEdit)中获取命令,并使用QProcess类执行该命令。同时,当取消按钮(cancelButton)被点击时,窗口将关闭。

最后,在main.cpp文件中,我们创建了QApplication对象,并实例化Widget类,启动该应用程序的事件循环。以下是main.cpp文件中的代码:

#include "widget.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();

    return a.exec();
}

通过以上的代码实现,我们可以创建一个基于Qt框架的命令行窗口应用程序。通过文本输入框输入命令,并点击"确定"按钮进行执行,点击"取消"按钮关闭窗口。