Results 1 to 19 of 19

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

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #11
    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.

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.