当前位置:   article > 正文

QT学习之旅 - 关于StatusBar底边的限制,窗口限制,多线程的使用_gtk statusbar的大小

gtk statusbar的大小

StatusBar大小限制(注意这是StatusBar大小不是窗口的)

/*
statusBar()->setMinimumSize(600,400);
statusBar()->setMinimumWidth(600);
statusBar()->setMinimumHeight(400);
*/
//ui-statusbar设置
ui->statusbar->setMinimumSize(600,400);
ui->statusbar->setMinimumWidth(600);
ui->statusbar->setMinimumHeight(400);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

设置窗口大小限制

setMinimumSize(600,400);
//setMinimumWidth(600);
//setMinimumHeight(400);
  • 1
  • 2
  • 3

时间设置+statusbar中显示

statusbar中输入文字
status->setText(time + QString(" ") + message);
ui->statusbar->addWidget(status);
  • 1
  • 2
时间设置
QString time = QDateTime::currentDateTime().toString("hh:mm:ss.zzz");
  • 1

获取本机地址

ConfigDialog::ConfigDialog(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ConfigDialog)
{
    ui->setupUi(this);
    QStringList list1 = QStringList(getLocalHostIpAddress());
    ui->combLocalIP ->addItems(QStringList(list1));
}

QString ConfigDialog::getLocalHostIpAddress(){
    QString strIpAddress;
    QList<QHostAddress> ipAddressList = QNetworkInterface::allAddresses();
    //获取第一个本主机的IPv4地址
    int nListSize = ipAddressList.size();
    for(int i = 0;i < nListSize; i++){
        if(ipAddressList.at(i) != QHostAddress::LocalHost &&
           ipAddressList.at(i).toIPv4Address()){
           strIpAddress = ipAddressList.at(i).toString();
           break;
        }
    }
    //如果没找到,则以本地IP地址位IP
    if(strIpAddress.isEmpty()){
        strIpAddress = QHostAddress(QHostAddress::LocalHost).toString();
    }
    return strIpAddress;
}


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
comboBox控件写入数据
QStringList list1 = QStringList(getLocalHostIpAddress());
ui->combLocalIP ->addItems(QStringList(list1));
  • 1
  • 2

获取LineEdit控件内容

QString str = ui->textEdit->text();
  • 1

获取ComboBox控件内容

QString my_local_ip = ui->combLocalIP->currentText();
  • 1

引入QtCharts

#pro中
QT += charts
  • 1
  • 2
//HexDataProess.cpp
#include "iostream"
#include "QString"
#include <QtNetwork>
#include <QMainWindow>
#include <QtCharts>
#include <QUdpSocket>
#include <QTimerEvent>
#include <QDebug>
#include "HexDataProcess.h"

