⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 selectdialog.cpp

📁 本代码采用嵌入式qt编写
💻 CPP
字号:
#include "selectdialog.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 <qimage.h>#include <qpixmap.h>#include <sqlite3.h>#include <qmessagebox.h>#include <qstring.h>/*  *  Constructs a SelectDialog 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. */SelectDialog::SelectDialog( QWidget* parent,  const char* name, bool modal, WFlags fl )    : SelectDialogForm( parent, name, modal, fl ){    pDB = NULL;    errMsg = NULL;    int rc = sqlite3_open("test.db", &pDB );    tableListView -> setSorting( -1, FALSE );    fieldListView -> setSorting( -1, FALSE );        addToComboBoxAndListView();        connect( okButton, SIGNAL( clicked() ), this, SLOT( selectTableSlot() ) );    connect( cancelButton, SIGNAL( clicked() ), this, SLOT( close() ) );    connect( addToButton, SIGNAL( clicked() ), this, SLOT( addToSlot() ) );    connect( deleteButton, SIGNAL( clicked() ), this, SLOT( deleteSlot() ) );    connect( tableComboBox, SIGNAL( activated ( const QString & )  ), this, SLOT( addToOrderByComboBox( const QString & ) ) );     }/*   *  Destroys the object and frees any allocated resources */SelectDialog::~SelectDialog(){    // no need to delete child widgets, Qt does it all for us}void SelectDialog::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 ) {	 addToTableComboBox( tableNames, rows,columns );	 addToTableListView( tableNames, rows,columns );     }     else {	 QMessageBox::warning( this, "Add Table", 			       "Add table and field failure!" ) ;     }}void SelectDialog::addToTableComboBox( char **tableNames, int rows, int columns ){    QString tableStr;    for( int i = 1; i < ( rows + 1 ) * columns; i++ )    {	tableStr = tableNames[i];	tableComboBox -> insertItem( tableStr );    }        tableStr = tableNames[1];    addToOrderByComboBox( tableStr );}void SelectDialog::addToOrderByComboBox( const QString &tableName ){    char **fieldName;    int rows;    int columns;    QString fieldStr;        QString cmd = "select fieldName from tableField where tableName == '" + tableName + "'";    int rc = sqlite3_get_table( pDB, cmd, &fieldName, &rows, &columns, &errMsg );    if( rc == SQLITE_OK ) {	orderByComboBox -> clear();	orderByComboBox -> insertItem( "" );	for( int i = 2; i < ( rows + 1 ) * columns; i++ )	{	    fieldStr = fieldName[i];	    orderByComboBox -> insertItem( fieldStr );	}    }    else {	QMessageBox::warning( this, "ComboBox", 			      "unable to insert into order by combo box!" );    }}void SelectDialog::addToTableListView( 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 Table",				  "Add table to list view failure!" );	}    }}void SelectDialog::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 );    }           itemTable -> setOpen( TRUE );    itemField = new QListViewItem( itemTable, 0 );    itemField -> setText( 0, "*" );    }void SelectDialog::addToSlot(){    QListViewItemIterator it( tableListView );    while ( it.current() ) {            QListViewItem *item = it.current();	    if( item -> isSelected() ) {		QListViewItem *newItem = new QListViewItem( fieldListView, 0 );		newItem -> setOpen( TRUE );		newItem -> setText(0, item -> text(0) );		break;	    }		            ++it;	}}void SelectDialog::deleteSlot(){    fieldListView -> takeItem( fieldListView -> currentItem() );}QString SelectDialog::getSelectCmd(){    QString fieldStr;    QString conditionStr;    QListViewItemIterator it( fieldListView );        for( int i = 0; it.current(); i++, it++ )     {		QListViewItem *item = it.current();	if( i == 0 ) {	    fieldStr = item -> text( 0 );	}	else {	    fieldStr = fieldStr + "," + item -> text( 0 );	}    }        int numLines =  getMultiEditLines( conditionMultiEdit );          for( int i = 0; i < numLines; i++ )    {	if( i != ( numLines - 1 )  ) {	    conditionStr += conditionMultiEdit -> textLine( i ) +" and ";	}	else {	    conditionStr += conditionMultiEdit -> textLine( i );	}    }        return selectCmd( fieldStr,conditionStr );    }QString SelectDialog::selectCmd( QString fieldStr, QString conditionStr ){    QString selectCmd;    if( ( orderByComboBox -> currentText() )== "" && conditionStr == "" ) {	selectCmd = "select " + fieldStr + " from " + 		    tableComboBox -> currentText();		        }    else if(( orderByComboBox -> currentText() )== "" && conditionStr != "") {	selectCmd = "select " + fieldStr + " from " + tableComboBox -> currentText() 		    + " where " + conditionStr;		       }    else if( ( orderByComboBox -> currentText() ) != "" && conditionStr == "" ) {	selectCmd = "select " + fieldStr + " from " + tableComboBox -> currentText() 		    + " order by " + orderByComboBox -> currentText()		    + "  " + orderComboBox -> currentText();     }    else {	selectCmd = "select " + fieldStr + " from " + tableComboBox -> currentText() 		    + " where " + conditionStr + " order by " +		    orderByComboBox -> currentText() + "  " + orderComboBox -> currentText();     }        return selectCmd;    }int SelectDialog::getMultiEditLines( QMultiLineEdit *multiEdit ){   int numLines = multiEdit -> numLines();   while( numLines > 0 && multiEdit -> textLine( numLines - 1 ) == "" )   {       numLines = numLines - 1;    }   return numLines;}void SelectDialog::selectTableSlot(){    char **selectResult;    int rows;    int columns;    QString cmdStr = getSelectCmd();    int rc = sqlite3_get_table( pDB, cmdStr, &selectResult, &rows, &columns, &errMsg );    if( rc == SQLITE_OK ) {	QMessageBox::information( this, "Select Table",				  "select successful!");	emit selectOk( selectResult, rows, columns );    }    else {	if( (QMessageBox::warning( this, "Select Warning",				   "What you put is:\n" 				   + cmdStr + "\n"				   					  "Please input select field.\n"				   "Do you want to try again?", 			      QMessageBox::No, QMessageBox::Yes )) 	    == QMessageBox::No )	    close();    }}void SelectDialog::closeEvent( QCloseEvent *event ){    int rc = sqlite3_close( pDB );    if( rc == SQLITE_OK ) {	event -> accept();    }    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -