[Qt-interest] How to create a widget without showing it

Matthias Pospiech matthias.pospiech at gmx.de
Tue May 19 15:50:14 CEST 2009


Matthias Pospiech schrieb:
> I am using the following code to generate a ItemDelegate with a 
> DoubleSpinBox.
>
> The problem I have is that the spinbox is displayed not inside the 
> table, but somewhere in the Dialog and
> that the app crashed at second creation of the editor.
>   
I changed the code to

    DoubleSpinBoxDelegate * delegateSpinBox = new 
DoubleSpinBoxDelegate(this) ;
    delegateSpinBox->setEditor(new QDoubleSpinBox(this));
    delegateSpinBox->Editor()->setRange(0,20000);
    delegateSpinBox->Editor()->setDecimals(4);
    tableViewPowerDegree->setItemDelegateForColumn(1,delegateSpinBox);

void DoubleSpinBoxDelegate::setEditor(QDoubleSpinBox * doubleSpinBox)
{
    editor = doubleSpinBox;
}

QDoubleSpinBox * DoubleSpinBoxDelegate::Editor()
{
    return editor;
}

QWidget *DoubleSpinBoxDelegate::createEditor(QWidget *parent,
                                             const QStyleOptionViewItem 
&/* option */,
                                             const QModelIndex &/* index 
*/) const
{
    QDoubleSpinBox * newEditor = new QDoubleSpinBox(parent);
    if (editor) {
        newEditor->setDecimals(editor->decimals());
        newEditor->setRange(editor->minimum(),editor->maximum());
    }
    return newEditor;
}

which works, but in the second line

    delegateSpinBox->setEditor(new QDoubleSpinBox(this));

the Spinbox is created within the containing widget (QDialog) and shown 
there. However I do not want it to be shown at all.

Matthias



More information about the Qt-interest-old mailing list