Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2012972
  • 博文数量: 356
  • 博客积分: 8284
  • 博客等级: 中将
  • 技术积分: 4580
  • 用 户 组: 普通用户
  • 注册时间: 2009-05-15 20:25
个人简介

天行健,君子以自强不息

文章分类

全部博文(356)

文章存档

2018年(1)

2016年(4)

2015年(13)

2014年(14)

2013年(2)

2012年(25)

2011年(43)

2010年(65)

2009年(189)

分类: LINUX

2009-11-13 16:55:14

初学QT照着demo做一些例子
dialog.cpp

#include

#include "dialog.h"

#include "wigglywidget.h"

Dialog::Dialog(QWidget *parent)

: QDialog(parent)

{

WigglyWidget *wigglyWidget = new WigglyWidget;

QLineEdit *lineEdit = new QLineEdit;

QVBoxLayout *layout = new QVBoxLayout;

layout->addWidget(wigglyWidget);

layout->addWidget(lineEdit);

setLayout(layout);

connect(lineEdit, SIGNAL(textChanged(QString)),

wigglyWidget, SLOT(setText(QString)));

lineEdit->setText(tr("欢迎来到我的博客"));

setWindowTitle(tr("Wiggly"));

resize(360, 145);

}

dialog.h
#ifndef DIALOG_H
 #define DIALOG_H

 #include

 class Dialog : public QDialog
 {
     Q_OBJECT

 public:
     Dialog(QWidget *parent = 0);
 };

 #endif
wigglywidget.cpp
#include

 #include "wigglywidget.h"

 WigglyWidget::WigglyWidget(QWidget *parent)
     : QWidget(parent)
 {
     setBackgroundRole(QPalette::Midlight);
     setAutoFillBackground(true);

     QFont newFont = font();
     newFont.setPointSize(newFont.pointSize() + 20);
     setFont(newFont);

     step = 0;
     timer.start(60, this);
 }

 void WigglyWidget::paintEvent(QPaintEvent * /* event */)
 {
     static const int sineTable[16] = {
         0, 38, 71, 92, 100, 92, 71, 38, 0, -38, -71, -92, -100, -92, -71, -38
     };

     QFontMetrics metrics(font());
     int x = (width() - metrics.width(text)) / 2;
     int y = (height() + metrics.ascent() - metrics.descent()) / 2;
     QColor color;

     QPainter painter(this);
     for (int i = 0; i < text.size(); ++i) {
         int index = (step + i) % 16;
         color.setHsv((15 - index) * 16, 255, 191);
         painter.setPen(color);
         painter.drawText(x, y - ((sineTable[index] * metrics.height()) / 400),
                          QString(text[i]));
         x += metrics.width(text[i]);
     }
 }

 void WigglyWidget::timerEvent(QTimerEvent *event)
 {
     if (event->timerId() == timer.timerId()) {
         ++step;
         update();
     } else {
         QWidget::timerEvent(event);
     }
 }
wigglywidget.h
#ifndef WIGGLYWIDGET_H
 #define WIGGLYWIDGET_H

 #include
 #include

 class WigglyWidget : public QWidget
 {
     Q_OBJECT

 public:
     WigglyWidget(QWidget *parent = 0);

 public slots:
     void setText(const QString &newText) { text = newText; }

 protected:
     void paintEvent(QPaintEvent *event);
     void timerEvent(QTimerEvent *event);

 private:
     QBasicTimer timer;
     QString text;
     int step;
 };

 #endif
main.cpp
#include

 #include "dialog.h"

 int main(int argc, char *argv[])
 {
     QApplication app(argc, argv);

     Dialog dialog;
     dialog.show();
     return app.exec();
 }



阅读(2486) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~