QT习惯总结

QT习惯总结:

1. 随机数种子只需调用一次即可。

        //产生随机数
        static bool hasCalledQsrand = false;
        if(false == hasCalledQsrand){//随机数种子只种一次,下次不再种植
            qsrand(QTime(0, 0, 0).secsTo(QTime::currentTime()));
        }
        int num = qrand() %11 + 20;//20~30的随机数

       

2. QDialog::exec()

exec()是一个循环时间函数exec()什么时候才能返回
    1. 返回QDialog::Accepted:
        调用accept()()
    2. 返回QDialog::Rejected:
        reject(),close(),hide(),destory(),delete
    3. 返回参数:done(int r)

[virtual slot] int QDialog::exec()
Shows the dialog as a modal dialog, blocking until the user closes it. The function returns a DialogCode result.
If the dialog is application modal, users cannot interact with any other window in the same application until they close the dialog. If the dialog is window modal, only interaction with the parent window is blocked while the dialog is open. By default, the dialog is application modal.
See also open(), show(), result(), and setWindowModality().

用法:
if (QDialog::Accepted == ADialog.exec()){
    ...

}else{
    ...
}

3. QVariant


  QDataStream out(...);
  QVariant v(123);                // The variant now contains an int
  int x = v.toInt();              // x = 123
  out << v;                       // Writes a type tag and an int to out
  v = QVariant("hello");          // The variant now contains a QByteArray
  v = QVariant(tr("hello"));      // The variant now contains a QString
  int y = v.toInt();              // y = 0 since v cannot be converted to an int
  QString s = v.toString();       // s = tr("hello")  (see QObject::tr())
  out << v;                       // Writes a type tag and a QString to out
  ...
  QDataStream in(...);            // (opening the previously written stream)
  in >> v;                        // Reads an Int variant
  int z = v.toInt();              // z = 123
  qDebug("Type is %s",            // prints "Type is int"
          v.typeName());
  v = v.toInt() + 100;            // The variant now hold the value 223
  v = QVariant(QStringList());

4. QVariantList的巧用

QVariantList values;
values.clear();//1. 清空
cPatient = editPatientInfoDialog.GetPatientInfo();
values << cPatient.m_iPatentType
       << cPatient.m_iSex
       << cPatient.m_iAge
       << cPatient.m_strIdentification
       << cPatient.m_iAgeType;
UpdateSqlDataWrap(record.value(cmysqldataWrap::Result_ID).toInt(), values);

 

 

10.28

编译依赖,减少编译时间

https://www.cnblogs.com/ORC-Lee/p/4848220.html

 

5、跨平台特性:

变量定义时,尽量使用位数确定的基本类型

qint64,uint8,quint8,quint16... ...

 

6、多线程保护数据

static QMutex sendCommandMutex;   //多线程保护command的mutex
void CmyDataType::writeData(const char *data, qint64 maxSize)
{
    if(NULL != data)
    {
        sendCommandMutex.lock();
        sendCommandList << QByteArray(data, maxSize);
        sendCommandMutex.unlock();
    }
}

 

7、Q_FUNC_INFO 调试 宏

 

8、频繁使用的小算法建议使用宏实现

例如:

#ifndef _min_
#define _min_(a,b) (((a) < (b)) ? (a) : (b))
#endif

#ifndef max
#define max(a,b) (((a) > (b)) ? (a) : (b))
#endif

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值