QList<double> REM(QString data)
{
    QList<double> Orignal;
    QList<QString> Changing;    //储存6字节分割的数据
    QList<double> result;

    int countflag = 0;

    while(countflag < data.size())  //将数据导入链表
    {
        QString rem;
        rem = data.mid(countflag,6);
        //qDebug()<<"数据组包"<<rem;
        Changing.append(rem);
        countflag+=6;
    }

    QString rem;   //将链表内的数据依次转换
    for(int i = 0 ; i < Changing.size(); i++)
    {
        //qDebug("%d",i);
        rem=Changing[i];
        int count;
        qint8 array[6];
        qint64 num[24]={0};
        int h=23;

        for(int b = 0 ; b < 6 ; b++ )
        {
            array[b]=rem.at(b).unicode();   //返回指定位置字符的ascii值
        }

        for(int l=5;l>=0;l--)           //获取补码转入整形数组中
        {
            if(array[l]>57)             //属于abcde
            {
                if( array[l] > 64 && array[l] < 71 )    //大写
                count = array[l] - 55;
                else
                count = array[l] - 87;                  //小写

                for(int j = 0 ; j < 4 ; j++)            //将数据转换为二进制
                {
                    num[h--] = count % 2;
                    count /= 2;
                }
            }
            else                    //属于0-9
            {
                count = array[l] - 48;
                for(int j = 0 ; j < 4 ; j++)
                {
                    num[h--] = count % 2;
                    count/=2;
                }
            }
        }


        if(num[0]==1)       //如果是负数,需将补码转为原码
        {
            for(int i=23;i>0;i--)       //减一
               {
                    if(num[i] == 1)
                    {
                        num[i] = 0;
                        break;
                    }
                    else
                    {
                        num[i]=1;
                    }
               }

            for(int i=23;i>0;i--)   //取反
            {
                if(num[i]==1)
                    num[i]=0;
                else
                    num[i]=1;
            }
        }

        double sum=0;          //得到采样值

        for(int i=23;i>0;i--)
        {
            for(int l = 0 ; l< 23 - i ; l++)
            {
                num[i] *= 2;
            }
            sum += num[i];
        }

        if(num[0] == 1)       //判断数据的正负
            sum *= (-1);

        //qDebug()<<"原始数据大小"<<sum;
        result.append( sum / ADC_24_MAX * voltage);
        Orignal.append(sum);
    }

    return result;

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
//HexDataProess.h
#ifndef HEXDATAPROCESS_H
#define HEXDATAPROCESS_H

#include "QString"
#include <QMainWindow>

#define  ADC_24_MAX  8388607
#define  voltage     (2.5)
QList<double> REM(QString date);

#endif // HEXDATAPROCESS_H

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

QT Thread(多线程)

QT的线程是为了,防止一直在一个线程中而不执行其他应用

#include <QThread>
class 类名 : public QThread{};
  • 1
  • 2
c++多线程在Qt中使用
#include "testthread.h"
#include "ui_testthread.h"
#include "unistd.h"
#include <QDebug>

TestThread::TestThread(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::TestThread)
{
    ui->setupUi(this);
    connect(ui->pushButton,&QPushButton::clicked,this,&TestThread::on_pushButton_click);
}

TestThread::~TestThread()
{
    delete ui;
}

void TestThread::showInfo(){
    int i=0;
    for(;;){
        qDebug() << i;
        ui->lineEdit->setText(QString::number(i++));
        sleep(1);
    }
}

void TestThread::on_pushButton_click(){
    std::thread my_thread(&TestThread::showInfo,this);
    my_thread.detach();
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
QThread

在这里插入图片描述

开启线程的方法void start(QThread::Priority priority = InheritPriority)

退出线程void quit()void exit(int returnCode = 0)这个exit要退出时在时间片上的线程退出不了,要跟bool wait(unsigned long time = ULONG_MAX)进行配合

终止线程的方式(不推荐)void terminate()不安全的函数,一般要和wait函数配合
在这里插入图片描述
使用一个自定义类继承QThread来重写run方法

//my_thread.cpp
#include <my_thread.h>
#include <QDebug>

void My_Thread::run(){
    int i = 0;
    for(;;){
        emit threadSignal(i++);
        qDebug() << i;
        this->sleep(1);
        if(i > 10){ //大于10退出循环
            break;
        }
    }
}

My_Thread::My_Thread(QObject *parent):QThread(parent){

}

My_Thread::~My_Thread(){
	//解构函数
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
//自定义ui界面(这里是继上个页面的ui)
#include "testthread.h"
#include "ui_testthread.h"
#include "unistd.h"
#include <QDebug>

void TestThread::on_pushButton_click(){
    my_thread->start();
}

TestThread::TestThread(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::TestThread)
{
    ui->setupUi(this);
    my_thread = new My_Thread(this);//my_thread 在.h中申明
    connect(ui->pushButton,&QPushButton::clicked,this,&TestThread::on_pushButton_click);
    connect(my_thread,&UdpThread::threadSignal,[=](int val){
        ui->lineEdit->setText(QString::number(val));
    });
}



TestThread::~TestThread()
{
    delete ui;
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
线程的退出

线程执行时退出-程序异常结束

TestThread::~TestThread()
{  //显示使用的ui  cpp文件的解构函数
    delete ui;
    my_thread->exit(); //线程退出
    my_thread->wait(); //线程等待
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/94934
推荐阅读
相关标签
  

闽ICP备14008679号