如何用QT实现一个计时器的程序

如何用QT实现一个计时器的程序,第1张

database infomastion

$db_info=array(

'h'=>'localhost',

'u'=>'root',

'pwd'=>'program',

'dbname'=>'agency'

)

$mysqli = new mysql($db_info)

$query = $mysqli->query("select * from user_list")

while($row=$mysqli->fetch_row($query)){

echo $row[1]

}

建议你这样试试看:

#ifndef QWAITTINGDIALOG_H

#define QWAITTINGDIALOG_H

#include <QDialog>

#include <QLabel>

#include <QThread>

#include <QTimer>

class QWaittingDialog : public QDialog

{

  Q_OBJECT

public:

  explicit QWaittingDialog(QWidget *parent = nullptr)

  void Run(int nStartValue)

signals:

public slots:

  void on_timer_timeout()

private:

  QLabel*             m_pLabel

  QTimer*             m_pTimer

  int                 m_nStartValue

  // QWidget interface

protected:

  void paintEvent(QPaintEvent* event)

}

#endif // QWAITTINGDIALOG_H

#include "QWaittingDialog.h"

#include <QPainter>

#include <QVBoxLayout>

QWaittingDialog::QWaittingDialog(QWidget *parent) : QDialog(parent)

  , m_nStartValue(0)

{

  setWindowFlags(Qt::FramelessWindowHint | Qt::Tool | Qt::WindowStaysOnTopHint)

  // 全透明

  setAttribute(Qt::WA_TranslucentBackground)

  QVBoxLayout* pVBoxLayout = new QVBoxLayout(this)

  m_pLabel = new QLabel()

  pVBoxLayout->addWidget(m_pLabel)

  m_pLabel->setText("")

  m_pLabel->setAlignment(Qt::AlignCenter)

  QString strStyle = "QLabel{"

                     "font-family: \"Microsoft YaHei\""

                     "font-size: 128px"

                     "color: rgb(50, 50, 50, 180)"

                     "}"

  m_pLabel->setStyleSheet(strStyle)

  m_pTimer = new QTimer(this)

  connect(m_pTimer, &QTimer::timeout, this, &QWaittingDialog::on_timer_timeout)

  m_pTimer->setInterval(1000)

}

void QWaittingDialog::Run(int nStartValue)

{

  m_pTimer->stop()

  m_nStartValue = nStartValue

  QWidget* parent = parentWidget()

  move(parent->pos())

  resize(parent->rect().width(), parent->rect().height())

  on_timer_timeout()

  m_pTimer->start()

  show()

}

void QWaittingDialog::on_timer_timeout()

{

  if (m_nStartValue == 0)

  {

      m_pTimer->stop()

      close()

      return

  }

  m_pLabel->setText(QString::number(m_nStartValue))

  m_nStartValue--

}

void QWaittingDialog::paintEvent(QPaintEvent* event)

{

  // 避免鼠标穿透

  QPainter painter(this)

  painter.fillRect(this->rect(), QColor(240, 240, 240, 1))   // 配合全透明属性一起使用

}

这样做的好处:

注意事项:

// 启动定时器

QTimer *timer = new QTimer()

connect(timer, SIGNAL(timeout()), this, SLOT(timeout())

progressBar->setMaximum(100)

// 写槽函数

void MainWindow::timeout() {

    static int nVal = 0    

    progressBar->setValue(nVal++)

}

// 再然后启动定时器即可

timer->start(1000)// 每秒触发一次


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/tougao/11290548.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-15
下一篇 2023-05-15

发表评论

登录后才能评论

评论列表(0条)

保存