QT随手笔记

 

  • 细说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();
  • asc码转字符或string

int ascValue = 56;
QString str = static_cast<QString>(ascValue );
qDebug()<<str <<endl;

  • 从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 用法

  1. 包含头文件: #include <QThread>

  2. 创建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 变量可以知道最终选择的是哪一个状态。

 

QT 捕获关闭窗口事件:

connect(this, &MainWindow::destroyed, this, &MainWindow::window_destroyed_handle);

void MainWindow::window_destroyed_handle()
{
    qDebug()<<"window_destroyed"<<endl;
}

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值