- UI
- 关键代码 mainwindow.h
1 QStringListModel *slm; 2 QStringList *sl;
mainwindow.cpp init初始化
1 //初始化combox 2 ui->comboBox->insertItem(0,"山羊座","山羊座1"); 3 ui->comboBox->insertItem(1,"处女座","处女座"); 4 ui->comboBox->insertItem(2,"双鱼座","双鱼座1"); 5 ui->comboBox->insertItem(3,"狮子座","狮子座1"); 6 7 this->sl = new QStringList(); 8 this->sl->append("中文数据测试123a"); 9 this->slm=new QStringListModel(this); 10 this->slm->setStringList(*sl); 11 ui->listView->setModel(slm);
click事件
1 void MainWindow::on_pushButton_clicked() 2 { 3 this->sl->clear(); 4 5 QString qstr = ui->comboBox->currentText(); 6 ui->lineEdit->setText(qstr); 7 8 qDebug()<<qstr; 9 //转换为C风格的字符串(字符串严格转换) 10 const char *psearch = qstr.toStdString().c_str(); 11 qDebug()<<psearch; 12 13 ifstream fin("C:\nasa.txt"); 14 15 if(!fin) 16 { 17 qDebug()<<"文件打开失败"; 18 return; 19 } 20 else 21 { 22 qDebug()<<"文件打开成功"; 23 } 24 25 //用于读取文件 26 char strall[3001]={0}; 27 //设置读取个数 28 int read_count=0; 29 while(!fin.eof()) 30 { 31 read_count++; 32 33 if(read_count>15000) 34 { 35 qDebug()<<strall<<endl; 36 break; 37 } 38 39 //读取 40 memset(strall,0,sizeof(strall)); 41 fin.getline(strall,3000); 42 43 //设置编码方式,解决乱码问题 44 QTextCodec *codec = QTextCodec::codecForName("gb2312"); 45 QString qstrall = codec->toUnicode(strall); 46 47 //转化为C风格 48 const char *pstrall = qstrall.toStdString().c_str(); 49 char *pfind = strstr(pstrall,psearch); 50 51 if(pfind!=NULL) 52 { 53 qDebug()<<pstrall; 54 //添加到列表 55 sl->append(qstrall); 56 } 57 } 58 59 this->slm->setStringList(*sl); 60 ui->listView->setModel(slm); 61 }
comboBox_currentIndexChanged
1 void MainWindow::on_comboBox_currentIndexChanged(int index) 2 { 3 QString qstr = ui->comboBox->currentText(); 4 ui->lineEdit->setText(qstr); 5 }