C++ Qt 知识总结

  1. 面向过程的static:
    1. 函数中的静态变量 :当变量声明为static时,空间将在程序的生命周期内分配,其被存放在在全局数据区。即使多次调用该函数,静态变量的空间也只分配一次
    2. 静态区(全局区):静态变量和全局变量的存储区域是一起的,一旦静态区的内存被分配, 静态区的内存直到程序全部结束之后才会被释放。
#include <iostream> 
#include <string> 
using namespace std; 

void demo() 
{
   
    
    static int count = 0;  // 静态变量 ,调用demo函数时才会构造count变量,main函数结束时才析构
    cout << count << " "; 
    count++; 
} 

int main() 
{
   
    
    for (int i=0; i<5; ++i)  
        demo(); 
    return 0; 
}

// 输出:0 1 2 3 4
  1. 面向对象的static:
    1. 类中的静态变量 声明为static的变量只被初始化一次,因为它们在单独的静态存储中分配了空间,因此类中的静态变量由对象共享,可以减小对象的大小。对于不同的对象,不能有相同静态变量的多个副本。也是因为这个原因,静态变量不能使用构造函数初始化
    2. 类中的静态成员变量必须在类内声明,在类外定义(被const修饰的除外)
    3. 静态类 :和变量一样,静态类的生命周期直到程序的结束。在main结束后才会调用静态类的析构函数。
class Apple 
{
   
    
public: 
    static int i;  // 类内声明

    Apple() 
    {
   
    
        // Do nothing 
    }; 
}; 

int Apple::i = 1;  // 类内定义
class Apple 
{
   
    
public: 
    // 被const修饰的static变量在c++11中可以在类内被直接初始化。
    static const int i=10; 
    Apple() 
    {
   
    
        // Do nothing 
    }; 
};
  1. C++11 condition_variable使用方法
#include <iostream>           // std::cout
#include <thread>             // std::thread
#include <chrono>             // std::chrono::seconds
#include <mutex>              // std::mutex, std::unique_lock
#include <condition_variable> // std::condition_variable, std::cv_status
std::condition_variable cv;
int value;
void do_read_value()
{
   
   
    std::cin >> value;
    cv.notify_one(); //只有键盘敲入一个字符,按下enter,才往下执行cv.notify_one();
}

int main ()
{
   
   
    std::cout << "Please, enter an integer (I'll be printing dots): \n";
    std::thread th(do_read_value);
    std::mutex mtx;
    std::unique_lock<std::mutex> lck(mtx);    		// 加锁互斥量,若lck构造时不传入mtx,会崩溃
	cv.wait_for(lck,std::chrono::seconds(1))// 当超时1s的时候,相当于收到了通知信号,就被唤醒干活了
    std::cout << "You entered: " << value << '\n';
    th.join();
    return 0;
}
  1. Qt中条件等待、异步转同步方式整理
#include <QThread>
#include <QDebug>
#include <QJsonObject>
#include <QTime>
#include <QTimer>
#include <QEventLoop>
#include <QMutex>
#include <QWaitCondition>
 
// Qt中条件等待、异步转同步方式整理
class ThreadTest : public QThread
{
   
   
    Q_OBJECT
public:
    explicit ThreadTest(QObject *parent = nullptr) : QThread(parent){
   
   }
    void run() override
    {
   
   
        m_stop = false;
        qDebug()<<"void run() override start";
        // 1. QTime
        m_time.restart();
        while ( !m_stop && m_time.elapsed() < m_waitInterval )
        {
   
   
            msleep(20);
        }
 
        // 2. QEventLoop
        m_loop = new QEventLoop;
        QTimer::singleShot(m_waitInterval, m_loop, SLOT(quit()));
        m_loop->exec();
 
        // 3. QWaitCondition
        m_mutex.lock();
        m_condition.wait(&m_mutex, m_waitInterval);
        m_mutex.unlock();
 
        qDebug()<<"void run() override end";
    }
 
    Q_INVOKABLE void stop()
    {
   
   
        m_stop = true;
        m_loop->quit();
        m_condition.wakeAll();
    }
 
private:
    bool m_stop;
    const int m_waitInterval = 5000;
    QTime m_time;
    QEventLoop *m_loop;
    QMutex m_mutex;
    QWaitCondition m_condition;
};
  1. Qml中组件构造顺序,析构顺序,发出Component.onCompleted、Component.onDestruction顺序
