Results 1 to 19 of 19

Thread: How to create a Combo box in a Tablview Model

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    uzairsaeed702 Guest

    Default Re: How to create a Combo box in a Tablview Model

    Quote Originally Posted by jthomps View Post
    I am definitely not an expert, but I have successfully used a ProgressBar delegate as follows. In my MainWindow, I set the item delegate for the view as follows:
    Qt Code:
    1. ui->tableView->setItemDelegate(new ProgressBarDelegate());
    To copy to clipboard, switch view to plain text mode 
    ProgressBarDelegate is a class derived from QStyledItemDelegate and here's the paint method:
    Qt Code:
    1. void ProgressBarDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
    2. {
    3. if (!index.isValid())
    4. return;
    5.  
    6. int col = index.column();
    7.  
    8. if (col == 5)
    9. {
    10. int progress = index.data().toInt();
    11. QStyleOptionProgressBar progressBarOption;
    12. progressBarOption.rect = QRect(option.rect.x(), option.rect.y() + 5 , option.rect.width(), option.rect.height() / 1.5);
    13. if (progress == 100)
    14. {
    15. QApplication::style()->drawItemText(painter, option.rect, Qt::AlignCenter|Qt::AlignVCenter, option.palette, true, QString::number(progress) + "%");
    16. }
    17. else
    18. {
    19. progressBarOption.minimum = 0;
    20. progressBarOption.maximum = 100;
    21. progressBarOption.progress = progress;
    22. progressBarOption.text = QString::number(progress) + "%";
    23. progressBarOption.textVisible = true;
    24. progressBarOption.textAlignment = Qt::AlignCenter|Qt::AlignVCenter;
    25. QApplication::style()->drawControl(QStyle::CE_ProgressBar, &progressBarOption, painter);
    26. }
    27. }
    28. else
    29. {
    30. QStyledItemDelegate::paint(painter, option, index);
    31. }
    32.  
    33. return;
    34. }
    To copy to clipboard, switch view to plain text mode 
    So you'll see that the delegate is set at the view level and then in the paint method, I only override how the progress bar I want in column 5 is drawn and I call the base class paint method for all other columns.

    If I am following your thread correctly, I believe you should be able to take a similar approach.

    Good luck.
    Hey jthomps,

    That looks helpful to me and it gives the clear idea to play for with custom columns . I will try to do it through delegates hopefully it will work.

    Thanks

    Regards
    Uzair saeed

    Quote Originally Posted by ChrisW67 View Post
    If you only want customised behaviour for a couple of columns (or rows) then you only apply a custom delegate to those columns (or rows). This is exactly what your own linked combo box delegate does.

    Jthomps approach is also a way to achieve the goal. The paint() and createEditor() functions look at which column they are being asked to act on and modify their behaviour accordingly. This method ties the delegate fairly closely to this particular view, i.e. You cannot reuse this delegate class on another view with 8 columns to get customised behaviour on columns 3 and 6.
    Yes i am getting the idea now but in actually Model View concept is bit confusing i guess i have to study that way more time to get the clear idea. But i will try today.

    Thanks ChrisW67


    Added after 5 minutes:


    Quote Originally Posted by anda_skoa View Post
    Let me get this straight:
    - you want the delegate in certain columns only
    - you ignore the existance of setItemDelegateForColumn, which does exactly that
    - you keep pretending that your problem is not solved

    Only reason to do that is to troll, right?

    Cheers,
    _
    Hey,

    It was my previous try but i am getting the combo box in each columns . Problem is only that Model View concept is bit confusing and if i go with QTableWidget its easy but takes time . Actually later from that combo box i have to attach the event which can invoke the dialog so for that complexity i am planing which method would be easy for me. So i am asking question and i am beginner that is why trying approaches .

    Thanks
    Last edited by uzairsaeed702; 14th January 2015 at 10:41.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to create a Combo box in a Tablview Model

    Yes true but in that example everything is handled by QSqlTableModel class which automatically detects the queries and place the combo box where it is necessary , which is invalid in my case. My model
    The example you linked to has nothing to do with an SQL model. The model data can come from anywhere as long as it is presented using the QAbstractItemModel interface. The model provides data only.

    The rendering of data in a view has nothing to do with the model. The view passes the painting and editor creation for each cell to a delegate, either the default one or one you provide, and the delegate decides what to display for the data the model is providing. The model may provide data indicating a colour, font, icon etc. but the view delegate can choose how to use (or ignore) that information when displaying the data. The type of editor provided for a cell is entirely decided by the delegate.

  3. The following user says thank you to ChrisW67 for this useful post:


  4. #3
    uzairsaeed702 Guest

    Default Re: How to create a Combo box in a Tablview Model

    Quote Originally Posted by ChrisW67 View Post
    The example you linked to has nothing to do with an SQL model. The model data can come from anywhere as long as it is presented using the QAbstractItemModel interface. The model provides data only.

    The rendering of data in a view has nothing to do with the model. The view passes the painting and editor creation for each cell to a delegate, either the default one or one you provide, and the delegate decides what to display for the data the model is providing. The model may provide data indicating a colour, font, icon etc. but the view delegate can choose how to use (or ignore) that information when displaying the data. The type of editor provided for a cell is entirely decided by the delegate.
    Hey ChrisW67,

    Now i understand everything , i am sorry this model view concept is confusing . I will try to alter the delegate class with Paint and create editor and will try to play with it. Hopefully i will achieve my target. I will stick to model view concept instead of QtableWidget .

    Thanks for the motivation.

  5. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to create a Combo box in a Tablview Model

    Quote Originally Posted by uzairsaeed702 View Post
    It was my previous try but i am getting the combo box in each columns
    I highly doubt it.
    Unless you are still calling setItemDelegate() instead of the correct method.

    Cheers,
    _

Similar Threads

  1. Create Complex combo control
    By frsdot in forum General Programming
    Replies: 1
    Last Post: 20th October 2013, 15:14
  2. Replies: 1
    Last Post: 11th June 2013, 16:56
  3. Value from combo box to LCD
    By dayo30 in forum Qwt
    Replies: 4
    Last Post: 1st February 2013, 00:40
  4. How to create Combo box using QML in Qt
    By Channareddy in forum Newbie
    Replies: 0
    Last Post: 4th July 2011, 05:47
  5. Is it possible to create a combo with paper sizes?
    By aekilic in forum Qt Programming
    Replies: 10
    Last Post: 19th January 2010, 09:16

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
  •  
Qt is a trademark of The Qt Company.