控件用于计时,如录音时,录视频时,或者群聊计时等等
?
class TimeRunningWidget : public QWidget
{
Q_OBJECT
public:
explicit TimeRunningWidget(QWidget* parent = nullptr);
~TimeRunningWidget();
void StartRun();
void StopRun();
void ClearTime();
private:
void InitWidget();
protected:
void paintEvent(QPaintEvent* e);
private:
QTimer *m_timer;
QTime m_time;
QString m_timeStr;
};
TimeRunningWidget::TimeRunningWidget(QWidget *parent):QWidget(parent)
{
InitWidget();
//Qt::WindowFlags flags = windowFlags();
//setWindowFlags( flags | Qt::FramelessWindowHint);
}
TimeRunningWidget::~TimeRunningWidget()
{
}
void TimeRunningWidget::StartRun()
{
m_timer->start();
}
void TimeRunningWidget::StopRun()
{
if(m_timer->isActive())
m_timer->stop();
}
void TimeRunningWidget::ClearTime()
{
if(m_timer->isActive())
m_timer->stop();
m_time.setHMS(0,0,0);
m_timeStr = m_time.toString("hh:mm:ss");
update();
}
void TimeRunningWidget::InitWidget()
{
m_timer = new QTimer(this);
m_timer->setInterval(1000);
m_time.setHMS(0,0,0);
m_timeStr = "00:00:00";
connect(m_timer,&QTimer::timeout,this,[this](){
m_time = m_time.addSecs(1);
m_timeStr = m_time.toString("hh:mm:ss");
update();
});
m_timer->start();
update();
}
void TimeRunningWidget::paintEvent(QPaintEvent *e)
{
Q_UNUSED(e);
QPainter painter;
painter.begin(this);
painter.setRenderHints(QPainter::Antialiasing, true);
QPen pen;
pen.setColor(Qt::red);
painter.setPen(pen);
painter.drawRoundedRect(1, 1, width()-2, height()-2, 16, 16);
painter.save();
QBrush br;
br.setStyle(Qt::SolidPattern);
br.setColor(Qt::red);
painter.setBrush(br);
painter.drawEllipse(QRect(10, height()/2 - 5 , 10, 10));
painter.restore();
painter.save();
pen.setColor(Qt::red);
painter.setPen(pen);
QFont font;
font.setBold(true);
font.setPixelSize(16);
painter.setFont(font);
painter.drawText(QRect(20,0,width()-20,height()), Qt::AlignCenter, m_timeStr);
painter.restore();
painter.end();
}
|