Qt 5滚动字幕(左/右)+闪烁效果(QLabel控件显示)
来源:互联网 发布:java设计模式是什么 编辑:程序博客网 时间:2024/05/21 08:56
一、说明:
Qt版本为:Qt 5.9.1
二、简单解述:
1、字幕效果主要是应用QString QString::mid(int pos, int n = -1) const函数截取字符,另外需要一个定时器刷新(QTimer),具体信息可以参考Qt帮助文档,索引mid,过滤QString class。
2、闪烁效果很简单,用hide和show函数即可,使用一个定时器刷新。
3、效果展示:
三、上代码:
1、ui部分:
拖动一个label和button到界面中,设置好label的字体颜色和大小,字体颜色可以直接在ui中改变样式表。
2、头文件:
#ifndef MAINWINDOW_H#define MAINWINDOW_H#include <QMainWindow>#include <QTimer>namespace Ui {class MainWindow;}class MainWindow : public QMainWindow{ Q_OBJECTpublic: explicit MainWindow(QWidget *parent = 0); ~MainWindow();private slots: void scrollCaption(); void show_hide(); void on_twinkleBt_clicked();private: Ui::MainWindow *ui; QTimer *mtimer; QTimer *ntimer; QString showtext; int curIndex;};#endif // MAINWINDOW_H
3、.cpp代码:
#include "mainwindow.h"#include "ui_mainwindow.h"#include <QDebug>//#define RTMainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow){ ui->setupUi(this); ui->twinkleBt->setText("关闭闪烁");#ifndef RT curIndex = showtext.size(); //左移#endif#ifdef RT curIndex = 0; // 右移#endif showtext = "天气变冷,请同学们注意保暖!"; mtimer = new QTimer(this); connect(mtimer, SIGNAL(timeout()), this, SLOT(scrollCaption())); mtimer->start(1000); ntimer = new QTimer(this); connect(ntimer, SIGNAL(timeout()), this, SLOT(show_hide())); ntimer->start(250);}MainWindow::~MainWindow(){ delete ui;}void MainWindow::scrollCaption(){ int isize = showtext.size(); // 文字个数#ifndef RT // 左移 if (curIndex > isize) curIndex = 0; qDebug() << curIndex; ui->label->setText(showtext.mid(curIndex++)); // .mid(pos); 从pos开始截取字符串#endif#ifdef RT // 右移 if (curIndex < 0) curIndex = isize; ui->label->setText(showtext.mid(curIndex--));#endif}void MainWindow::show_hide(){ if (ui->label->isHidden() == true) ui->label->show(); else ui->label->hide();}// 停止/开启闪烁void MainWindow::on_twinkleBt_clicked(){ if (ntimer->isActive() == true) { disconnect(ntimer, SIGNAL(timeout()), this, SLOT(show_hide())); ntimer->stop(); if (ui->label->isHidden() == true) ui->label->show(); ui->twinkleBt->setText("开启闪烁"); } else { connect(ntimer, SIGNAL(timeout()), this, SLOT(show_hide())); ntimer->start(250); ui->twinkleBt->setText("关闭闪烁"); }}
工程打包下载:http://download.csdn.net/download/wu9797/10149734
阅读全文
0 0
- Qt 5滚动字幕(左/右)+闪烁效果(QLabel控件显示)
- Qt(QLabel)实现滚动字幕
- QT 实现在ui控件QLabel中实现滚动字幕
- Qt实现滚动字幕效果
- QT QLabel右跑马显示文字
- Qt QLabel左跑马显示文字
- openCV图片显示到Qt控件QLabel
- 走马灯 (滚动字幕效果)
- QT 实现字幕滚动(上下滚动)
- QLabel和QTimer之滚动字幕
- QLabel和QTimer之滚动字幕完善
- QT---字幕滚动效果,检测鼠标进入和离开控制字幕滚动
- 滚动字幕——左滚、右滚、上滚、下滚
- 【Android】滚动字幕(走马灯)效果
- JavaScript字幕滚动效果
- Qt之滚动字幕
- Qt 字幕滚动程序
- Qt实现滚动字幕
- mfc edit控件无法设置颜色。无法进入到OnCtlColor()函数
- 脱离微信开放平台绑定公众号个数问题
- zTree 的简单使用
- 双十二年终大促,MathType不要错过
- 函数第一讲:创建以及helloword模板
- Qt 5滚动字幕(左/右)+闪烁效果(QLabel控件显示)
- C++Primer第五版中的c++11特性归纳(四)
- Python3 去掉bs4爬取信息中的‘\t’'\r' '\r'
- Problem1:twoSum
- 音频参数解析
- SSL_1194 最优乘车
- Lock同步_小记
- 证券投资基金名词解释
- 2017年12月7号---我的第一篇博客