📄 modifydialog.cpp
字号:
#include "modifydialog.h"#include <qcombobox.h>#include <qheader.h>#include <qlabel.h>#include <qlistview.h>#include <qmultilineedit.h>#include <qpushbutton.h>#include <qlayout.h>#include <qvariant.h>#include <qtooltip.h>#include <qwhatsthis.h>#include <sqlite3.h>#include <qmessagebox.h>#include <qstring.h>/* * Constructs a ModifyDialog which is a child of 'parent', with the * name 'name' and widget flags set to 'f' * * The dialog will by default be modeless, unless you set 'modal' to * TRUE to construct a modal dialog. */ModifyDialog::ModifyDialog( QWidget* parent, const char* name, bool modal, WFlags fl ) : ModifyDialogForm( parent, name, modal, fl ){ pDB = NULL; errMsg = NULL; int rc = sqlite3_open("test.db", &pDB ); tableListView -> setSorting( -1, FALSE ); addToComBoxAndListView(); connect( cancelButton, SIGNAL( clicked() ), this, SLOT( close() ) ); connect( deleteButton, SIGNAL( clicked() ), this, SLOT( deleteSlot() ) ); connect( deleteTableButton, SIGNAL( clicked()), this, SLOT( deleteTableSlot() ) ); connect( updateButton, SIGNAL( clicked() ), this, SLOT( updateSlot() ) ); }/* * Destroys the object and frees any allocated resources */ModifyDialog::~ModifyDialog(){ // no need to delete child widgets, Qt does it all for us}void ModifyDialog::addToComBoxAndListView(){ char **tableNames; int rows; int columns; int rc = sqlite3_get_table( pDB, "select tableName from tableField where property == 1", &tableNames, &rows, &columns, &errMsg ); if( rc == SQLITE_OK ) { addToTableComboBox( tableNames, rows,columns ); addToTableListView( tableNames, rows,columns ); } else { QMessageBox::warning( this, "Add Table", "Add table and field failure!" ) ; }}void ModifyDialog::addToTableComboBox( char **tableNames, int rows, int columns ){ QString tableStr; tableComboBox -> clear(); //add for( int i = 1; i < ( rows + 1 ) * columns; i++ ) { tableStr = tableNames[i]; tableComboBox -> insertItem( tableStr ); } tableStr = tableNames[1]; }void ModifyDialog::addToTableListView( char **tableNames, int rows, int columns ){ char **fieldNames; int row; int column; tableListView -> clear(); //add for( int i = ( ( rows + 1 ) * columns - 1 ); i >= 1; i-- ) { QString cmd(""); QString tableName( tableNames[i] ); cmd = "select fieldName from tableField where tableName == '" + tableName + "'"; int rc = sqlite3_get_table( pDB, cmd, &fieldNames, &row, &column, &errMsg ); if( rc == SQLITE_OK ) { addTableAndField( fieldNames, row, column ); } else { QMessageBox::warning( this, "Add Table", "Add table to list view failure!" ); } }}void ModifyDialog::addTableAndField( char **fieldNames, int rows, int columns ){ QListViewItem *itemTable = new QListViewItem( tableListView, 0 ); QListViewItem *itemField; QString text; text = fieldNames[1]; itemTable -> setText( 0, text ); for( int i = ( ( rows + 1 ) * columns - 1 ) ; i >= 2 ; i-- ) { itemTable -> setOpen( TRUE ); itemField = new QListViewItem( itemTable, 0 ); text = fieldNames[i]; itemField -> setText( 0, text ); } }QString ModifyDialog::getDeleteCmd(){ int numLines = getMultiEditLines( deleteMultiEdit ); QString deleteStr; QString deleteCmd; for( int i = 0; i < numLines; i++ ) { if( i != ( numLines - 1 ) ) { deleteStr += deleteMultiEdit -> textLine( i ) +" and "; } else { deleteStr += deleteMultiEdit -> textLine( i ); } } if( deleteStr == "" ) { deleteCmd = "delete from " + tableComboBox -> currentText(); } else { deleteCmd = "delete from " + tableComboBox -> currentText() + " where " + deleteStr; } return deleteCmd;}int ModifyDialog::getMultiEditLines( QMultiLineEdit *multiEdit ){ int numLines = multiEdit -> numLines(); while( numLines > 0 && multiEdit -> textLine( numLines - 1 ) == "" ) { numLines = numLines - 1; } return numLines;}void ModifyDialog::deleteSlot(){ if( ( QMessageBox::information( this, "Delete", " Do you really want to" " delete these material", QMessageBox::No, QMessageBox::Yes ) ) == QMessageBox::Yes) { int rc = sqlite3_exec( pDB, getDeleteCmd(), 0, 0, &errMsg ); if( rc == SQLITE_OK ) { QMessageBox::information( this, "Delete", "Delete successful!"); } else { if( (QMessageBox::warning( this, "Delete", "Delete failed!\n" "Do you want to try again?", QMessageBox::No, QMessageBox::Yes) ) == QMessageBox::No) close(); } }}void ModifyDialog::deleteTableFromTableField(){ QString cmd; cmd = "delete from tableField where tableName == '" + tableComboBox -> currentText() + "'"; int rc = sqlite3_exec( pDB, cmd, 0, 0, &errMsg );} QString ModifyDialog::getDeleteTableCmd(){ QString deleteTableCmd; deleteTableCmd = "drop table " + tableComboBox -> currentText(); return deleteTableCmd; }void ModifyDialog::deleteTableSlot(){ if( ( QMessageBox::information( this, "Delete", " Do you really want to" " delete these table", QMessageBox::No, QMessageBox::Yes ) ) == QMessageBox::Yes) { int rc = sqlite3_exec( pDB, getDeleteTableCmd(), 0, 0, &errMsg ); if( rc == SQLITE_OK ) { deleteTableFromTableField(); addToComBoxAndListView(); QMessageBox::information( this, "Delete Table", "Delete table successful!"); } else { if( (QMessageBox::warning( this, "Delete Table", "Delete table failed!\n" "Do you want to try again?", QMessageBox::No, QMessageBox::Yes) ) == QMessageBox::No) close(); } }}QString ModifyDialog::getUpdateCmd(){ QString assignStr; QString conditionStr; QString updateCmd( "" ); int numLines = getMultiEditLines( updateAssignMultiEdit ); for( int i = 0; i < numLines; i++ ) { if( i != ( numLines - 1 ) ) { assignStr += updateAssignMultiEdit -> textLine( i ) +","; } else { assignStr += updateAssignMultiEdit -> textLine( i ); } } numLines = getMultiEditLines( updateConditionMultiEdit ); for( int i = 0; i < numLines; i++ ) { if( i != ( numLines - 1 ) ) { conditionStr += updateConditionMultiEdit -> textLine( i ) +" and "; } else { conditionStr += updateConditionMultiEdit -> textLine( i ); } } if( assignStr == "" ) { updateCmd = "update"; return updateCmd; } else { if( conditionStr == "" ) { updateCmd = "update " + tableComboBox -> currentText() + " set " + assignStr; } else { updateCmd = "update " + tableComboBox -> currentText() + " set " + assignStr + " where " + conditionStr; } return updateCmd; }}void ModifyDialog::updateSlot(){ int rc = sqlite3_exec( pDB, getUpdateCmd(), 0, 0, &errMsg ); if( rc == SQLITE_OK ) { QMessageBox::information( this, "Update Table", "Update successful!" ); } else { if( (QMessageBox::warning( this, "Update Table", "Update table failed!\n" "What you input is wrong.\n" "Do you want to try again?", QMessageBox::No, QMessageBox::Yes) ) == QMessageBox::No ) close(); } }void ModifyDialog::closeEvent( QCloseEvent *event ){ int rc = sqlite3_close( pDB ); if( rc == SQLITE_OK ) { event -> accept(); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -