#include <QApplication>
#include <QWidget>
#include <QPushButton>
#include <QGridLayout>
#include <QLabel>
class RemoteControl : public QWidget {
Q_OBJECT
public:
RemoteControl(QWidget *parent = nullptr) : QWidget(parent) {
// 设置窗口标题
setWindowTitle(tr("遥控车控制界面"));
// 创建按钮
QPushButton *forwardButton = new QPushButton(tr("前进"), this);
QPushButton *leftButton = new QPushButton(tr("左转"), this);
QPushButton *stopButton = new QPushButton(tr("停止"), this);
QPushButton *rightButton = new QPushButton(tr("右转"), this);
QPushButton *backwardButton = new QPushButton(tr("后退"), this);
// 设置按钮的固定大小
forwardButton->setFixedSize(100, 50);
leftButton->setFixedSize(100, 50);
stopButton->setFixedSize(100, 50);
rightButton->setFixedSize(100, 50);
backwardButton->setFixedSize(100, 50);
// 创建标签
QLabel *statusLabel = new QLabel(tr("状态:停止"), this);
// 创建网格布局
QGridLayout *gridLayout = new QGridLayout(this);
// 设置网格布局的行和列
gridLayout->setRowCount(3);
gridLayout->setColumnCount(3);
// 在网格布局中添加按钮和标签
gridLayout->addWidget(forwardButton, 0, 1);
gridLayout->addWidget(leftButton, 1, 0);
gridLayout->addWidget(stopButton, 1, 1);
gridLayout->addWidget(rightButton, 1, 2);
gridLayout->addWidget(backwardButton, 2, 1);
gridLayout->addWidget(statusLabel, 3, 0, 1, 3); // 跨足3行1列
// 连接按钮的点击事件和槽函数
connect(forwardButton, &QPushButton::clicked, this, &RemoteControl::onForwardClicked);
connect(leftButton, &QPushButton::clicked, this, &RemoteControl::onLeftClicked);
connect(stopButton, &QPushButton::clicked, this, &RemoteControl::onStopClicked);
connect(rightButton, &QPushButton::clicked, this, &RemoteControl::onRightClicked);
connect(backwardButton, &QPushButton::clicked, this, &RemoteControl::onBackwardClicked);
// 初始化遥控车状态
statusLabel->setText(tr("状态:停止"));
}
private slots:
void onForwardClicked() {
// 前进按钮点击事件处理函数
qDebug() << "前进";
// 执行遥控车前进的操作
// ...
}
void onLeftClicked() {
// 左转按钮点击事件处理函数
qDebug() << "左转";
// 执行遥控车左转的操作
// ...
}
void onStopClicked() {
// 停止按钮点击事件处理函数
qDebug() << "停止";
// 执行遥控车停止的操作
// ...
}
void onRightClicked() {
// 右转按钮点击事件处理函数
qDebug() << "右转";
// 执行遥控车右
以下是一个使用c++ Qt编写的简单的遥控车控制界面的示例源代码,详细注释
于 2023-04-14 16:46:05 首次发布