Qt之日常积累

Qt获取QDataTimeEdit的值

QDateTime datetimes;

QDateTimeEdit *time = new QDateTimeEdit(QDateTime::currentDateTime());

datetimes = time->dateTime();

读取QTextEdit的值 QString content = ui.contentTextEdit->toPlainText(); QString和std::string互转
std::string cstr;
QString qstring;
//****从std::string 到QString
qstring = QString(QString::fromLocal8Bit(cstr.c_str()));
//****从QString 到 std::string
cstr = std::string((const char *)qstring.toLocal8Bit());

CopyFile函数用法

BOOL CopyFile(

LPCTSTR lpExistingFileName, // pointer to name of an existing file
LPCTSTR lpNewFileName, // pointer to filename to copy to
BOOL bFailIfExists // flag for operation if file exists
);
其中各参数的意义:
LPCTSTR lpExistingFileName, // 你要拷贝的源文件名
LPCTSTR lpNewFileName, // 你要拷贝的目标文件名
BOOL bFailIfExists // 如果目标已经存在,不拷贝(True)并返回False,覆盖目标(false)
如:
//拷贝文件c:\log.txt到d:\log.txt,如果D:\log.txt已经存在,就覆盖
CopyFile("c:\\log.txt","d:\\log.txt",false); 


QString的字符转换

String fromAscii ( const char * str, int size = -1 )
QString fromLatin1 ( const char * str, int size = -1 )
QString fromLocal8Bit ( const char * str, int size = -1 )
QString fromRawData ( const QChar * unicode, int size )
QString fromStdString ( const std::string & str)
QString fromStdWString ( const std::wstring & str)
QString fromUcs4 ( const uint * unicode, int size = -1 )
QString fromUtf8 ( const char * str, int size = -1 )
QString fromUtf16 ( const ushort * unicode, int size = -1 )
QString fromWCharArray ( const wchar_t * string, int size = -1)


qstring ->std::string
qstring::toStdString () ,qstring::toStdWString ()

BSTR<->qstring
BSTR bstr_str;
qstring q_str((QChar*)bstr_str, wcslen(bstr_str))
bstr_str = SysAllocString(q_str.utf16())

remember use SysFreeString onBSTR

qstring<->LPCSTR
qstring::toLocal8Bit().constData()
QString fromLocal8Bit ( const char * str, int size = -1 )

qstring<->LPCWSTR
qstring::utf16()
QString fromUtf16 ( const ushort * unicode, int size = -1 )

qstring<->CString
CString c_str(qstring::utf16())
QString fromUtf16 (LPCTSTR(c_str) )



1.QString转char *
先将QString转换为QByteArray,再将QByteArray转换为char *。
注意:不能用下面的转换形式char *mm = str.toLatin1().data();。因为这样的话,str.toLatin1()得到的QByteArray类型结果就不能保存,最后转换,mm的值就为空。
 
2. char * 转QString
可以使用QString的构造函数进行转换:QString(const QLatin1String &str);
QLatin1String的构造函数:QLatin1String(const char *str);
则如下语句是将char * mm转换为QString str:
str = QString(QLatin1String(mm));

当转换中文时

1.QString转char *

先将QString转为标准库中的string类型,然后将string转为char*,如下:

std::string str = filename.toStdString();

const char* ch = str.c_str();

2. char * 转QString

char * ch;

 QString str(ch);


例子:
 
view plain
//#include "myclass.h"  
#include <QtGui/QApplication>  
#include <QtDebug>  
#include <QString>  
  
int main(int argc, char *argv[])  
{  
    QApplication a(argc, argv);  
  
    QString str = “hello”; //QString转char *  
     QByteArray ba = str.toLatin1();  
    char *mm = ba.data();  
    qDebug()<<mm<<endl;  //调试时,在console中输出  
  
      QString nn = QString(QLatin1String(mm)); //char *转QString  
    qDebug()<<nn<<endl;  //调试时,在console中输出  
  
    //MyClass w;  
    //w.show();  
    return a.exec();  


qt中获取文件路径和文件名

void MainWindow ::on_pushButton_2_clicked ()
{
    QString file_full, file_name, file_path;
    QFileInfo fi;
    file_full = QFileDialog::getOpenFileName(this);
    fi = QFileInfo(file_full);
    file_name = fi.fileName();
    file_path = fi.absolutePath();
    ui->textEdit->setText(file_path+"\\"+file_name);
}



数据库操作

query.exec("insert into FaceRec_RegInfo (FRRegID, FRName, FRSex, FRBirthday, FRIDNum, FRAddress, FRImgName, FRJobNum, FRDepartment, FRRemark, FRUpdateTime) values('"+Guid+"','"+Name+"',1,'"+Birthday+"','"+regInfo.szIDNum+"','"+regInfo.szAddress+"','"+regInfo.szImgName+"','"+regInfo.szJobNum+"','"+regInfo.szDept+"','"+regInfo.szRemark+"','"+DBTime+"');");

数据库执行变量操作

query.exec(QString("delete from information where name='%1'").arg(name_del)); 
1可以在query.exec前,声明QString sqlquery;将带变量的sql语句放入sqlquery中
sqlquery=QObject::tr("insert into table values('%1','%2')").arg(value1,value2);
执行query.exec(sqlquery);就可以了

修改QDateEdit的值


QString str=m_pRegInfo->dBirthday.toString("yyyy-MM-dd");//m_pRegInfo->dBirthday为QDateTime类型的值

QStringList strList=str.split("-");

 QDate day(strList[0].toInt(),strList[1].toInt(),strList[2].toInt());

ui.birthDateEdit->setDate(day);

你可能感兴趣的:(Qt之日常积累)