How to resize QWidget created using QItemDelegate?



  • I have a QWidget that contains a QStandartItemModel to store data and QTableView to display the data.

    Another QWidget is used to query and filter from the database to add a row to the QStandardItem model.

    A QItemDelegate has been implemented to edit an existing row in the table. The QItemDelegate uses the same database QWidget, but when the new window is shown during editing, it doesn't match the same size and position as it does when adding the new item.

    Code for Adding data

    void zoneBase::addSource()
    {
        // Need a popup window to display the DBFilter selection widget.
        popupSources = new QDialog(this, Qt::Dialog);
        QPointer<QVBoxLayout> popupLayout = new QVBoxLayout();
    
        QPointer<IQDatabaseGUI::DBFilter> sources = new IQDatabaseGUI::DBFilter(); // This is the widget class displayed.
     
        // Catch the signals from the dbFilter.
        connect(sources, SIGNAL(selectionsUpdated(boost::shared_ptr<IQDatabase::DatabaseSelection>)), this, SLOT(updateSources(boost::shared_ptr<IQDatabase::DatabaseSelection>))); // Catch the selectionsUpdated signal and extract the selected materials.
        connect(sources, SIGNAL(accepted()), popupSources, SLOT(accept())); // Close the popup.
        connect(sources, SIGNAL(rejected()), popupSources, SLOT(reject())); // Do nothing and close the popup.
    
        popupLayout->addWidget(sources);
        popupSources->setLayout(popupLayout);
        popupSources->exec();
    }
    

    Reimplementation of QItemDelegate::createEditor

    QWidget * materialSelectDelegate::createEditor(QWidget * parent, const QStyleOptionViewItem & option, const QModelIndex & index) const
    {
        // Need a popup window to display the DBFilter selection widget.
        popupSources = new QDialog(parent, Qt::Dialog);
        QPointer<QVBoxLayout> popupLayout = new QVBoxLayout();
    
        QPointer<IQDatabaseGUI::DBFilter> sources = new IQDatabaseGUI::DBFilter(); // This is the widget class displayed.
     
        // Catch the signals from the dbFilter.
        connect(sources, SIGNAL(selectionsUpdated(boost::shared_ptr<IQDatabase::DatabaseSelection>)), this, SLOT(updateSources(boost::shared_ptr<IQDatabase::DatabaseSelection>))); // Catch the selectionsUpdated signal and extract the selected materials.
        connect(sources, SIGNAL(accepted()), popupSources, SLOT(accept())); // Close the popup.
        connect(sources, SIGNAL(rejected()), popupSources, SLOT(reject())); // Do nothing and close the popup.
    
        popupLayout->addWidget(sources);
        popupSources->setLayout(popupLayout);
    
        return popupSources;
    }
    

    The main difference is that instead of calling QDiolog::exec() the QDialog is returned. The result is that the GUI is displayed at the incorrect size and position in the screen. I can't figure out how to resolve this.



  • createEditor is meant to display a widget inside the item area, not to pop up a dialog.
    just create a signal in the delegate (Q_SIGNAL void requestEditDialog();) and implement:

    QWidget * materialSelectDelegate::createEditor(QWidget * parent, const QStyleOptionViewItem & option, const QModelIndex & index) const
    {
        requestEditDialog();
    return new QWidget(parent);
    }
    

    now just connect to the signal a slot that creates and execs the dialog



  • @VRonin said in How to resize QWidget created using QItemDelegate?:

    const QModelIndex & index

    Would the slot be a part of the materialSelectDelegate class or the class that hosts the QStandardItemModel?

    Thanks



  • I would not put it in the delegate



  • @VRonin

    Unless the editor widget is returned by createEditor(), the setModelData() method is not called.

    So your method solves the Widget settings problem, but how is the editor linked to the original QStandardItemModel?



  • Change the signal to pass the QModelIndex back to the editor


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.