Qt常用片段代码库

1.Qt中QString,int,char,QByteArray之间相互转换

2.QT获取系统当前时间

[cpp]  view plain  copy
 print ?
  1. #include <QtCore/QDateTime>  
  2.   
  3. QDateTime dt;    
  4. QTime time;    
  5. QDate date;    
  6. dt.setTime(time.currentTime());    
  7. dt.setDate(date.currentDate());    
  8. //自定义格式  
  9. QString currentDate = dt.toString("yyyy-MM-dd hh:mm");   

3.QT写文本文件(QTextStream 写文件)

[cpp]  view plain  copy
 print ?
  1. #include <QtCore/QTextStream>  
  2. #include <QtCore/QFile>  
  3. #include <QtCore/QIODevice>  
  4.   
  5. QString sFilePath = "C:\\test.txt";    
  6. QFile file(sFilePath);    
  7. //方式:Append为追加,WriteOnly,ReadOnly    
  8. if (!file.open(QIODevice::WriteOnly|QIODevice::Text)) {      
  9.     QMessageBox::critical(NULL, "提示""无法创建文件");    
  10.     return;      
  11. }      
  12. QTextStream out(&file);      
  13. out<<"要写入内容"<<endl;      
  14. out.flush();      
  15. file.close();      
  16. //注意写入方式的选择,注意写完后的关闭操作!  

4.选择保存目录对话框

[cpp]  view plain  copy
 print ?
  1. QString saveDir = QFileDialog::getExistingDirectory(NULL, this,"保存目录""选择目录"true);  


5.分割字符串

[cpp]  view plain  copy
 print ?
  1. QString str = "a,,b,c";  
  2. QStringList list = str.split(",", QString::SkipEmptyParts);  
  3. // list: [ "a", "b", "c" ]  
  4. for (int i = 1;i<list.length();i++)  
  5. {  
  6. }  


6.QPushButton 和 QToolButton

[cpp]  view plain  copy
 print ?
  1. QPushButton* pushBtn = new QPushButton("按钮1名称");  
  2.   
  3. QToolButton* toolBtn = new QToolButton;  
  4. toolBtn->setToolTip("按钮2说明");  
  5. toolBtn->setIcon(QIcon(":/icons/btn.png"));  
  6. connect(toolBtn,SIGNAL(clicked()),this,SLOT(OnBtnClicked()));  


7.科学技术法-正则表达式

[cpp]  view plain  copy
 print ?
  1. //正则表达式-科学技术法 支持:-1.5e+99  
  2. const QString RegExp_REAL = "[+-]?[\\d]+([\\.][\\d]*)?([Ee][+-]?[0-9]{0,2})?";  
  3. //正则表达式-整数类型  
  4. const QString REGEXP_INTEGER = "^-?[0-9]+$";  
  5. QLineEdit* lineEdit =  new QLineEdit;  
  6. QRegExp regx(RegExp_REAL);  
  7. QValidator *validator = new QRegExpValidator(regx, lineEdit);  
  8. lineEdit->setValidator(validator);   


8.QMap遍历

[cpp]  view plain  copy
 print ?
  1. //QMap<QString, QString> map;  
  2. QMap<QString, QString>::const_iterator it = map.constBegin();  
  3. while (it != map.constEnd()) {  
  4.     QString value = it.value();  
  5.     //...  
  6.     ++it;  
  7. }  

9.设置界面风格

[cpp]  view plain  copy
 print ?
  1. //设置界面风格  
  2. QWidget* widget = new QWidget;  
  3. //window风格  
  4. widget->setStyle(new QWindowsStyle());  
  5. //Mac风格  
  6. widget->setStyle(new QMacStyle());  

10.Qt 已默认方式打开所有文件ShellExecuteA

[cpp]  view plain  copy
 print ?
  1. QString sParamFilePath = "文件路径";  
  2.             sParamFilePath.replace("/","\\");    
  3.             ShellExecuteA(NULL,"open",""+sParamFilePath,NULL,NULL,SW_SHOW);  



11.QTableView实现同时删除被选中的多行记录


[cpp]  view plain  copy
 print ?
  1. QItemSelectionModel *selections = matrixTable->selectionModel();    
  2. QModelIndexList selected = selections->selectedIndexes();   
  3. QMap<intint> rowMap;   
  4. foreach (QModelIndex index, selected)    
  5. {    
  6.     rowMap.insert(index.row(), 0);    
  7. }  
  8.   
  9. QMapIterator<intint> rowMapIterator(rowMap);    
  10. rowMapIterator.toBack();    
  11. while (rowMapIterator.hasPrevious())    
  12. {     
  13.     rowMapIterator.previous();    
  14.     int rowToDel = rowMapIterator.key();    
  15.     m_matrixModel->removeRow(rowToDel);    
  16. }  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值