📄 insertdialog.cpp
字号:
#include "insertdialog.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 <qstring.h>#include <qmessagebox.h>#include <sqlite3.h>/* * Constructs a InsertDialog 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. */InsertDialog::InsertDialog( QWidget* parent, const char* name, bool modal, WFlags fl ) : InsertDialogForm( parent, name, modal, fl ){ pDB = NULL; errMsg = NULL; int rc = sqlite3_open("test.db", &pDB ); tableListView -> setSorting( -1, FALSE ); addToComboBoxAndListView(); connect( cancelButton, SIGNAL( clicked()), this, SLOT( close() )); connect( okButton, SIGNAL( clicked()), this, SLOT( insertValuesSlot() ) );} /* * Destroys the object and frees any allocated resources */InsertDialog::~InsertDialog(){ // no need to delete child widgets, Qt does it all for us}void InsertDialog::addToComboBoxAndListView(){ 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 ) { addTableToComboBox( tableNames, rows,columns ); addTableToListView( tableNames, rows,columns ); } else { QMessageBox::warning( this, "Add Table", "Add table to ComboBox failure!" ) ; }}void InsertDialog::addTableToComboBox( char **tableNames, int rows, int columns ){ QString tableStr; for( int i = 1; i < ( rows + 1 ) * columns; i++ ) { tableStr = tableNames[i]; tableComboBox -> insertItem( tableStr ); }}void InsertDialog::addTableToListView( char **tableNames, int rows, int columns ){ char **fieldNames; int fieldRows; int fieldColumns; 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, &fieldRows, &fieldColumns, &errMsg ); if( rc == SQLITE_OK ) { addTableAndField( fieldNames, fieldRows, fieldColumns ); } else { QMessageBox::warning( this, "Add to List View", "Add table to ListView failure!" ); } }}void InsertDialog::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 InsertDialog::getInsertCmd(){ int numLines = getMultiEditLines( fieldMultiEdit ); QString insertCmd; QString values; for( int i = 0; i < numLines; i++ ) { if( i != ( numLines - 1 ) ) { values += fieldMultiEdit -> textLine( i ) +","; } else { values += fieldMultiEdit -> textLine( i ); } } insertCmd = "insert into " + tableComboBox -> currentText() + " values" + "(" + values + ")"; return insertCmd;}int InsertDialog::getMultiEditLines( QMultiLineEdit *multiEdit ){ int numLines = multiEdit -> numLines(); while( numLines > 0 && multiEdit -> textLine( numLines - 1 ) == "" ) { numLines = numLines - 1; } return numLines;}void InsertDialog::insertValuesSlot(){ int rc = sqlite3_exec( pDB, getInsertCmd(), 0, 0, &errMsg ); if( rc == SQLITE_OK ) { if( QMessageBox::information(this, "Insert Values", "Insert into table successful!\n" "Do you want to continue to insert?", QMessageBox::No, QMessageBox::Yes) == QMessageBox::No ) close(); else { fieldMultiEdit -> clear(); } } else { if( QMessageBox::warning( this, "Insert Table", "What you write is wrong,\n" "Unable to insert into table,\n" "Do you want to retry?", QMessageBox::Cancel, QMessageBox::Retry) == QMessageBox::Cancel ) close(); }}void InsertDialog::closeEvent( QCloseEvent *event ){ int rc = sqlite3_close( pDB ); if( rc == SQLITE_OK ) { event -> accept(); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -