您的位置:首页 > 编程语言 > Qt开发

Qt利用代码实现流水灯的效果

2015-04-16 22:53 134 查看
用代码实现流水灯的效果
其实很想实现这种流水灯的效果了,看起来挺酷的,用处也很多,只是开始没有思路不知道怎么去实现,于是在我的超市收银项目中就采用了图片加载的方式进行显示,效果如下图所示:



由于是动态图片,显示的时候就要用到QMovie进行加载,简单的代码如下所示:

QMovie *move = new QMovie(":/images/splash.gif");
    QLabel *label = new QLabel("",0);
    label->setWindowIcon(QIcon(":/images/main.png"));
    label->setAttribute(Qt::WA_TranslucentBackground,true);//窗体背景透明
    label->setGeometry(510,300,450,100);
    label->setMovie(move);
    move->start();
    label->setWindowFlags(Qt::FramelessWindowHint);
    label->show();


利用动态图片工具***自己想要的图片,在需要进行加载的时候进行显示即可。这个办法很简单,但是很麻烦,不通用,如果需要显示的字符不一样,就得***另一张图片了,费时费力。果断放弃。

另一种方式是直接用代码实现,简单快捷、很通用。效果如下图所示:





说明此方法是参考别人的。博客地址:http://blog.sina.com.cn/s/blog_a6fb6cc90102vcdn.html

基本思想是每隔500毫秒显示字符串的变化,时间可以用定时器进行设置,代码如下所示:

设置窗体透明

this->setWindowFlags(Qt::FramelessWindowHint);
    this->setAttribute(Qt::WA_TranslucentBackground);//窗体背景透明


添加label用于显示文字并对文字设置简单的效果:

this->m_scrollCaptionLabel = new QLabel(this);
    this->m_scrollCaptionLabel->setToolTip("模拟流水灯效果");
    this->m_scrollCaptionLabel->setFont(QFont("Times",30,QFont::Bold));
    this->m_scrollCaptionLabel->setStyleSheet("color: blue;");
实现部分:

m_scrollCaptionStr = QString("欢迎加入我们:技术在于分享、交流 936563422");
    QTimer *m_timer = new QTimer(this);
    QObject::connect(m_timer,SIGNAL(timeout()),this,SLOT(slot_scrollCaption()));
    QObject::connect(this->m_quitButton,SIGNAL(clicked()),this,SLOT(close()));
    m_timer->start(500);
槽函数实现部分:

void Widget::slot_scrollCaption()
  {
      static int nPos = 0;

      if (nPos > m_scrollCaptionStr.length())
      {
          nPos = 0;
      }

      this->m_scrollCaptionLabel->setText(this->m_scrollCaptionStr.mid(nPos));
      ++nPos;
  }


效果看起来挺不错的,以后可以用在某些特定的场合,多谢“一去二三里”了,在此感谢你……



技术在于分享、交流……

Email: creazylq@163.com

QQ:936563422
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: