基于qt的對話框錄入界面本科畢業(yè)論文_第1頁
基于qt的對話框錄入界面本科畢業(yè)論文_第2頁
基于qt的對話框錄入界面本科畢業(yè)論文_第3頁
基于qt的對話框錄入界面本科畢業(yè)論文_第4頁
基于qt的對話框錄入界面本科畢業(yè)論文_第5頁
已閱讀5頁,還剩11頁未讀 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

實驗報告課程名稱軟件案例分析實驗名稱基于對話框的錄入界面班級學號姓名成績指導教師實驗日期實驗?zāi)康牧私釷t數(shù)據(jù)庫訪問技術(shù)(加載數(shù)據(jù)庫驅(qū)動)。掌握QSqlDatabase類和QSqlQuery類的使用。了解熟悉QtCreator開發(fā)環(huán)境及相關(guān)控件的使用。了解信號和槽的關(guān)聯(lián)及作用。掌握基于對話框的用戶界面設(shè)計方法和數(shù)據(jù)操作方法。掌握主窗口和子窗口之間數(shù)據(jù)訪問方法。實驗環(huán)境QtCreator+MinGW4.9(32-bit),Windows7,4G內(nèi)存,DELL筆記本一臺。實驗要求實現(xiàn)對人事(或其他)基本信息的增加、編輯、刪除、刷新和導出功能。參考運行界面如圖所示。運行界面友好簡潔。主界面“增加個人基本信息”對話框“修改個人基本信息”對話框?qū)嶒灢襟E及代碼(1)啟動QtCreator,創(chuàng)建基于部件框的QtWidgetsApplication類型的應(yīng)用程序,命名為Widget。(2)修改對話框標題為“人事基本信息管理”。在主界面窗口中添加6個按鈕控件,一個TableWidget控件,界面設(shè)計如圖所示。在主界面設(shè)置6個PushButton控件,設(shè)置text文本為“刷新”,“導出”,“增加”,“編輯”,“刪除”,“退出”,編輯TableWidget的條目,分別為“序號”,“姓名”,“性別”,“出生日期”,“電話”,“最后學位”,“畢業(yè)學?!保爱厴I(yè)時間”,“備注”。向工程中添加Qt設(shè)計師界面類(模板為Widget)子對話框資源,命名為“Add”,向界面添加Label,LineEdit,DateEdit,ConboBox等控件若干,界面設(shè)計如圖。在Widget.h添加如下代碼://聲明信號signals:voidsendStr(QString,QString,QString,QString,QString,QString,QString,QString,QString,int);//聲明槽privateslots:voidon_pushButton_clicked();voidon_tableWidget_cellDoubleClicked(introw,intcolumn);voidon_pushButton_4_clicked();voidon_tableWidget_cellClicked(introw,intcolumn);voidon_pushButton_3_clicked();voidon_pushButton_5_clicked();voidon_pushButton_2_clicked();private:Ui::Widget*ui;intr;Add*add;//增加界面對象QSqlDatabasedb;public:voidcreateConnection();//創(chuàng)建連接voidinit();voidsetCW();voidTable2Excel(QTableWidget*table,QStringtitle);//導出函數(shù)protected:voidcontextMenuEvent(QContextMenuEvent*);//增加右擊菜單創(chuàng)建連接連接數(shù)據(jù)庫,其函數(shù)代碼如下:voidWidget::createConnection(){db=QSqlDatabase::addDatabase("QMYSQL");db.setHostName("localhost");db.setPort(3306);db.setDatabaseName("qt");db.setUserName("root");db.setPassword("121819");if(!db.open()){QMessageBox::warning(this,"警告","數(shù)據(jù)庫連接失敗?。。?);exit(0);return;}}初始化函數(shù)代碼如下(主要為查詢數(shù)據(jù)庫代碼):voidWidget::init(){QSqlQueryquery("select*fromrenshi");while(query.next()){QStringListlist;list<<query.value(0).toString()<<query.value(1).toString()<<query.value(2).toString()<<query.value(3).toString()<<query.value(4).toString()<<query.value(5).toString()<<query.value(6).toString()<<query.value(7).toString()<<query.value(8).toString();intrownum=ui->tableWidget->rowCount();ui->tableWidget->insertRow(rownum);ui->tableWidget->setItem(rownum,0,newQTableWidgetItem(list.at(0)));ui->tableWidget->setItem(rownum,1,newQTableWidgetItem(list.at(1)));ui->tableWidget->setItem(rownum,2,newQTableWidgetItem(list.at(2)));ui->tableWidget->setItem(rownum,3,newQTableWidgetItem(list.at(3)));ui->tableWidget->setItem(rownum,4,newQTableWidgetItem(list.at(4)));ui->tableWidget->setItem(rownum,5,newQTableWidgetItem(list.at(5)));ui->tableWidget->setItem(rownum,6,newQTableWidgetItem(list.at(6)));ui->tableWidget->setItem(rownum,7,newQTableWidgetItem(list.at(7)));ui->tableWidget->setItem(rownum,8,newQTableWidgetItem(list.at(8)));}}在構(gòu)造函數(shù)中添加如下代碼:ui->setupUi(this);add=newAdd;connect(this,SIGNAL(sendStr(QString,QString,QString,QString,QString,QString,QString,QString,QString,int)),add,SLOT(recvStr(QString,QString,QString,QString,QString,QString,QString,QString,QString,int)));connect(add,SIGNAL(senMsg()),this,SLOT(on_pushButton_clicked()));setCW();//去掉默認行號QHeaderView*headerView=ui->tableWidget->verticalHeader();headerView->setHidden(true);createConnection();init();r=-1;導出函數(shù),代碼如下:voidWidget::Table2Excel(QTableWidget*table,QStringtitle){QStringfileName=QFileDialog::getSaveFileName(table,"保存",QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation),"Excel文件(*.xls*.xlsx)");if(fileName!=""){QAxObject*excel=newQAxObject;if(excel->setControl("Excel.Application"))//連接Excel控件{excel->dynamicCall("SetVisible(boolVisible)","false");//不顯示窗體excel->setProperty("DisplayAlerts",false);//不顯示任何警告信息。如果為true那么在關(guān)閉是會出現(xiàn)類似“文件已修改,是否保存”的提示QAxObject*workbooks=excel->querySubObject("WorkBooks");//獲取工作簿集合workbooks->dynamicCall("Add");//新建一個工作簿QAxObject*workbook=excel->querySubObject("ActiveWorkBook");//獲取當前工作簿QAxObject*worksheet=workbook->querySubObject("Worksheets(int)",1);inti,j,colcount=table->columnCount();QAxObject*cell,*col;//標題行cell=worksheet->querySubObject("Cells(int,int)",1,1);cell->dynamicCall("SetValue(constQString&)",title);cell->querySubObject("Font")->setProperty("Size",18);//調(diào)整行高worksheet->querySubObject("Range(constQString&)","1:1")->setProperty("RowHeight",30);//合并標題行QStringcellTitle;cellTitle.append("A1:");cellTitle.append(QChar(colcount-1+'A'));cellTitle.append(QString::number(1));QAxObject*range=worksheet->querySubObject("Range(constQString&)",cellTitle);range->setProperty("WrapText",true);range->setProperty("MergeCells",true);range->setProperty("HorizontalAlignment",-4108);//xlCenterrange->setProperty("VerticalAlignment",-4108);//xlCenter//列標題for(i=0;i<colcount;i++){QStringcolumnName;columnName.append(QChar(i+'A'));columnName.append(":");columnName.append(QChar(i+'A'));col=worksheet->querySubObject("Columns(constQString&)",columnName);col->setProperty("ColumnWidth",table->columnWidth(i)/6);cell=worksheet->querySubObject("Cells(int,int)",2,i+1);columnName=table->horizontalHeaderItem(i)->text();cell->dynamicCall("SetValue(constQString&)",columnName);cell->querySubObject("Font")->setProperty("Bold",true);cell->querySubObject("Interior")->setProperty("Color",QColor(191,191,191));cell->setProperty("HorizontalAlignment",-4108);//xlCentercell->setProperty("VerticalAlignment",-4108);//xlCenter}//數(shù)據(jù)區(qū)for(i=0;i<table->rowCount();i++){for(j=0;j<colcount;j++){worksheet->querySubObject("Cells(int,int)",i+3,j+1)->dynamicCall("SetValue(constQString&)",table->item(i,j)?table->item(i,j)->text():"");}}//畫框線QStringlrange;lrange.append("A2:");lrange.append(colcount-1+'A');lrange.append(QString::number(table->rowCount()+2));range=worksheet->querySubObject("Range(constQString&)",lrange);range->querySubObject("Borders")->setProperty("LineStyle",QString::number(1));range->querySubObject("Borders")->setProperty("Color",QColor(0,0,0));//調(diào)整數(shù)據(jù)區(qū)行高QStringrowsName;rowsName.append("2:");rowsName.append(QString::number(table->rowCount()+2));range=worksheet->querySubObject("Range(constQString&)",rowsName);range->setProperty("RowHeight",20);workbook->dynamicCall("SaveAs(constQString&)",QDir::toNativeSeparators(fileName));//保存至fileNameworkbook->dynamicCall("Close()");//關(guān)閉工作簿excel->dynamicCall("Quit()");//關(guān)閉exceldeleteexcel;excel=NULL;if(QMessageBox::question(NULL,"完成","文件已經(jīng)導出,是否現(xiàn)在打開?",QMessageBox::Yes|QMessageBox::No)==QMessageBox::Yes){QDesktopServices::openUrl(QUrl("file:///"+QDir::toNativeSeparators(fileName)));}}else{QMessageBox::warning(NULL,"錯誤","未能創(chuàng)建Excel對象,請安裝MicrosoftExcel。",QMessageBox::Apply);}}}右鍵菜單函數(shù),代碼如下:voidWidget::contextMenuEvent(QContextMenuEvent*){QMenu*menu=newQMenu(this);QAction*action=newQAction(this);QAction*action1=newQAction(this);QAction*action2=newQAction(this);QAction*action3=newQAction(this);QAction*action4=newQAction(this);QAction*action5=newQAction(this);action->setText(tr("刷新"));action1->setText(tr("導出"));action2->setText(tr("增加"));action3->setText(tr("編輯"));action4->setText(tr("刪除"));action5->setText(tr("退出"));connect(action,SIGNAL(triggered(bool)),this,SLOT(on_pushButton_clicked()));connect(action1,SIGNAL(triggered(bool)),this,SLOT(on_pushButton_2_clicked()));connect(action2,SIGNAL(triggered(bool)),this,SLOT(on_pushButton_3_clicked()));connect(action3,SIGNAL(triggered(bool)),this,SLOT(on_pushButton_4_clicked()));connect(action4,SIGNAL(triggered(bool)),this,SLOT(on_pushButton_5_clicked()));connect(action5,SIGNAL(triggered(bool)),this,SLOT(close()));menu->addAction(action);menu->addSeparator();menu->addAction(action1);menu->addSeparator();menu->addAction(action2);menu->addSeparator();menu->addAction(action3);menu->addSeparator();menu->addAction(action4);menu->addSeparator();menu->addAction(action5);menu->addSeparator();menu->exec(QCursor::pos());}雙擊修改函數(shù)和6個PushButton添加信號對應(yīng)的槽(類似消息響應(yīng)函數(shù)),代碼如下:voidWidget::on_pushButton_clicked(){ui->tableWidget->setRowCount(0);ui->tableWidget->clearContents();init();}voidWidget::on_tableWidget_cellDoubleClicked(introw,intcolumn){emitsendStr(ui->tableWidget->item(row,0)->text(),ui->tableWidget->item(row,1)->text(),ui->tableWidget->item(row,2)->text(),ui->tableWidget->item(row,3)->text(),ui->tableWidget->item(row,4)->text(),ui->tableWidget->item(row,5)->text(),ui->tableWidget->item(row,6)->text(),ui->tableWidget->item(row,7)->text(),ui->tableWidget->item(row,8)->text(),2);add->setWindowTitle("修改個人基本信息");add->show();}voidWidget::on_pushButton_4_clicked(){if(r==-1){QMessageBox::warning(this,"警告","請選擇要修改的行!");return;}on_tableWidget_cellDoubleClicked(r,0);}voidWidget::on_tableWidget_cellClicked(introw,intcolumn){r=row;}voidWidget::on_pushButton_3_clicked(){add->show();emitsendStr(QString::number(ui->tableWidget->rowCount()+1),"","","","","","","","",1);add->setWindowTitle("增加個人基本信息");}voidWidget::on_pushButton_5_clicked(){if(r==-1){QMessageBox::warn

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論