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

亚马逊站外推广网站网络营销工程师是做什么的

亚马逊站外推广网站,网络营销工程师是做什么的,e网站的图标怎么做,无锡做网站公司哪家好介绍 本文将介绍如何使用Qt框架实现一个简单的交通信号灯控制程序。本程序包括一个图形界面#xff0c;显示红、黄、绿三色信号灯#xff0c;并通过定时器控制信号灯的切换。同时#xff0c;我们还将实现一个带有按钮的界面#xff0c;用于展示信号灯的状态。 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 创建新项目 在Qt Creator主界面点击File - New File or Project。在弹出的窗口中选择Application - Qt Widgets Application然后点击Choose...按钮。输入项目名称例如trafficlightApp选择项目保存路径然后点击Next。选择合适的Qt版本和编译器配置然后点击Next。确认类名设置默认情况下主窗口类名为MainWindow。你可以保留默认值或者修改点击Next。点击FinishQt Creator会自动生成一个新的Qt Widgets项目。 3. 设置项目结构 3.1 添加必要文件 在项目创建完成后你可以看到Qt Creator已经为你生成了基本的项目结构。接下来我们需要手动添加一些文件。 在项目视图中右键点击项目名称trafficlightApp选择Add New...。选择C Class然后点击Choose...。输入类名为TrafficLightWidget基类选择QWidget然后点击Next再点击Finish。使用相同的方法再添加一个类Widget基类选择QWidget。 我们的项目结构如下 trafficlightApp/ ├── images/ │ ├── green.png │ ├── red.png │ └── yellow.png ├── trafficlightApp.pro ├── main.cpp ├── trafficlightwidget.cpp ├── trafficlightwidget.h ├── widget.cpp ├── widget.h └── widget.ui4. 编写代码 4.1 修改trafficlightApp.pro文件 编辑项目文件trafficlightApp.pro添加资源文件信息若为windows系统上QT Creator创建的项目trafficlightApp.pro不要修改会自动配置在Ubuntu系统上需要手动在文件中添加源文件和头文件等。 QT core guigreaterThan(QT_MAJOR_VERSION, 4): QT widgetsCONFIG c11# 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_BEFORE0x060000 # 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.png4.2 编写main.cpp #include trafficlightwidget.h #include QApplicationint 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 QTimerclass 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_H4.4 编写trafficlightwidget.cpp #include trafficlightwidget.h #include QPainterTrafficLightWidget::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 lights 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 QWidgetQT_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_H4.6 编写widget.cpp #include widget.h #include ui_widget.h #include QGraphicsEffect #includeQMessageBoxWidget::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 version1.0 encodingUTF-8? ui version4.0classWidget/classwidget classQWidget nameWidgetproperty namegeometryrectx0/xy0/ywidth800/widthheight600/height/rect/propertyproperty namewindowTitlestring红绿灯模拟程序/string/propertywidget classQCheckBox namecheckBoxproperty namegeometryrectx10/xy10/ywidth81/widthheight31/height/rect/propertyproperty nametextstringCheckBox/string/property/widgetwidget classQToolButton nametoolButtonproperty namegeometryrectx710/xy0/ywidth71/widthheight31/height/rect/propertyproperty namestyleSheetstring notrtrueborder-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/propertyproperty nametextstringClick Me/string/property/widgetwidget classQPushButton namepushButtonproperty namegeometryrectx30/xy100/ywidth100/widthheight100/height/rect/propertyproperty namestyleSheetstring notrtruebackground-color:white; border-radius:5px; border:1px solid black; color:#587fba;/string/propertyproperty nametextstringMyButton/string/propertyproperty nameflatboolfalse/bool/property/widgetwidget classQPushButton namepushButton_2property namegeometryrectx330/xy100/ywidth100/widthheight100/height/rect/propertyproperty namestyleSheetstring notrtruebackground-color:rgb(85, 255, 0); border-radius:5px; border:1px solid black; color:#587fba;/string/propertyproperty nametextstringMyButton/string/propertyproperty nameflatboolfalse/bool/property/widget/widgetresources/connections/ /ui配置这些控件的属性如位置、文本内容和样式表等可以参考前面提供的widget.ui文件内容。 5. 构建和运行项目 点击Qt Creator中的Build按钮来构建项目。如果项目配置正确且代码无误构建过程应能成功完成。 点击Run按钮运行程序。你将看到一个包含交通信号灯和两个按钮的窗口。红色、黄色、绿色信号灯会根据设定的时长自动切换。 运行结果展示 通过上述详细步骤你已经完成了一个基于Qt的简单交通信号灯控制程序的实现。希望这些说明对你有所帮助。
http://www.hkea.cn/news/14453035/

相关文章:

  • 网站建设伍首选金手指wordpress添加跳转页面
  • 全国购物网站排名品牌网站建设咨询
  • 龙华建设网站腾讯企业邮箱入口网页版
  • 南通物流网站建设三明建设网站
  • wordpress多站点好用吗wordpress编辑器没了
  • 网站中高端建设wordpress后台总是404
  • mm131网站用什么软件做的英文网站建设60
  • 北京网站建设seo优化企业建设网站的方式有两种
  • 无锡网站建设团队h5海报是怎么做出来的
  • 网站入口首页wordpress qq头像
  • 网站建设教学运动鞋子网站建设规划书
  • 新乡市延津县建设局网站动态图片怎么制作
  • 重庆做石材的网站网店运营推广实训
  • 网站源码超市网站可以用什么语言开发做
  • 建网站 多少钱钱全屋定制家具设计师培训
  • 做冷冻食品的网站网站报价详情
  • 做网站的时候怎么照片路径深圳公司电话
  • 做一个产品网站要多少钱自己做网站好难挣钱
  • 网站建设佰首选金手指二八网站建设ktv
  • 沈阳网站设计公司有哪些教师进修学校网站建设方案
  • 网站外链应该怎么做做网站开发的应选什么专业
  • 拖式网站建设一个合格的网站设计
  • 天河建设网站企业天津市网站建站制作
  • 做盗版电影网站页面简单的网站
  • wordpress 编辑器漏洞seo短视频网页入口引流网站推荐
  • 网站建设云南免费的com域名注册
  • 广东省建设职业注册中心网站代理记账公司注册需要什么条件
  • wordpress侧边小图标联系方式网站文章优化事项
  • 音乐网站素材平面设计师要学哪些软件
  • 网站建设一年多少恰学历提升官网报名咨询