-
細說Lambda表達式
Lambda是c++11新增加的特性, QT使用時需添加如下:
1).pro文件內需要添加如下一行:
CONFIG += c++11
2)QT配合信號一起使用非常方便
QPushButton *b2 = new QPushButton(this);
b2->move(700,600);
b2->setText("Lambda test");
connect(b2, &QPushButton::clicked,
[=]() //"="號指把外部所有局部變量,類中所有成員以值傳遞方式
{
qDebug()<< "Lambda test!"<<endl;
}
);
-
QT文件操作
下面是QT 文件操作的一個實例,應用場景是按鍵打開''TXT"類型的一個配置文件,並通過","提取到數字,截圖如下:
void MainWindow::on_pushButton_11_clicked() //Read Configuration file
{
QString path = QFileDialog::getOpenFileName(this,"open","../","TXT(*.txt)");
if(path.isEmpty() == false)
{
//文件對象
QFile file(path);
bool isOK = file.open(QIODevice::ReadOnly); //只讀打開
if(isOK)
{
//讀文件 默認只識別 UTF-8
#if 0
QByteArray array = file.readAll();
ui->TxtLog->setText(QString(array));
#endif
QByteArray array;
QString str,str0,str1;
while(file.atEnd() == false)
{
array = file.readLine(); //memcpy(recvBuf,recvArr,BuffLen);
str = array;
array.clear();
QStringList strList = str.split(",", QString::SkipEmptyParts);
str0 = strList.at(0);
str1 = strList.at(1);
qDebug() <<str0.toInt() <<":"<<str1.toInt() <<endl;
//ui->TxtLog->setText(str);
}
}
file.close();
}
-
QT數據類型轉換
-
Float 轉 Qstring
法1:
float value_a = 1.506;
Qstring str;
str = QString("%1 mV").arg(value_a );
補充:如果要將轉換的字符串保留2位小數點,方法如下:
QString str = QString::number(value_a,'f',2);
法2:
下面的方法可以保留自定義的小數點位數,非常好用。
float value_a = 1.51234;
Qstring str;
str = QString::number(value_a ,'f', 2);
//'f'表示非科學計數法,後面的 2 表示保留2位小數
-
int 轉 Qstring
int i = 5;
QString str = QString::number(i);
-
Qstring 轉 int
QString str("100");
int tmp = str.toInt();
-
Qstring 轉 float
QString data;
float num = data.toFloat();
-
從Qstring字符串中去掉某指定字符
Qstring str1 = "\tA\r\n";
str1.remove(QChar('\r'), Qt::CaseInsensitive); //remove "\r"
str1.remove(QChar('\n'), Qt::CaseInsensitive); //remove "\n"
qDebug()<<str1 <<endl; //打印結果爲"\tA"
- QString中某字符轉ASC碼
QString str = "aBCdefg";
int ascVal = str.at(0).toLatin1();
qDebug()<<"str0= "<<ascVal<<endl; //打印出 "str0= 97"
QT thread 用法
Qt裏面有2種線程的選擇:
- Qt內置的QThread
- c++11帶的std::thread
1. Qt內置的QThread 用法
-
包含頭文件: #include <QThread>
-
創建myThread類,並繼承 QThread
class myThread : public QThread
{
Q_OBJECT
public:
explicit myThread(QObject *parent = 0);
protected:
void run(); //重寫父類的虛函數 run
signals:
void mySig_complete();
public slots:
};
3.寫Thread的處理函數- myThread::run
#include <QDebug>
#include "mythread.h"
myThread::myThread(QObject *parent) : QThread(parent)
{
/* 構造函數 */
}
void myThread::run() //線程入口
{
/* user thread handle code */
}
4. 關閉QT界面時,析構Thread
4.1 首先在UI的Qwidget的構造函數添加關閉UI的信號和槽
connect(this,&myThread_xx::destroyed,this,&myThread_xx::stopThread);
4.2 在關閉UI的槽函數內析構Thread內存
void myThread_xx::stopThread()
{
qDebug()<<"thread stopped"<<endl;
/* stop thread */
thread->quit();
/* wait for thread finish processing */
thread->wait();
}
2. Qt內c++11的std::thread用法
1. 包含頭文件
#include <thread>
#include <condition_variable>
2. 聲明線程函數和條件變量
void register_parse(); //process thread
std::mutex my_mutex;
std::condition_variable cv; //cpp11 條件變量
這裏使用的信號量,是由 mutex和condition_variable組合使用的。
3. 在構造函數內創建線程
std::thread t1(&MainWindow::register_parse, this); //thread call on Class Member
if(t1.joinable())
{
//t1.join();
t1.detach();
}
else
{
qDebug()<<"thread is unjoinable"<<endl;
}
4. 發送信號量
cv.notify_one(); //release semaphore
qDebug()<<"接球"<<endl;
5. 線程阻塞等待信號量
void MainWindow::register_parse()
{
for(;;)
{
std::unique_lock<std::mutex> locker(my_mutex);
cv.wait(locker); //wait semaphore
qDebug()<<"satrt register parse..."<<endl;
}
}
QT 複選框用法:
當使用兩兩互斥的選擇框場景時,RadioButton控件絕對不能用的,只能使用CheckBox控件。
下面講解下兩兩互斥場景下的Check Box控件的用法:
上圖所示的場景需要兩兩互斥,PASS和FAIL狀態同一時刻只能有1個被選擇。
代碼實現如下:
void MainWindow::on_checkBox_PASS_clicked(bool checked)
{
if(checked)
{
status_DetectorPower = true; //class自定義的bool類型
ui->checkBox_DEC_2->setChecked(false);
}
}
void MainWindow::on_checkBox_FAIL_clicked(bool checked)
{
if(checked)
{
status_DetectorPower = false; //class自定義的bool類型
ui->checkBox_DEC->setChecked(false);
}
}
以上代碼即可實現,status_DetectorPower 的 bool 變量可以知道最終選擇的是哪一個狀態。