📄 q3datatable.cpp
字号:
d->cur.setSort( sort );}/*! \overload Sets the sort to be applied to the displayed data to \a sort. If there is no current cursor, nothing happens. A QSqlIndex contains field names and their ordering (ASC or DESC); these are used to compose the ORDER BY clause. \sa sort()*/void Q3DataTable::setSort( const QSqlIndex& sort ){ d->cur.setSort( sort );}QStringList Q3DataTable::sort() const{ return d->cur.sort();}/*! Returns the cursor used by the data table.*/Q3SqlCursor* Q3DataTable::sqlCursor() const{ return d->cur.cursor();}void Q3DataTable::setConfirmEdits( bool confirm ){ d->dat.setConfirmEdits( confirm );}void Q3DataTable::setConfirmInsert( bool confirm ){ d->dat.setConfirmInsert( confirm );}void Q3DataTable::setConfirmUpdate( bool confirm ){ d->dat.setConfirmUpdate( confirm );}void Q3DataTable::setConfirmDelete( bool confirm ){ d->dat.setConfirmDelete( confirm );}/*! \property Q3DataTable::confirmEdits \brief whether the data table confirms edit operations If the confirmEdits property is true, the data table confirms all edit operations (inserts, updates and deletes). Finer control of edit confirmation can be achieved using \l confirmCancels, \l confirmInsert, \l confirmUpdate and \l confirmDelete. \sa confirmCancels() confirmInsert() confirmUpdate() confirmDelete()*/bool Q3DataTable::confirmEdits() const{ return ( d->dat.confirmEdits() );}/*! \property Q3DataTable::confirmInsert \brief whether the data table confirms insert operations If the confirmInsert property is true, all insertions must be confirmed by the user through a message box (this behavior can be changed by overriding the confirmEdit() function), otherwise all insert operations occur immediately. \sa confirmCancels() confirmEdits() confirmUpdate() confirmDelete()*/bool Q3DataTable::confirmInsert() const{ return ( d->dat.confirmInsert() );}/*! \property Q3DataTable::confirmUpdate \brief whether the data table confirms update operations If the confirmUpdate property is true, all updates must be confirmed by the user through a message box (this behavior can be changed by overriding the confirmEdit() function), otherwise all update operations occur immediately. \sa confirmCancels() confirmEdits() confirmInsert() confirmDelete()*/bool Q3DataTable::confirmUpdate() const{ return ( d->dat.confirmUpdate() );}/*! \property Q3DataTable::confirmDelete \brief whether the data table confirms delete operations If the confirmDelete property is true, all deletions must be confirmed by the user through a message box (this behavior can be changed by overriding the confirmEdit() function), otherwise all delete operations occur immediately. \sa confirmCancels() confirmEdits() confirmUpdate() confirmInsert()*/bool Q3DataTable::confirmDelete() const{ return ( d->dat.confirmDelete() );}/*! \property Q3DataTable::confirmCancels \brief whether the data table confirms cancel operations If the confirmCancel property is true, all cancels must be confirmed by the user through a message box (this behavior can be changed by overriding the confirmCancel() function), otherwise all cancels occur immediately. The default is false. \sa confirmEdits() confirmCancel()*/void Q3DataTable::setConfirmCancels( bool confirm ){ d->dat.setConfirmCancels( confirm );}bool Q3DataTable::confirmCancels() const{ return d->dat.confirmCancels();}/*! \reimp For an editable table, creates an editor suitable for the field in column \a col. The editor is created using the default editor factory, unless a different editor factory was installed with installEditorFactory(). The editor is primed with the value of the field in \a col using a property map. The property map used is the default property map, unless a new property map was installed with installPropertMap(). If \a initFromCell is true then the editor is primed with the value in the Q3DataTable cell.*/QWidget * Q3DataTable::createEditor( int , int col, bool initFromCell ) const{ if ( d->dat.mode() == QSql::None ) return 0; Q3SqlEditorFactory * f = (d->editorFactory == 0) ? Q3SqlEditorFactory::defaultFactory() : d->editorFactory; Q3SqlPropertyMap * m = (d->propertyMap == 0) ? Q3SqlPropertyMap::defaultMap() : d->propertyMap; QWidget * w = 0; if( initFromCell && d->editBuffer ){ w = f->createEditor( viewport(), d->editBuffer->fieldPtr( indexOf( col ) ) ); if ( w ) m->setProperty( w, d->editBuffer->value( indexOf( col ) ) ); } return w;}/*! \reimp */bool Q3DataTable::eventFilter( QObject *o, QEvent *e ){ if ( d->cancelMode ) return true; int r = currentRow(); int c = currentColumn(); if ( d->dat.mode() != QSql::None ) { r = d->editRow; c = d->editCol; } d->cancelInsert = false; d->cancelUpdate = false; switch ( e->type() ) { case QEvent::KeyPress: { int conf = QSql::Yes; QKeyEvent *ke = (QKeyEvent*)e; if ( ( ke->key() == Qt::Key_Tab || ke->key() == Qt::Key_BackTab ) && ke->state() & Qt::ControlButton ) return false; if ( ke->key() == Qt::Key_Escape && d->dat.mode() == QSql::Insert ){ if ( confirmCancels() && !d->cancelMode ) { d->cancelMode = true; conf = confirmCancel( QSql::Insert ); d->cancelMode = false; } if ( conf == QSql::Yes ) { d->cancelInsert = true; } else { QWidget *editorWidget = cellWidget( r, c ); if ( editorWidget ) { editorWidget->setActiveWindow(); editorWidget->setFocus(); } return true; } } if ( ke->key() == Qt::Key_Escape && d->dat.mode() == QSql::Update ) { if ( confirmCancels() && !d->cancelMode ) { d->cancelMode = true; conf = confirmCancel( QSql::Update ); d->cancelMode = false; } if ( conf == QSql::Yes ){ d->cancelUpdate = true; } else { QWidget *editorWidget = cellWidget( r, c ); if ( editorWidget ) { editorWidget->setActiveWindow(); editorWidget->setFocus(); } return true; } } if ( ke->key() == Qt::Key_Insert && d->dat.mode() == QSql::None ) { beginInsert(); return true; } if ( ke->key() == Qt::Key_Delete && d->dat.mode() == QSql::None ) { deleteCurrent(); return true; } if ( d->dat.mode() != QSql::None ) { if ( (ke->key() == Qt::Key_Tab) && (c < numCols() - 1) && (!isColumnReadOnly( c+1 ) || d->dat.mode() == QSql::Insert) ) d->continuousEdit = true; else if ( (ke->key() == Qt::Key_BackTab) && (c > 0) && (!isColumnReadOnly( c-1 ) || d->dat.mode() == QSql::Insert) ) d->continuousEdit = true; else d->continuousEdit = false; } Q3SqlCursor * sql = sqlCursor(); if ( sql && sql->driver() && !sql->driver()->hasFeature( QSqlDriver::QuerySize ) && ke->key() == Qt::Key_End && d->dat.mode() == QSql::None ) {#ifndef QT_NO_CURSOR QApplication::setOverrideCursor( Qt::WaitCursor );#endif int i = sql->at(); if ( i < 0 ) { i = 0; sql->seek(0); } while ( sql->next() ) i++; setNumRows( i+1 ); setCurrentCell( i+1, currentColumn() );#ifndef QT_NO_CURSOR QApplication::restoreOverrideCursor();#endif return true; } break; } case QEvent::FocusOut: { QWidget *editorWidget = cellWidget( r, c ); repaintCell( currentRow(), currentColumn() ); if ( !d->cancelMode && editorWidget && o == editorWidget && ( d->dat.mode() == QSql::Insert) && !d->continuousEdit) { setCurrentCell( r, c ); d->cancelInsert = true; } d->continuousEdit = false; break; } case QEvent::FocusIn: repaintCell( currentRow(), currentColumn() ); break; default: break; } return Q3Table::eventFilter( o, e );}/*! \reimp */void Q3DataTable::resizeEvent ( QResizeEvent * e ){ if ( sqlCursor() && sqlCursor()->driver() && !sqlCursor()->driver()->hasFeature( QSqlDriver::QuerySize ) ) loadNextPage(); Q3Table::resizeEvent( e );}/*! \reimp */void Q3DataTable::contentsContextMenuEvent( QContextMenuEvent* e ){ Q3Table::contentsContextMenuEvent( e ); if ( isEditing() && d->dat.mode() != QSql::None ) endEdit( d->editRow, d->editCol, autoEdit(), false ); if ( !sqlCursor() ) return; if ( d->dat.mode() == QSql::None ) { if ( isReadOnly() ) return; enum { IdInsert, IdUpdate, IdDelete }; QPointer<Q3PopupMenu> popup = new Q3PopupMenu( this, "qt_datatable_menu" ); int id[ 3 ]; id[ IdInsert ] = popup->insertItem( tr( "Insert" ) ); id[ IdUpdate ] = popup->insertItem( tr( "Update" ) ); id[ IdDelete ] = popup->insertItem( tr( "Delete" ) ); bool enableInsert = sqlCursor()->canInsert(); popup->setItemEnabled( id[ IdInsert ], enableInsert ); bool enableUpdate = currentRow() > -1 && sqlCursor()->canUpdate() && !isColumnReadOnly( currentColumn() ); popup->setItemEnabled( id[ IdUpdate ], enableUpdate ); bool enableDelete = currentRow() > -1 && sqlCursor()->canDelete(); popup->setItemEnabled( id[ IdDelete ], enableDelete ); int r = popup->exec( e->globalPos() ); delete (Q3PopupMenu*) popup; if ( r == id[ IdInsert ] ) beginInsert(); else if ( r == id[ IdUpdate ] ) { if ( beginEdit( currentRow(), currentColumn(), false ) ) setEditMode( Editing, currentRow(), currentColumn() ); else endUpdate(); } else if ( r == id[ IdDelete ] ) deleteCurrent(); e->accept(); }}/*! \reimp */void Q3DataTable::contentsMousePressEvent( QMouseEvent* e ){ Q3Table::contentsMousePressEvent( e );}/*! \reimp */QWidget* Q3DataTable::beginEdit ( int row, int col, bool replace ){ d->editRow = -1; d->editCol = -1; if ( !sqlCursor() ) return 0; if ( d->dat.mode() == QSql::Insert && !sqlCursor()->canInsert() ) return 0; if ( d->dat.mode() == QSql::Update && !sqlCursor()->canUpdate() ) return 0; d->editRow = row; d->editCol = col; if ( d->continuousEdit ) { // see comment in beginInsert() bool fakeReadOnly = isColumnReadOnly( col ); setColumnReadOnly( col, false ); QWidget* w = Q3Table::beginEdit( row, col, replace ); setColumnReadOnly( col, fakeReadOnly ); return w; } if ( d->dat.mode() == QSql::None && sqlCursor()->canUpdate() && sqlCursor()->primaryIndex().count() > 0 ) return beginUpdate( row, col, replace ); return 0;}/*! \reimp */void Q3DataTable::endEdit( int row, int col, bool, bool ){ bool accept = autoEdit() && !d->cancelInsert && !d->cancelUpdate; QWidget *editor = cellWidget( row, col ); if ( !editor ) return; if ( d->cancelMode ) return; if ( d->dat.mode() != QSql::None && d->editBuffer ) { Q3SqlPropertyMap * m = (d->propertyMap == 0) ? Q3SqlPropertyMap::defaultMap() : d->propertyMap; d->editBuffer->setValue( indexOf( col ), m->property( editor ) ); clearCellWidget( row, col ); if ( !d->continuousEdit ) { switch ( d->dat.mode() ) { case QSql::Insert: if ( accept ) QTimer::singleShot( 0, this, SLOT(doInsertCurrent()) ); else endInsert(); break; case QSql::Update: if ( accept ) QTimer::singleShot( 0, this, SLOT(doUpdateCurrent()) ); else endUpdate(); break; default: break; } } } else { setEditMode( NotEditing, -1, -1 ); } if ( d->dat.mode() == QSql::None ) viewport()->setFocus(); updateCell( row, col ); emit valueChanged( row, col );}/*! \internal */void Q3DataTable::doInsertCurrent(){ insertCurrent();}/*! \internal */void Q3DataTable::doUpdateCurrent(){ updateCurrent(); if ( d->dat.mode() == QSql::None ) { viewport()->setFocus(); }}/*! \reimp */void Q3DataTable::activateNextCell(){// if ( d->dat.mode() == QSql::None )// Q3Table::activateNextCell();}/*! \internal*/void Q3DataTable::endInsert(){ if ( d->dat.mode() != QSql::Insert ) return; d->dat.setMode( QSql::None ); d->editBuffer = 0; verticalHeader()->setLabel( d->editRow, QString::number( d->editRow +1 ) ); d->editRow = -1; d->editCol = -1; d->insertRowLast = -1; d->insertHeaderLabelLast.clear(); setEditMode( NotEditing, -1, -1 ); setNumRows( d->insertPreRows ); d->insertPreRows = -1; viewport()->setFocus();}/*! \internal */void Q3DataTable::endUpdate(){ d->dat.setMode( QSql::None );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -