Qt开发常用库与技巧总结一

camera:摄像头应用,开关摄像头,截图

class CameraAdapter : public QWidget
{
    Q_OBJECT

public:
    explicit CameraAdapter(QWidget *parent = 0);
    ~CameraAdapter();
    void closeCamera()const;
    void openCamera(const QString& description=QString());
    void enumCamera();
private slots:
    void onImageCaptured(int id,QImage img);
    void onImageSaved(int id, const QString &fileName);

private:
    Ui::CameraAdapter *ui;
    QCamera*               mCamera;         //摄像头对象
    QCameraViewfinder*     mViewfinder;     //摄像头取景器
    QCameraImageCapture*   mImageCapture;   //截图对象

};

hidusb:usb通信,包括枚举usb设备,读写数据

class HidDev : public QObject
{
    Q_OBJECT
public:
    explicit HidDev(QObject *parent = 0);
    ~HidDev();
    void enumHidDev();
    bool openHidDev(unsigned short vid, unsigned short pid);
    void closeHidDev();
    int hidWrite(const unsigned char *data, size_t length);
    int hidRead();
private:
    hid_device* mHidDev = nullptr;
    bool mRun = true;
};

mqtt:mqtt通信,包括订阅和发布

class MqttAdapter:public QObject
{
    Q_OBJECT
public:
    MqttAdapter(QObject* parent=0);
    ~MqttAdapter();
    bool initMqtt(const QString& clientId);
    void subScribe(const QString& toptic);
    int publicMessage(const QString& message,const QString& topic);
    void recvMessage(const char *data,char* topic);
    void connectLost(char* cause);
private:
    void printConnectError(const int errCode)const;
private:
    MQTTClient  mClient;
    bool        mhasConn = false;
};

pinyin:中文转拼音,以及缩写

class PinyinAdapter : public QObject
{
    Q_OBJECT
public:
    explicit PinyinAdapter(QObject *parent = 0);
    void pinyinParse(QString &content,QString& pingying,QString& abbrev);
};

qrcode:二维码生成

QPixmap getQrCode(const QString& content)
{
    QRcode *qrcode;
    qrcode=QRcode_encodeString(content.toStdString().c_str(),2,QR_ECLEVEL_Q,QR_MODE_8,1);
    qint32 temp_width=200;  //宽度
    qint32 temp_height=200; //高度
    qint32 qrcode_width=qrcode->width>0?qrcode->width:1;
    double scale_x=(double)temp_width/(double)qrcode_width;
    double scale_y=(double)temp_height/(double)qrcode_width;
    QImage mainimg=QImage(temp_width,temp_height,QImage::Format_ARGB32);
    QPainter painter(&mainimg);
    QColor background(Qt::white);
    painter.setBrush(background);
    painter.setPen(Qt::NoPen);
    painter.drawRect(0,0,temp_width,temp_height);
    QColor foreground(Qt::black);
    painter.setBrush(foreground);
    for(qint32 y=0;ydata[y*qrcode_width+x];
            if(b &0x01)
            {
                QRectF r(x*scale_x,y*scale_y,scale_x,scale_y);
                painter.drawRects(&r,1);
            }
        }
    }
   return QPixmap::fromImage(mainimg);
}

sql:数据库的简单使用

namespace XZPSQL {

    static  QSqlDatabase _db;
    /*
    @brief 打印支持的数据库类型
    */
    static void printDrivers()const
    {
        xPrint<

ssh:ssh通信,比如登录linux操作命令    

class SshAdapter:public QObject
{
    Q_OBJECT
public:
    SshAdapter(QObject* parent=0);
    void login(const QString& ipAddr,const QString& username,const QString& password);
    void runCmd(const QString& cmd);
    void cmdResult(const char* data);
    void loginResult(bool result);
    QSshManager* getSshManger()const;
private:
    QSshManager *mSshManger;
    int         channelNo=-1;    //表示渠道的序号
};

uart:串口通信

class UartDev : public QObject
{
    Q_OBJECT
public:
    explicit UartDev(QObject *parent = 0);
    bool openCom(const QString com);
    void closeCom() const;
    void printComInfos();
    void sendCmd(const QString& cmd);
signals:

private slots:
    void onReadData();
private:
    QSerialPort*    mSerialPort;
};

xApp:对QApplication的封装,包含dump文件的生成,日志的处理

static void MessageOutPut(QtMsgType type,const QMessageLogContext& context,const QString& msg)
{
   Q_UNUSED(type)
   Q_UNUSED(context)
   static bool _printInfo = deBugOpen();
   static QMutex mutex;
   QMutexLocker locker(&mutex);
   fprintf(stderr,"%s\n",msg.toLocal8Bit().data());
   if(!_printInfo) return;
   QString current_date_time = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");
   QString message = QString("%1 %2").arg(current_date_time).arg(msg);
   QFile file(LOG_FILE);
   file.open(QIODevice::WriteOnly | QIODevice::Append);
   QTextStream text_stream(&file);
   text_stream << message << "\r\n";
   file.flush();
   file.close();
}


XApplication::XApplication(const char* appName,int &argc, char **argv):QApplication(argc,argv)
{

#ifdef QT_NO_DEBUG
    QBreakpadInstance.setDumpPath("crashs");
#endif
    checkOnly(appName);
    qInstallMessageHandler(MessageOutPut);

    setApplicationName("XZPCode");
    setApplicationVersion("1.0.0.1");
    setOrganizationName("new thinging studio.std");
    setOrganizationDomain("www.baidu.org");
}

XApplication::~XApplication()
{
    xPrint;
}

void XApplication::checkOnly(const char* appName)
{
    if(multOpen()) return;
    //  创建互斥量
    HANDLE m_hMutex  = CreateMutex(NULL, FALSE, LPCWSTR(appName));
    //  检查错误代码
    if  (GetLastError() == ERROR_ALREADY_EXISTS)  {
      //  如果已有互斥量存在则释放句柄并复位互斥量
     CloseHandle(m_hMutex);
     m_hMutex  =  NULL;
      //  程序退出
      exit(0);
    }

}

player:包含QMediaPlayer/VLC播放视频 和 多窗口显示同一个视频

整理中。。。。。。

recode:采集音频和摄像头合成视频

整理中。。。。。。

DemoUI:包含UI常用设置技巧:如无边框 可拖动 置顶,自定义控件等技巧

整理中。。。。。。

 

代码地址:https://gitee.com/xzpblog/Qt-application-library

你可能感兴趣的:(Qt)