当前位置: 首页 > news >正文

西宁手机网站微站建设广东seo加盟

西宁手机网站微站建设,广东seo加盟,WordPress 3.5火车头发布接口,南通网站建设招聘介绍 本文将介绍如何使用Qt框架实现一个简单的交通信号灯控制程序。本程序包括一个图形界面,显示红、黄、绿三色信号灯,并通过定时器控制信号灯的切换。同时,我们还将实现一个带有按钮的界面,用于展示信号灯的状态。 1. 安装Qt开…

介绍

        本文将介绍如何使用Qt框架实现一个简单的交通信号灯控制程序。本程序包括一个图形界面,显示红、黄、绿三色信号灯,并通过定时器控制信号灯的切换。同时,我们还将实现一个带有按钮的界面,用于展示信号灯的状态。

1. 安装Qt开发环境

        如果你还没有安装Qt开发环境,请前往Qt官网下载并安装最新的Qt版本。安装过程中请确保选择了Qt Creator和相应的Qt版本(如Qt 5或Qt 6)。本文所示例子基于Qt 5.12版本实现。

2. 创建新项目

2.1 打开Qt Creator

启动Qt Creator开发环境。

2.2 创建新项目

  1. 在Qt Creator主界面,点击File -> New File or Project
  2. 在弹出的窗口中,选择Application -> Qt Widgets Application,然后点击Choose...按钮。
  3. 输入项目名称,例如trafficlightApp,选择项目保存路径,然后点击Next
  4. 选择合适的Qt版本和编译器配置,然后点击Next
  5. 确认类名设置,默认情况下,主窗口类名为MainWindow。你可以保留默认值或者修改,点击Next
  6. 点击Finish,Qt Creator会自动生成一个新的Qt Widgets项目。

3. 设置项目结构

3.1 添加必要文件

在项目创建完成后,你可以看到Qt Creator已经为你生成了基本的项目结构。接下来,我们需要手动添加一些文件。

  1. 在项目视图中右键点击项目名称(trafficlightApp),选择Add New...
  2. 选择C++ Class,然后点击Choose...
  3. 输入类名为TrafficLightWidget,基类选择QWidget,然后点击Next,再点击Finish
  4. 使用相同的方法,再添加一个类Widget,基类选择QWidget

我们的项目结构如下:

trafficlightApp/
├── images/
│   ├── green.png
│   ├── red.png
│   └── yellow.png
├── trafficlightApp.pro
├── main.cpp
├── trafficlightwidget.cpp
├── trafficlightwidget.h
├── widget.cpp
├── widget.h
└── widget.ui

4. 编写代码

4.1 修改trafficlightApp.pro文件

        编辑项目文件trafficlightApp.pro,添加资源文件信息,若为windows系统上QT Creator创建的项目trafficlightApp.pro不要修改,会自动配置;在Ubuntu系统上需要手动在文件中添加源文件和头文件等。

QT       += core guigreaterThan(QT_MAJOR_VERSION, 4): QT += widgetsCONFIG += c++11# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0SOURCES += \main.cpp \trafficlightwidget.cpp \widget.cppHEADERS += \trafficlightwidget.h \widget.hFORMS += \widget.ui# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += targetDISTFILES += \images/green.png \images/red.png \images/yellow.png

4.2 编写main.cpp

#include "trafficlightwidget.h"
#include <QApplication>int main(int argc, char *argv[])
{QApplication a(argc, argv);TrafficLightWidget w;w.show();return a.exec();
}

4.3 编写trafficlightwidget.h

#ifndef TRAFFICLIGHTWIDGET_H
#define TRAFFICLIGHTWIDGET_H#include <QWidget>
#include <QTimer>class TrafficLightWidget : public QWidget
{Q_OBJECTpublic:explicit TrafficLightWidget(QWidget *parent = nullptr);protected:void paintEvent(QPaintEvent *event) override;private slots:void updateLight();private:QTimer *timer;int lightIndex; // 0 for red, 1 for yellow, 2 for greenint redDuration; // Duration for red light in msint yellowDuration; // Duration for yellow light in msint greenDuration; // Duration for green light in msint elapsedTime; // Time elapsed since last change in msint remainingTime; // Remaining time for current light in secondsint getLightDuration(int light) const; // Function to get the duration for the given light
};#endif // TRAFFICLIGHTWIDGET_H

