📄 q3datatable.cpp
字号:
/*! Returns true if the table will automatically delete the cursor specified by setSqlCursor(); otherwise returns false.*/bool Q3DataTable::autoDelete() const{ return d->cur.autoDelete();}/*! Sets the cursor auto-delete flag to \a enable. If \a enable is true, the table will automatically delete the cursor specified by setSqlCursor(). If \a enable is false (the default), the cursor will not be deleted.*/void Q3DataTable::setAutoDelete( bool enable ){ d->cur.setAutoDelete( enable );}/*! \property Q3DataTable::autoEdit \brief whether the data table automatically applies edits The default value for this property is true. When the user begins an insert or update in the table there are two possible outcomes when they navigate to another record: \list 1 \i the insert or update is is performed -- this occurs if autoEdit is true \i the insert or update is abandoned -- this occurs if autoEdit is false \endlist*/void Q3DataTable::setAutoEdit( bool autoEdit ){ d->dat.setAutoEdit( autoEdit );}bool Q3DataTable::autoEdit() const{ return d->dat.autoEdit();}/*! \property Q3DataTable::nullText \brief the text used to represent NULL values The nullText property will be used to represent NULL values in the table. The default value is provided by the cursor's driver.*/void Q3DataTable::setNullText( const QString& nullText ){ d->nullTxt = nullText; d->nullTxtChanged = true;}QString Q3DataTable::nullText() const{ return d->nullTxt;}/*! \property Q3DataTable::trueText \brief the text used to represent true values The trueText property will be used to represent NULL values in the table. The default value is "True".*/void Q3DataTable::setTrueText( const QString& trueText ){ d->trueTxt = trueText;}QString Q3DataTable::trueText() const{ return d->trueTxt;}/*! \property Q3DataTable::falseText \brief the text used to represent false values The falseText property will be used to represent NULL values in the table. The default value is "False".*/void Q3DataTable::setFalseText( const QString& falseText ){ d->falseTxt = falseText;}QString Q3DataTable::falseText() const{ return d->falseTxt;}/*! \property Q3DataTable::dateFormat \brief the format used for displaying date/time values The dateFormat property is used for displaying date/time values in the table. The default value is Qt::LocalDate.*/void Q3DataTable::setDateFormat( const Qt::DateFormat f ){ d->datefmt = f;}Qt::DateFormat Q3DataTable::dateFormat() const{ return d->datefmt;}/*! \property Q3DataTable::numRows \brief the number of rows in the table*/int Q3DataTable::numRows() const{ return Q3Table::numRows();}/*! \reimp The number of rows in the table will be determined by the cursor (see setSqlCursor()), so normally this function should never be called. It is included for completeness.*/void Q3DataTable::setNumRows ( int r ){ Q3Table::setNumRows( r );}/*! \reimp The number of columns in the table will be determined automatically (see addColumn()), so normally this function should never be called. It is included for completeness.*/void Q3DataTable::setNumCols ( int r ){ Q3Table::setNumCols( r );}/*! \property Q3DataTable::numCols \brief the number of columns in the table*/int Q3DataTable::numCols() const{ return Q3Table::numCols();}/*! Returns the text in cell \a row, \a col, or an empty string if the cell is empty. If the cell's value is NULL then nullText() will be returned. If the cell does not exist then an empty string is returned.*/QString Q3DataTable::text ( int row, int col ) const{ if ( !sqlCursor() ) return QString(); QString s; if ( sqlCursor()->seek( row ) ) s = sqlCursor()->value( indexOf( col ) ).toString(); sqlCursor()->seek( currentRow() ); return s;}/*! Returns the value in cell \a row, \a col, or an invalid value if the cell does not exist or has no value.*/QVariant Q3DataTable::value ( int row, int col ) const{ if ( !sqlCursor() ) return QVariant(); QVariant v; if ( sqlCursor()->seek( row ) ) v = sqlCursor()->value( indexOf( col ) ); sqlCursor()->seek( currentRow() ); return v;}/*! \internal Used to update the table when the size of the result set cannot be determined - divide the result set into pages and load the pages as the user moves around in the table.*/void Q3DataTable::loadNextPage(){ if ( d->haveAllRows ) return; if ( !sqlCursor() ) return; int pageSize = 0; int lookAhead = 0; if ( height() ) { pageSize = (int)( height() * 2 / 20 ); lookAhead = pageSize / 2; } int startIdx = verticalScrollBar()->value() / 20; int endIdx = startIdx + pageSize + lookAhead; if ( endIdx < numRows() || endIdx < 0 ) return; // check for empty result set if ( sqlCursor()->at() == QSql::BeforeFirst && !sqlCursor()->next() ) { d->haveAllRows = true; return; } while ( endIdx > 0 && !sqlCursor()->seek( endIdx ) ) endIdx--; if ( endIdx != ( startIdx + pageSize + lookAhead ) ) d->haveAllRows = true; // small hack to prevent Q3Table from moving the view when a row // is selected and the contents is resized SelectionMode m = selectionMode(); clearSelection(); setSelectionMode( NoSelection ); setNumRows( endIdx ? endIdx + 1 : 0 ); sqlCursor()->seek( currentRow() ); setSelectionMode( m );}/*! \internal */void Q3DataTable::sliderPressed(){ disconnect( verticalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(loadNextPage()) );}/*! \internal */void Q3DataTable::sliderReleased(){ loadNextPage(); connect( verticalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(loadNextPage()) );}/*! Sorts column \a col in ascending order if \a ascending is true (the default); otherwise sorts in descending order. The \a wholeRows parameter is ignored; Q3DataTable always sorts whole rows by the specified column.*/void Q3DataTable::sortColumn ( int col, bool ascending, bool ){ if ( sorting() ) { if ( isEditing() && d->dat.mode() != QSql::None ) endEdit( d->editRow, d->editCol, autoEdit(), false ); if ( !sqlCursor() ) return; QSqlIndex lastSort = sqlCursor()->sort(); QSqlIndex newSort( lastSort.cursorName(), QLatin1String("newSort") ); const QSqlField *field = sqlCursor()->fieldPtr( indexOf( col ) ); if ( field ) newSort.append( *field ); newSort.setDescending( 0, !ascending ); horizontalHeader()->setSortIndicator( col, ascending ); setSort( newSort ); refresh(); }}/*! \reimp */void Q3DataTable::columnClicked ( int col ){ if ( sorting() ) { if ( !sqlCursor() ) return; QSqlIndex lastSort = sqlCursor()->sort(); bool asc = true; if ( lastSort.count() && lastSort.fieldPtr( 0 )->name() == sqlCursor()->fieldPtr( indexOf( col ) )->name() ) asc = lastSort.isDescending( 0 ); sortColumn( col, asc ); emit currentChanged( sqlCursor() ); }}/*! Repaints the cell at \a row, \a col.*/void Q3DataTable::repaintCell( int row, int col ){ QRect cg = cellGeometry( row, col ); QRect re( QPoint( cg.x() - 2, cg.y() - 2 ), QSize( cg.width() + 4, cg.height() + 4 ) ); repaintContents( re, false );}/*! \reimp This function renders the cell at \a row, \a col with the value of the corresponding cursor field on the painter \a p. Depending on the table's current edit mode, paintField() is called for the appropriate cursor field. \a cr describes the cell coordinates in the content coordinate system. If \a selected is true the cell has been selected and would normally be rendered differently than an unselected cell. \sa QSql::isNull()*/void Q3DataTable::paintCell( QPainter * p, int row, int col, const QRect & cr, bool selected, const QColorGroup &cg ){ Q3Table::paintCell( p, row, col, cr, selected, cg ); // empty cell if ( !sqlCursor() ) return; p->setPen( selected ? cg.highlightedText() : cg.text() ); if ( d->dat.mode() != QSql::None ) { if ( row == d->editRow && d->editBuffer ) { paintField( p, d->editBuffer->fieldPtr( indexOf( col ) ), cr, selected ); } else if ( row > d->editRow && d->dat.mode() == QSql::Insert ) { if ( sqlCursor()->seek( row - 1 ) ) paintField( p, sqlCursor()->fieldPtr( indexOf( col ) ), cr, selected ); } else { if ( sqlCursor()->seek( row ) ) paintField( p, sqlCursor()->fieldPtr( indexOf( col ) ), cr, selected ); } } else { if ( sqlCursor()->seek( row ) ) paintField( p, sqlCursor()->fieldPtr( indexOf( col ) ), cr, selected ); }}/*! Paints the \a field on the painter \a p. The painter has already been translated to the appropriate cell's origin where the \a field is to be rendered. \a cr describes the cell coordinates in the content coordinate system. The \a selected parameter is ignored. If you want to draw custom field content you must reimplement paintField() to do the custom drawing. The default implementation renders the \a field value as text. If the field is NULL, nullText() is displayed in the cell. If the field is Boolean, trueText() or falseText() is displayed as appropriate.*/void Q3DataTable::paintField( QPainter * p, const QSqlField* field, const QRect & cr, bool ){ if ( !field ) return; p->drawText( 2,2, cr.width()-4, cr.height()-4, fieldAlignment( field ), fieldToString( field ) );}/*! Returns the alignment for \a field.*/int Q3DataTable::fieldAlignment( const QSqlField* /*field*/ ){ return Qt::AlignLeft | Qt::AlignVCenter; //## Reggie: add alignment to Q3Table}/*! If the cursor's \a sql driver supports query sizes, the number of rows in the table is set to the size of the query. Otherwise, the table dynamically resizes itself as it is scrolled. If \a sql is not active, it is made active by issuing a select() on the cursor using the \a sql cursor's current filter and current sort.*/void Q3DataTable::setSize( Q3SqlCursor* sql ){ // ### what are the connect/disconnect calls doing here!? move to refresh() if ( sql->driver() && sql->driver()->hasFeature( QSqlDriver::QuerySize ) ) { setVScrollBarMode( Auto ); disconnect( verticalScrollBar(), SIGNAL(sliderPressed()), this, SLOT(sliderPressed()) ); disconnect( verticalScrollBar(), SIGNAL(sliderReleased()), this, SLOT(sliderReleased()) ); disconnect( verticalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(loadNextPage()) ); if ( numRows() != sql->size() ) setNumRows( sql->size() ); } else { setVScrollBarMode( AlwaysOn ); connect( verticalScrollBar(), SIGNAL(sliderPressed()), this, SLOT(sliderPressed()) ); connect( verticalScrollBar(), SIGNAL(sliderReleased()), this, SLOT(sliderReleased()) ); connect( verticalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(loadNextPage()) ); setNumRows(0); loadNextPage(); }}/*! Sets \a cursor as the data source for the table. To force the display of the data from \a cursor, use refresh(). If \a autoPopulate is true, columns are automatically created based upon the fields in the \a cursor record. If \a autoDelete is true (the default is false), the table will take ownership of the \a cursor and delete it when appropriate. If the \a cursor is read-only, the table becomes read-only. The table adopts the cursor's driver's definition for representing NULL values as strings. \sa refresh() setReadOnly() setAutoDelete() QSqlDriver::nullText()*/void Q3DataTable::setSqlCursor( Q3SqlCursor* cursor, bool autoPopulate, bool autoDelete ){ setUpdatesEnabled( false ); d->cur.setCursor( 0 ); if ( cursor ) { d->cur.setCursor( cursor, autoDelete ); if ( autoPopulate ) { d->fld.clear(); d->fldLabel.clear(); d->fldWidth.clear(); d->fldIcon.clear(); d->fldHidden.clear(); for ( int i = 0; i < sqlCursor()->count(); ++i ) { addColumn( sqlCursor()->fieldPtr( i )->name(), sqlCursor()->fieldPtr( i )->name() ); setColumnReadOnly( i, sqlCursor()->fieldPtr( i )->isReadOnly() ); } } setReadOnly( sqlCursor()->isReadOnly() ); if ( sqlCursor()->driver() && !d->nullTxtChanged ) setNullText(sqlCursor()->driver()->nullText() ); setAutoDelete( autoDelete ); } else { setNumRows( 0 ); setNumCols( 0 ); } setUpdatesEnabled( true );}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -