• 3314阅读
  • 3回复

[讨论]QT事件触发问题 [复制链接]

上一主题 下一主题
离线donjun
 

只看楼主 倒序阅读 楼主  发表于: 2016-03-04
发现我们这个论坛好像人流量不是很大啊,除了置顶帖点击量大些外其他帖子很少有人看,发帖的也不多
我在学习到本书第7章烤箱定时器旋钮对代码有些疑问,还请熟手门指点一二。我想我的主要问题是还没有从C的面相过程过度到QT的面相对象方式上。
如果我想在定时器走到0时,想要再触发另外一件事情,也就是调用另外一个函数应该怎么做
#ifndef OVENTIMER_H#define OVENTIMER_H
#include <QDateTime>#include <QWidget>
class QTimer;
class OvenTimer : public QWidget{    Q_OBJECT
public:    OvenTimer(QWidget *parent = 0);
    void setDuration(int secs);    int duration() const;    void draw(QPainter *painter);
signals:    void timeout(); //这个信号函数怎么只是定义了,没有看到实现过程,也不是QT函数啊。按F1没有找打呀
protected:    void paintEvent(QPaintEvent *event);    void mousePressEvent(QMouseEvent *event);
private:    QDateTime finishTime;    QTimer *updateTimer;    QTimer *finishTimer;};
#endif
#include <QtGui>#include <cmath>
#ifndef M_PI#define M_PI 3.14159265359#endif
#include "oventimer.h"
const double DegreesPerMinute = 7.0;const double DegreesPerSecond = DegreesPerMinute / 60;const int MaxMinutes = 45;const int MaxSeconds = MaxMinutes * 60;const int UpdateInterval = 5;
OvenTimer::OvenTimer(QWidget *parent)    : QWidget(parent){    finishTime = QDateTime::currentDateTime();
    updateTimer = new QTimer(this);    connect(updateTimer, SIGNAL(timeout()), this, SLOT(update()));
    finishTimer = new QTimer(this);    finishTimer->setSingleShot(true);    connect(finishTimer, SIGNAL(timeout()), this, SIGNAL(timeout()));    connect(finishTimer, SIGNAL(timeout()), updateTimer, SLOT(stop()));
    QFont font;    font.setPointSize(8);    setFont(font);}
void OvenTimer::setDuration(int secs){    secs = qBound(0, secs, MaxSeconds);//判断给定值是否在合理范围内
    finishTime = QDateTime::currentDateTime().addSecs(secs);
    if (secs > 0) {        updateTimer->start(UpdateInterval * 1000);//设置了5秒定时        finishTimer->start(secs * 1000);//设置了鼠标点击后的secs*1000的定时    } else {        updateTimer->stop();        finishTimer->stop();    }    update();}
int OvenTimer::duration() const{    int secs = QDateTime::currentDateTime().secsTo(finishTime);//间隔时间    if (secs < 0)        secs = 0;    return secs;}
void OvenTimer::mousePressEvent(QMouseEvent *event){    QPointF point = event->pos() - rect().center();    //根据鼠标点点击位置获得角度    double theta = std::atan2(-point.x(), -point.y()) * 180.0 / M_PI;//    setDuration(duration() + int(theta / DegreesPerSecond));    update();}
void OvenTimer::paintEvent(QPaintEvent * /* event */){    QPainter painter(this);    painter.setRenderHint(QPainter::Antialiasing, true);
    int side = qMin(width(), height());
    painter.setViewport((width() - side) / 2, (height() - side) / 2,                       side, side);    painter.setWindow(-50, -50, 100, 100);
    draw(&painter);}
void OvenTimer::draw(QPainter *painter){    static const int triangle[3][2] = {        { -2, -49 }, { +2, -49 }, { 0, -47 }    };    QPen thickPen(palette().foreground(), 1.5);    QPen thinPen(palette().foreground(), 0.5);    QColor niceBlue(150, 150, 200);
    painter->setPen(thinPen);    painter->setBrush(palette().foreground());    painter->drawPolygon(QPolygon(3, &triangle[0][0]));
    QConicalGradient coneGradient(0, 0, -90.0);    coneGradient.setColorAt(0.0, Qt::darkGray);    coneGradient.setColorAt(0.2, niceBlue);    coneGradient.setColorAt(0.5, Qt::white);    coneGradient.setColorAt(1.0, Qt::darkGray);
    painter->setBrush(coneGradient);    painter->drawEllipse(-46, -46, 92, 92);
    QRadialGradient haloGradient(0, 0, 20, 0, 0);    haloGradient.setColorAt(0.0, Qt::lightGray);    haloGradient.setColorAt(0.8, Qt::darkGray);    haloGradient.setColorAt(0.9, Qt::white);    haloGradient.setColorAt(1.0, Qt::black);
    painter->setPen(Qt::NoPen);    painter->setBrush(haloGradient);    painter->drawEllipse(-20, -20, 40, 40);
    QLinearGradient knobGradient(-7, -25, 7, -25);    knobGradient.setColorAt(0.0, Qt::black);    knobGradient.setColorAt(0.2, niceBlue);    knobGradient.setColorAt(0.3, Qt::lightGray);    knobGradient.setColorAt(0.8, Qt::white);    knobGradient.setColorAt(1.0, Qt::black);
    painter->rotate(duration() * DegreesPerSecond);    painter->setBrush(knobGradient);    painter->setPen(thinPen);    painter->drawRoundRect(-7, -25, 14, 50, 49, 49);
    for (int i = 0; i <= MaxMinutes; ++i) {        if (i % 5 == 0) {            painter->setPen(thickPen);            painter->drawLine(0, -41, 0, -44);            painter->drawText(-15, -41, 30, 30,                              Qt::AlignHCenter | Qt::AlignTop,                              QString::number(i));        } else {            painter->setPen(thinPen);            painter->drawLine(0, -42, 0, -44);        }        painter->rotate(-DegreesPerMinute);    }}
离线donjun

只看该作者 1楼 发表于: 2016-03-04
代码贴上来怎么这样了啊
离线XChinux

只看该作者 2楼 发表于: 2016-03-04
重新排下版吧.
用code标签,会类似于下面这样
  1. class MyClass
  2. {
  3. public:
  4.       MyClass();
  5. };

标签.
二笔 openSUSE Vim N9 BB10 XChinux@163.com 网易博客 腾讯微博
承接C++/Qt、Qt UI界面、PHP及预算报销系统开发业务
离线yixinuestc

只看该作者 3楼 发表于: 07-10
看了想吐
快速回复
限100 字节
 
上一个 下一个