[Qt-interest] problem with createEditor method of QItemDelegate when the returned QWidget has a layout
TP
paratribulations at free.fr
Thu Aug 13 16:57:45 CEST 2009
Hi,
Ok, someone has given part of the solution on the pyqt-pykde forum, and it
leads me to the final solution: replace the LabelAndSpinBox constructor by
the following. Only two lines have been added:
this->setAutoFillBackground( 1 );
L->setContentsMargins(0, 0, 0, 0);
LabelAndSpinBox::LabelAndSpinBox( QWidget * parent )
: QWidget( parent )
{
label = new QLabel( tr("Find &what:") );
spinbox = new QSpinBox;
this->setAutoFillBackground( 1 );
QHBoxLayout * L = new QHBoxLayout;
L->setContentsMargins(0, 0, 0, 0);
L->addWidget( label );
L->addWidget( spinbox );
this->setLayout( L );
}
Thanks everybody,
Julien
TP wrote:
> Hi,
>
> Try the example at the end of this message: it is a table with editable
> items. This is a modified version of the example "spinboxdelegate" of
> the Qt examples ("itemviews" section). When the user double-clicks on an
> item, a LabelAndSpinBox instance is created as delegate. The problem is
> that the QHBoxLayout layout of this delegate does not expand to occupy all
> the available space; therefore the item data value remains visible and the
> delegate is too small. How to make it work correctly?
> I have asked the same question on pyqt-pykde mailing list, but I prefer
> asking on the Qt forum too, because of a larger community.
>
> Thanks,
>
> Julien
>
>
> ///////////////////////////////////////////////////////////////////
>
> #include <QApplication>
> #include <QPushButton>
> #include <QStandardItem>
> #include <QTableView>
> #include <QModelIndex>
> #include <QSpinBox>
> #include <QLabel>
> #include <QHBoxLayout>
> #include <QItemDelegate>
>
> //////////////////////////////////////////////////////////////////////////
> // //
> // LabelAndSpinBox //
> // //
> //////////////////////////////////////////////////////////////////////////
>
> class LabelAndSpinBox : public QWidget
> {
> public:
> LabelAndSpinBox( QWidget * parent = 0 );
> void setMinimum( int value = 0 );
> void setMaximum( int value = 1 );
> void setValue( int value = 0 );
> int value();
> private:
> QLabel * label;
> QSpinBox * spinbox;
> };
>
> LabelAndSpinBox::LabelAndSpinBox( QWidget * parent )
> : QWidget( parent )
> {
> label = new QLabel( tr("Find &what:") );
> spinbox = new QSpinBox;
>
> QHBoxLayout * L = new QHBoxLayout;
> L->addWidget( label );
> L->addWidget( spinbox );
>
> this->setLayout( L );
> }
>
> void LabelAndSpinBox::setMinimum( int value )
> {
> spinbox->setMinimum( value );
> }
>
> void LabelAndSpinBox::setMaximum( int value )
> {
> spinbox->setMaximum( value );
> }
>
> void LabelAndSpinBox::setValue( int value )
> {
> spinbox->setValue( value );
> }
>
> int LabelAndSpinBox::value()
> {
> return spinbox->value();
> }
>
> //////////////////////////////////////////////////////////////////////////
> // //
> // spinboxdelegate //
> // //
> //////////////////////////////////////////////////////////////////////////
>
> class SpinBoxDelegate : public QItemDelegate
> {
> public:
> SpinBoxDelegate( QObject * parent = 0 ):QItemDelegate( parent ){ }
> QWidget * createEditor( QWidget * parent
> , const QStyleOptionViewItem & option
> , const QModelIndex & index ) const;
> void setEditorData( QWidget * editor
> , const QModelIndex & index );
> void setModelData( QWidget * editor
> , QAbstractItemModel * model
> , const QModelIndex & index ) const;
> void updateEditorGeometry( QWidget *editor,
> const QStyleOptionViewItem &option
> , const QModelIndex &/* index */) const;
> };
>
> void SpinBoxDelegate::updateEditorGeometry( QWidget *editor,
> const QStyleOptionViewItem &option
> , const QModelIndex &/* index */) const
> {
> editor->setGeometry(option.rect);
> }
>
> // The "const" are very important!
> QWidget * SpinBoxDelegate::createEditor( QWidget * parent
> , const QStyleOptionViewItem & option
> , const QModelIndex & index ) const
> {
> LabelAndSpinBox * editor = new LabelAndSpinBox( parent );
> editor->setMinimum( 0 );
> editor->setMaximum( 100 );
> return editor;
> }
>
> void SpinBoxDelegate::setModelData(
> QWidget * editor
> , QAbstractItemModel * model
> , const QModelIndex & index ) const
> {
> int value = dynamic_cast<LabelAndSpinBox *> ( editor )->value();
> QVariant qv( value );
> model->setData( index, qv );
> }
>
>
> void setEditorData( QWidget * editor
> , const QModelIndex & index )
> {
> int value = index.model()->data( index
> , Qt::DisplayRole ).toInt();
> dynamic_cast<LabelAndSpinBox *> ( editor )->setValue( value );
> }
>
>
>
> //////////////////////////////////////////////////////////////////////////
> // //
> // main //
> // //
> //////////////////////////////////////////////////////////////////////////
>
>
> int main( int argc, char **argv )
> {
> QApplication a( argc, argv );
>
> QStandardItemModel model( 4, 2 );
> QTableView tableView;
>
> tableView.setModel( & model );
>
> SpinBoxDelegate delegate;
> tableView.setItemDelegate( & delegate );
>
> for ( int row = 0; row<4; row++ )
> {
> for ( int col = 0; col<2; col++ )
> {
> QModelIndex index = model.index(row, col );
> QVariant qv( (row+1) * (col+1) );
> model.setData( index, qv );
> }
> }
>
> tableView.setWindowTitle( "Spin Box Delegate" );
> tableView.show();
>
> return a.exec();
> }
> ///////////////////////////////////////////////////////////////////
--
python -c "print ''.join([chr(154 - ord(c)) for c in '*9(9&(18%.\
9&1+,\'Z4(55l4('])"
"When a distinguished but elderly scientist states that something is
possible, he is almost certainly right. When he states that something is
impossible, he is very probably wrong." (first law of AC Clarke)
More information about the Qt-interest-old
mailing list