Window {
   
   
    width: 400
    height: 400
    visible: true
    Component.onCompleted: console.log("Component.onCompleted =========0")
    Component.onDestruction: console.log("Component.onDestruction =====================0")

    // 这里QYSimpleImage每次执行构造函数,都会将m_count + 1
    QYSimpleImage {
   
   
        Component.onCompleted: console.log("Component.onCompleted =========1")
        Component.onDestruction: console.log("Component.onDestruction =====================1")
        anchors.fill: parent
        QYSimpleImage{
   
   
            Component.onCompleted: console.log("Component.onCompleted =========2")
            Component.onDestruction: console.log("Component.onDestruction =====================2")
            anchors.fill: parent
            QYSimpleImage{
   
   
                Component.onCompleted: console.log("Component.onCompleted =========3")
                Component.onDestruction: console.log("Component.onDestruction =====================3")
                anchors.fill: parent
                QYSimpleImage{
   
   
                    Component.onCompleted: console.log("Component.onCompleted =========4")
                    Component.onDestruction: console.log("Component.onDestruction =====================4")
                    anchors.fill: parent
				}
			}
		}
	}
}
// 打印信息如下:
QYSimpleImage::QYSimpleImage m_count: 1
QYSimpleImage::QYSimpleImage m_count: 2
QYSimpleImage::QYSimpleImage m_count: 3
QYSimpleImage::QYSimpleImage m_count: 4
qml: Component.onCompleted =========0
qml: Component.onCompleted =========1
qml: Component.onCompleted =========2
qml: Component.onCompleted =========3
qml: Component.onCompleted =========4
qml: Component.onDestruction =====================4
qml: Component.onDestruction =====================3
qml: Component.onDestruction =====================2
qml: Component.onDestruction =====================1
qml: Component.onDestruction =====================0

// 组件析构顺序先内层,再外层(先子再父)
// Component.onCompleted信号发出顺序:先外层,再内层(先父再子)
// Component.onDestruction信号发出顺序:先内层,再外层(先子再父)
// qml父组件发送Component.onCompleted信号时,其子组件已经完成可写属性的赋值操作了
  1. QObject中enable属性会进行值的传递,若父类的enable为true,则子类也为true,若父类为false,子类也为false
  2. MouseArea事件传播特性:
MouseArea{
   
   	
	propagateComposedEvents: true //表示允许事件往下传递 
	hoverEnabled: true		// 若其他的组件也具有hover等效果,设置为true且执行onEntered onExited,事件就不会向下传播
	onEntered: {
   
   }
	onExited: {
   
   }
	onClicked: {
   
   
		// Do not handle this event, pass it to the next layer,如果为true,则不可以把事件传播到下一层,很有用
		mouse.accepted = false  
	}
}
MouseArea {
   
   
    anchors.fill: parent
    acceptedButtons: Qt.AllButtons
    propagateComposedEvents: true
    onPressed: {
   
   
        console.log("onPressed")
        mouse.accepted = false
    }
    onReleased: {
   
   
        console.log("onReleased")
        mouse.accepted = false
    }
    onClicked: {
   
   
        console.log("onClicked")
        mouse.accepted = false
    }
}

在propagateComposedEvents属性设置为true时:

  1. 若pressed的mouse.accepted = false,在点击事件后只有onpress会生效
  2. 若要让所有的事件都可以从上面穿透,将onPressed、onEntered等的mouse.accepted设置为false
  3. 正常点击事件顺序 : pressed->released->clicked,其中 clicked是组合事件
  1. 在使用tabview的qml组件时,想获得它的rows和columns,有两个方法:
    1. 使用C++定义的rowCount和columnCount,但是在刷新界面的时候老是会出现刷新一个item时宽高等信息不对称,暂时也不知道原理。隐藏着较多bug
    2. 用tableview内的rows和columns,就不会出现刷新某一个item时宽高等信息不对应的问题
  2. C++ new一个任意指针,delete不会有问题,但是如果p++之后,再次delete就会出错。若初始时将某个指针设置为nullptr,则可以无限delete而不会出错
  3. C 语言strcat函数:

char *strcat(char *dest, const char *src),两个指针必须使用\0作为结束符才可以正常拼接,包括平时使用char指针或者数组的时候都需要注意以\0作为结束符,否则在调用一些函数时无法得到正常的处理。

  1. C语言spintf函数:

int sprintf(char *str, char * format [, argument, …]);
如sprintf(rpn + strlen(rpn), " %s", number.c_str()),将number拼接至rpn+strlen(rpn)的位置,以空格作为间隔

  1. QDialog 与主页面无关 -> 先new类,再this->show();与主页面相关,-> 先new,this->exec(),delete this
  2. QTableWidget设置表头不可点击,ui->regionTable->horizontalHeader()->setSectionsClickable(false)
  3. QTableWidget currentcellchanged信号对于翻动QtableWidget很有用
  4. cv::Mat==操作符:
// 不相等为0,相等为255(nan与nan不相等)0显示为黑色,255显示为白色,
// temp == temp是指每一个单元格的值进行相等比较,相等为true,否则为false
cv::Mat tmpMask = (temp == temp)</
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值