Results 1 to 13 of 13

Thread: custom widget in delegate

  1. #1
    Join Date
    Feb 2014
    Posts
    60
    Thanks
    4
    Thanked 5 Times in 5 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default custom widget in delegate

    Hi,
    I need to show custom widget(lineEdit +PushButton) on double click of QTreeview so i have created my own Delegate but for some reason the widget is not shown.I have createEditor() method override in delegate.Here is a sample Code i have done.I have not implemented the paint method to render the data but my problem is the widget doesn't shown when create editor called.Guide me on this

    MainWindow.cpp

    Qt Code:
    1. for (int row = 0; row < 4; ++row) {
    2. for (int column = 0; column < 4; ++column) {
    3. QStandardItem *item = new QStandardItem(QString("row %0, column %1").arg(row).arg(column));
    4. model->setItem(row, column, item);
    5. }
    6. }
    7.  
    8. QTreeView *veiw = new QTreeView(this);
    9. MyDelegate *del = new MyDelegate;
    10. veiw->setItemDelegate(del);
    11. veiw->setModel(model);
    12.  
    13. setCentralWidget(veiw);
    To copy to clipboard, switch view to plain text mode 

    MyDelegate.cpp

    Qt Code:
    1. QWidget *MyDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
    2. {
    3. if(index.column() == 0)
    4. return new CustomWidget(parent);
    5. if(index.column() == 1)
    6. return new QComboBox(parent);
    7.  
    8. }
    To copy to clipboard, switch view to plain text mode 

    CustomWidget.cpp

    Qt Code:
    1. CustomWidget::CustomWidget(QWidget *parent) : QWidget(parent)
    2. {
    3. QLineEdit *edit = new QLineEdit;
    4. QHBoxLayout *layout = new QHBoxLayout;
    5. layout->addWidget(edit);
    6. layout->addWidget(but);
    7. setLayout(layout);
    8. }
    To copy to clipboard, switch view to plain text mode 
    Attached Images Attached Images

  2. #2
    Join Date
    Jun 2015
    Location
    India
    Posts
    185
    Thanks
    8
    Thanked 13 Times in 13 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: custom widget in delegate

    we need to implement
    void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) also, Implement & try if you did not.


    Added after 1 43 minutes:


    I tried changing your example. I could see custom editor at least with below method implementation (but at wrong place because I am passing hard coded rect).
    May be you could try pass proper geometry and try.

    void SpinBoxDelegate::updateEditorGeometry(QWidget *editor,
    const QStyleOptionViewItem &option, const QModelIndex &/* index */) const
    {
    QRect rect = option.rect;
    rect.adjust(0, 0,100, 50);
    editor->setGeometry(rect);
    }
    Last edited by prasad_N; 11th July 2015 at 15:19.
    Thanks :-)

  3. #3
    Join Date
    Feb 2014
    Posts
    60
    Thanks
    4
    Thanked 5 Times in 5 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: custom widget in delegate

    @prasad:
    Thanks ill give a try.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: custom widget in delegate

    What are the edit triggers for your view?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Feb 2014
    Posts
    60
    Thanks
    4
    Thanked 5 Times in 5 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: custom widget in delegate

    @wysto:
    QAbstractItemView::AllEditTriggers

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: custom widget in delegate

    Does your createEditor() method get called at all?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Feb 2014
    Posts
    60
    Thanks
    4
    Thanked 5 Times in 5 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: custom widget in delegate

    @wysota:
    it gets called and when i re-implement updateEditorGeometry(as suggested by prasad) the widget appears(may be not within the cell) but my doubt is why it doesnt appear before but when i use QSpinbox or QLineEdit then without updateEditorGeometry() the widget comes perfect within the cell.still confused on how to use custom widget with tree view .

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: custom widget in delegate

    Maybe it is a matter of setting size policies right?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Feb 2014
    Posts
    60
    Thanks
    4
    Thanked 5 Times in 5 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: custom widget in delegate

    @wysota:
    yes it should be,so how do we overcome this now where i need to change so that widget occupies the whole cell exactly.

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: custom widget in delegate

    Well... adjust the size policies of each widget and see if it changes anything
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. #11
    Join Date
    Jun 2015
    Location
    India
    Posts
    185
    Thanks
    8
    Thanked 13 Times in 13 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: custom widget in delegate

    Yes, I think its problem with size policies only, I showed CustomWidget and try to minimize width & height to zero But I could not do it.
    I think the minimum size of the CustomWidget is being much greater than cell size, if we can make minimum size of CustomWidget is to (0,0) & max size to default the it will be visible in cell.
    Thanks :-)

  12. #12
    Join Date
    Feb 2014
    Posts
    60
    Thanks
    4
    Thanked 5 Times in 5 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: custom widget in delegate

    I don't think so the problem is with size policy because the widget is visible only when i give rect.adjust()(as suggested by prasad) so the point(x,y) of the widget is the problem.I also tried with different size policy but still widget is not visible.So any suggestion how can i proceed further.
    Attached Images Attached Images

  13. #13
    Join Date
    Feb 2014
    Posts
    60
    Thanks
    4
    Thanked 5 Times in 5 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: custom widget in delegate

    Thanks for your help guys,the problem was with the layout spacing.Once we set Layout Spacing and margin to 0 .The widget came perfectly

Similar Threads

  1. Replies: 5
    Last Post: 30th October 2014, 20:40
  2. Replies: 1
    Last Post: 10th May 2011, 22:35
  3. Custom Model? Custom View? Custom Delegate?
    By Doug Broadwell in forum Newbie
    Replies: 4
    Last Post: 11th February 2010, 20:23
  4. Replies: 0
    Last Post: 1st February 2010, 11:00
  5. Question about custom view (or custom delegate)
    By skuda in forum Qt Programming
    Replies: 1
    Last Post: 21st September 2009, 20:06

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.