4.4 编写trafficlightwidget.cpp

#include "trafficlightwidget.h"
#include <QPainter>TrafficLightWidget::TrafficLightWidget(QWidget *parent): QWidget(parent),lightIndex(0), // Start with red lightredDuration(30000), // Red duration 30 secondsyellowDuration(3000), // Yellow duration 3 secondsgreenDuration(30000), // Green duration 30 secondselapsedTime(0), // Reset elapsed timeremainingTime(redDuration / 1000) // Start with red light's duration
{setFixedSize(200, 660); // Set the size of the traffic light widget to include countdown// Set up the timer to tick every second (1000 milliseconds)timer = new QTimer(this);connect(timer, &QTimer::timeout, this, &TrafficLightWidget::updateLight);timer->start(1000); // Start the timer
}void TrafficLightWidget::paintEvent(QPaintEvent *)
{QPainter painter(this);QColor lightRed(255, 0, 0, 255);QColor lightYellow(255, 255, 0, 255);QColor lightGreen(0, 255, 0, 255);QColor lightOff(50, 50, 50, 255);// Draw the traffic light framepainter.setBrush(Qt::black);painter.drawRect(10, 10, 180, 580);// Draw the red lightpainter.setBrush(lightIndex == 0 ? lightRed : lightOff);painter.drawEllipse(40, 30, 120, 120);// Draw the yellow lightpainter.setBrush(lightIndex == 1 ? lightYellow : lightOff);painter.drawEllipse(40, 180, 120, 120);// Draw the green lightpainter.setBrush(lightIndex == 2 ? lightGreen : lightOff);painter.drawEllipse(40, 330, 120, 120);// Draw the countdown timerpainter.setPen(Qt::white);QFont font = painter.font();font.setPointSize(24);painter.setFont(font);QString remainingTimeString = QString::number(remainingTime);painter.drawText(QRect(40, 530, 120, 50), Qt::AlignCenter, remainingTimeString);
}void TrafficLightWidget::updateLight()
{// Update remaining time for the current lightremainingTime = (getLightDuration(lightIndex) - elapsedTime) / 1000;elapsedTime += 1000; // Add 1 second to elapsed time// Check if the current light duration has passedif (elapsedTime >= getLightDuration(lightIndex)) {lightIndex = (lightIndex + 1) % 3; // Cycle through the lightselapsedTime = 0; // Reset elapsed timeremainingTime = getLightDuration(lightIndex) / 1000; // Reset remaining time}update(); // Request a repaint
}int TrafficLightWidget::getLightDuration(int light) const
{switch (light) {case 0: return redDuration; // Red light durationcase 1: return yellowDuration; // Yellow light durationcase 2: return greenDuration; // Green light durationdefault: return 0; // Default case (should not be reached)}
}

4.5 编写widget.h

#ifndef WIDGET_H
#define WIDGET_H#include <QWidget>QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACEclass Widget : public QWidget
{Q_OBJECTpublic:Widget(QWidget *parent = nullptr);~Widget();private slots:void on_toolButton_clicked();void on_pushButton_clicked();void on_pushButton_2_clicked();private:Ui::Widget *ui;
};#endif // WIDGET_H

4.6 编写widget.cpp

#include "widget.h"
#include "ui_widget.h"
#include <QGraphicsEffect>
#include<QMessageBox>Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget)
{ui->setupUi(this);// Set up button stylesui->pushButton->setStyleSheet("border-radius: 50%; background-color: #ff0000;");ui->pushButton_2->setStyleSheet("border-radius: 50%; background-color: rgb(85, 255, 0);");
}Widget::~Widget()
{delete ui;
}void Widget::on_toolButton_clicked()
{// Placeholder for tool button click event
}void Widget::on_pushButton_clicked()
{QMessageBox::information(this,"提示:","红灯,请等待!");
}void Widget::on_pushButton_2_clicked()
{QMessageBox::information(this,"提示:","绿灯,请通行!");
}

4.7 编辑widget.ui

在Qt Designer中打开widget.ui文件:

<?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>800</width><height>600</height></rect></property><property name="windowTitle"><string>红绿灯模拟程序</string></property><widget class="QCheckBox" name="checkBox"><property name="geometry"><rect><x>10</x><y>10</y><width>81</width><height>31</height></rect></property><property name="text"><string>CheckBox</string></property></widget><widget class="QToolButton" name="toolButton"><property name="geometry"><rect><x>710</x><y>0</y><width>71</width><height>31</height></rect></property><property name="styleSheet"><string notr="true">border-color: rgb(0, 255, 255);
border-left-color: qradialgradient(spread:pad, cx:0.5, cy:0.5, radius:0.5, fx:0.5, fy:0.5, stop:0 rgba(0, 0, 0, 0), stop:0.52 rgba(0, 0, 0, 0), stop:0.565 rgba(82, 121, 76, 33), stop:0.65 rgba(159, 235, 148, 64), stop:0.721925 rgba(255, 238, 150, 129), stop:0.77 rgba(255, 128, 128, 204), stop:0.89 rgba(191, 128, 255, 64), stop:1 rgba(0, 0, 0, 0));</string></property><property name="text"><string>Click Me</string></property></widget><widget class="QPushButton" name="pushButton"><property name="geometry"><rect><x>30</x><y>100</y><width>100</width><height>100</height></rect></property><property name="styleSheet"><string notr="true">background-color:white;
border-radius:5px;
border:1px solid  black;
color:#587fba;</string></property><property name="text"><string>MyButton</string></property><property name="flat"><bool>false</bool></property></widget><widget class="QPushButton" name="pushButton_2"><property name="geometry"><rect><x>330</x><y>100</y><width>100</width><height>100</height></rect></property><property name="styleSheet"><string notr="true">background-color:rgb(85, 255, 0);
border-radius:5px;
border:1px solid  black;
color:#587fba;</string></property><property name="text"><string>MyButton</string></property><property name="flat"><bool>false</bool></property></widget></widget><resources/><connections/>
</ui>

配置这些控件的属性,如位置、文本内容和样式表等,可以参考前面提供的widget.ui文件内容。

5. 构建和运行项目

点击Qt Creator中的Build按钮来构建项目。如果项目配置正确且代码无误,构建过程应能成功完成。

        点击Run按钮,运行程序。你将看到一个包含交通信号灯和两个按钮的窗口。红色、黄色、绿色信号灯会根据设定的时长自动切换。

运行结果展示:

4ec0e1f533f94a1b8a5cd0bbae78a090.png            091ad3c21227481f9e565a357e1cc5f2.png               cce42fb233b140eda43fc7030053a743.png

通过上述详细步骤,你已经完成了一个基于Qt的简单交通信号灯控制程序的实现。希望这些说明对你有所帮助。

 

http://www.hkea.cn/news/503862/

相关文章:

  • 上市公司专利查询网站百度广告投放价格
  • html5电商网页制作网站怎么seo关键词排名优化推广
  • 大同网站建设黄冈网站推广优化找哪家
  • 昌邑网站建设站长之家网站排名
  • 建设企业网站的需求分析免费域名
  • 重庆欧勒精细有限公司网站策划书百度竞价推广开户
  • 怎么做一键添加信任网站ios aso优化工具
  • ps做网站的分辨率多少钱苹果cms永久免费建站程序
  • 网站推广积分常用于网站推广的营销手段是
  • wordpress时间云储存沈阳网站制作优化推广
  • h5响应式网站建设竞价托管哪家效果好
  • 企业解决方案参考网站品牌软文营销案例
  • 做淘客要有好的网站上海百度seo
  • 网站建设 seojsc宁德seo推广
  • 建立网站的作用信息流优化师工作总结
  • 如何建设物流网站近期时事新闻
  • 网站开发大赛发言稿网址搜索
  • 论坛类型的网站怎么做拉新推广平台有哪些
  • pc官方网站视频专用客户端app
  • 成都哪家做网站建设比较好搜索关键词排名查询
  • 无锡网站优化推广广州网站推广运营
  • 电子商务网站开发的步骤短视频seo排名系统
  • 如何用模板做网站视频河北电子商务seo
  • 动态网站代码设计做小程序的公司
  • 网站建设软件开发的新闻北京关键词优化报价
  • 在上海做兼职在哪个网站好百度售后电话人工服务
  • 深圳网站开发招聘谁能给我个网址
  • 长沙做个网站多少钱怎样免费给自己的公司做网站
  • wordpress to微博优化营商环境条例
  • 做外贸通常用哪些网站seo网站监测