Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to resize QWidget created using QItemDelegate?
Forum Updated to NodeBB v4.3 + New Features

How to resize QWidget created using QItemDelegate?

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 2 Posters 1.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    CLang
    wrote on last edited by CLang
    #1

    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.

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      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

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      C 1 Reply Last reply
      0
      • VRoninV VRonin

        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

        C Offline
        C Offline
        CLang
        wrote on last edited by
        #3

        @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

        1 Reply Last reply
        0
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by
          #4

          I would not put it in the delegate

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          C 1 Reply Last reply
          0
          • VRoninV VRonin

            I would not put it in the delegate

            C Offline
            C Offline
            CLang
            wrote on last edited by
            #5

            @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?

            1 Reply Last reply
            0
            • VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by
              #6

              Change the signal to pass the QModelIndex back to the editor

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved