xxxx
第一部分:HelloWorld
- 项目目录中各个文件说明
|
文件 |
说明 |
|
helloworld.pro |
项目文件,包含项目相关信息 |
|
helloworld.pro.user |
与用户有关的项目信息 |
|
hellodialog.h |
类的头文件 |
|
hellodialog.cpp |
类的源文件 |
|
main.cpp |
Main方法 |
|
hellodialog.ui |
界面文件 |
第二部分:基础控件
- 类关系图
Qt Creator提供的默认基类只有QMainWindow、QWidget和QDialog三种。是的,这三种窗体也是以后用的最多的,QMainWindow是带有菜单栏和工具栏的主窗口类,QDialog是各种对话框的基类,而它们二者全部继承自QWidget。不仅如此,其实所有的窗口部件都继承自QWidget。
第三部分:QTimer定时器
https://gitee.com/qq602431132/lessonqt/tree/master/lesson_Timer
|
cpp |
||
|
//定时器指针 QTimer *fTimer; |
//初始化 fTimer = new QTimer(this); fTimer->stop(); //设置定时周期,单位毫秒。 fTimer->setInterval(1000); //关联定时器的信号与槽 connect(fTimer, SIGNAL(timeout()), this, SLOT(on_timer_timeout())); |
|
|
//计时器 QTime fTimeCounter; |
||
|
//定时器中断处理槽函数,手工定义 void on_timer_timeout(); |
//定时器中断响应槽函数 QTime curTime=QTime::currentTime(); //获取当前时间 ui->LCDHour->display(curTime.hour()); //显示 小时 ui->LCDMin->display(curTime.minute());//显示 分钟 ui->LCDSec->display(curTime.second());//显示 秒 int va=ui->progressBar->value(); //读取progressBar的数值 va++; if (va>100) va=0; ui->progressBar->setValue(va); //设置progressBar的数据 |
|
|
void on_btnStart_clicked(); //开始 定时器 按键 |
fTimer->start();//定时器开始工作 fTimeCounter.start();//计时器开始工作 //更新各按键的状态 ui->btnStart->setEnabled(false); ui->btnStop->setEnabled(true); ui->btnSetIntv->setEnabled(false); |
|
|
void on_btnStop_clicked(); //停止定时器按键 |
fTimer->stop(); //定时器停止 int tmMsec = fTimeCounter.elapsed(); //毫秒数 int ms = tmMsec % 1000; //余数毫秒 int sec = tmMsec / 1000; //整秒 QString str = QString::asprintf("流逝时间:%d 秒,%d 毫秒", sec, ms); ui->LabElapsTime->setText(str); //显示流逝的时间 ui->btnStart->setEnabled(true); //更新按键状态 ui->btnStop->setEnabled(false); ui->btnSetIntv->setEnabled(true); |
|
|
void on_btnSetIntv_clicked(); //设置周期 按键 |
//设置定时器周期 fTimer->setInterval(ui->spinBoxIntv->value()); //设置定时器的周期 |
第四部分:QThread
https://gitee.com/qq602431132/lessonqt/tree/master/lesson_Thread
|
cpp |
|||
|
//线程是继承自QThread,与主线程分离 class QDiceThread : public QThread |
|||
|
protected: void run() Q_DECL_OVERRIDE; //线程任务 private: int m_seq=0;//掷骰子次数序号 int m_diceValue;//骰子点数 //控制任务是否停止 开启:false,暂停:true bool m_Paused=true; //掷一次骰子 //控制进程是否停止 开启:true bool m_stop=false; //停止线程 |
//线程任务 m_stop=false;//启动线程时令m_stop=false m_seq=0; //掷骰子次数 qsrand(QTime::currentTime().msec());//随机数初始化,qsrand是线程安全的 while(!m_stop)//循环主体 { if (!m_Paused) { m_diceValue=qrand(); //获取随机数 m_diceValue=(m_diceValue % 6)+1; m_seq++; emit newValue(m_seq,m_diceValue); //发射信号 } msleep(500); //线程休眠500ms } // 在 m_stop==true时结束线程任务 quit();//相当于 exit(0),退出线程的事件循环 |
||
|
//子线程发出信号 signals: void newValue(int seq,int diceValue); //产生新点数的信号 |
|||
|
//主界面里 private: QDiceThread threadA; |
//线程开启 connect(&threadA,SIGNAL(started()),this, SLOT(onthreadA_started())); //线程关闭 connect(&threadA,SIGNAL(finished()),this, SLOT(onthreadA_finished())); //子线程信号 connect(&threadA,SIGNAL(newValue(int,int)),this, SLOT(onthreadA_newValue(int,int))); |
||
|
//主界面关闭,顺带关闭线程 protected: void closeEvent(QCloseEvent *event); |
//窗口关闭事件,必须结束线程 if (threadA.isRunning()) { threadA.stopThread(); threadA.wait(); } event->accept(); |
||
|
//线程开始按钮;执行任务按钮;暂停任务按钮;线程结束按钮。 private slots: void onthreadA_started(); void onthreadA_finished(); void onthreadA_newValue(int seq, int diceValue); void on_btnClear_clicked(); void on_btnDiceEnd_clicked(); void on_btnDiceBegin_clicked(); void on_btnStopThread_clicked(); void on_btnStartThread_clicked(); |
//启动线程 按钮 threadA.start(); //结束线程 按钮 threadA.stopThread();//结束线程的run()函数执行到m_stop=true;就会从循环里跳出来 threadA.wait();// //开始 掷骰子按钮 threadA.diceBegin(); //暂停 掷骰子按钮 threadA.dicePause(); //QDiceThread的newValue()信号的响应槽函数,显示骰子次数和点数 QString str=QString::asprintf("第 %d 次掷骰子,点数为:%d",seq,diceValue); ui->plainTextEdit->appendPlainText(str); QPixmap pic; //图片显示 QString filename=QString::asprintf(":/dice/images/d%d.jpg",diceValue); pic.load(filename); ui->LabPic->setPixmap(pic); |