Results 1 to 11 of 11

Thread: Add custom object to QListWidget

  1. #1
    Join Date
    Nov 2015
    Posts
    7
    Qt products
    Qt5
    Platforms
    Windows

    Default Add custom object to QListWidget

    Dear forum

    I can add to my QListWidget label, button,radiobutton and other, and all is working.

    All items appear in vertical....1 for every line....and all work ok.
    Also a scrollbar compare when the number of items occupy more space than the list.

    Problem start when i want for example, put in 1 line ,2 buttom (or more) and not only 1.

    So, I just try to insert only 2 button in horizontal, using the class QHBoxLayout ,then
    create 2 button, and put this 2 button inside,like in next code:



    QHBoxLayout *HLay= new QHBoxLayout();

    QPushButton *b1 = new QPushButton("B1");
    QPushButton *b2 = new QPushButton("B2");

    HLay->addWidget(b1);
    HLay->addWidget(b2);

    and now, i would like to insert this HLay in the QListWidget. So, create
    the List:


    QListWidget *List= new QListWidget(window);

    QListWidgetItem *ListItem=new QListWidgetItem();

    List->addItem(ListItem);
    List->setItemWidget(ListItem,HLay); //!!!!!!!!ERROR

    Last line of code is not correct because setItemWidget () accept Qwidget type
    and so not the HLay.

    Some help.

    Thanks for your time.

    Roberto

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Add custom object to QListWidget

    You will probably have to create a separate QWidget instance, insert your layout and buttons into that as children, and then insert *that* QWidget into your list widget.

    Qt Code:
    1. QHBoxLayout *HLay= new QHBoxLayout();
    2.  
    3. QPushButton *b1 = new QPushButton("B1");
    4. QPushButton *b2 = new QPushButton("B2");
    5.  
    6. HLay->addWidget(b1);
    7. HLay->addWidget(b2);
    8.  
    9. QWidget * twoButtonWidget = new QWidget();
    10. twoButtonWidget->setLayout( HLay );
    11.  
    12. QListWidget *List= new QListWidget(window);
    13.  
    14.  
    15. List->addItem(ListItem);
    16. List->setItemWidget(ListItem, twoButtonWidget );
    To copy to clipboard, switch view to plain text mode 

    If you plan to do this for every item in the list widget, then it would make sense to derive a custom class from QWidget that contains the layout and buttons and create instances of that to insert instead.

    Please use CODE tags when posting source code. When writing a post, click "Go Advanced" and click the "#" tool button to insert the code tags. Put your code between those tags.

  3. #3
    Join Date
    Nov 2015
    Posts
    7
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Add custom object to QListWidget

    Thanks for reply

    what i get is the Qlistwidget visible but empty...no button visible inside.

    If i add the instruction:

    twoButtonWidget->setFixedSize(200,200);

    then i can see the 2 button,but in the center of the list... Like if the layout rules canged.

    If i add the second couple of buttons, then they overlap.

    Do i have to change the style?

    Thanks

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Add custom object to QListWidget

    Try adding a horizontal stretch to the right side of the horizontal layout. QBoxLayout::addStretch().

    You should understand that when you add a widget to a list item it replaces the text in the item and is not interactive (i.e. clicking the button won't do anything). You probably want to look at using a QItemDelegate instead.

  5. #5
    Join Date
    Nov 2015
    Posts
    7
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Add custom object to QListWidget

    Quote Originally Posted by d_stranz View Post
    Try adding a horizontal stretch to the right side of the horizontal layout. QBoxLayout::addStretch().

    You should understand that when you add a widget to a list item it replaces the text in the item and is not interactive (i.e. clicking the button won't do anything). You probably want to look at using a QItemDelegate instead.


    Thanks Mr Stranz.

    I will chk delegate...Now i noted that if i add a single column of button (1 button over the other ), then they react to the click
    If i add a couple orizontal, and replicate in vertical, only the last 2 added reacr to the click.

    Thanks

  6. #6
    Join Date
    Nov 2015
    Posts
    7
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Add custom object to QListWidget

    Hello Mr Stranz

    I used delegate for paint a simple rectangle in a listview..this is the more simple example i want do,whitout
    event or other more complex thinks.

    What i did is to redifine the paint() function like in next code:
    I can see the list appear, but not the rectangle,and the paint() event is never called.

    This are the more important parts of my code:

    Qt Code:
    1. //-----------in Customdelegate .h-------------------
    2.  
    3. [COLOR="#FF8C00"]
    4. class Customdelegate:public QItemDelegate
    5. {
    6.  
    7. Q_OBJECT
    8.  
    9.  
    10. public:
    11.  
    12. Customdelegate(QObject *parent = 0);
    13.  
    14. virtual void paint(QPainter *painter,
    15. const QStyleOptionViewItem &option,
    16. const QModelIndex &index) const;
    17.  
    18. [/COLOR]
    19. };
    20.  
    21.  
    22. //--------------in Customdelegate.cpp----------------
    23.  
    24. [COLOR="#FF8C00"]
    25. Customdelegate::Customdelegate(QObject *parent):QItemDelegate(parent)
    26. {
    27.  
    28. objState=QStyle::State_Enabled;
    29. }
    30.  
    31.  
    32. void Customdelegate::paint(QPainter *painter,
    33. const QStyleOptionViewItem &option,
    34. const QModelIndex &index) const
    35.  
    36. {
    37.  
    38. QPen Penn;
    39. Penn.setWidth(5);
    40. Penn.setColor(Qt::red);
    41.  
    42.  
    43. QRect Rect;
    44. Rect.setRect(10,10,30,20);
    45.  
    46.  
    47. painter->setPen(Penn);
    48. painter->drawRect(Rect);
    49.  
    50. }
    51. [/COLOR]
    52.  
    53.  
    54. //-----------in my main.cpp------------
    55.  
    56. [COLOR="#FF8C00"]
    57. QWidget *window = new QWidget;
    58. window->setWindowTitle("Ciao");
    59.  
    60. QListView *ListW= new QListView(window);
    61.  
    62.  
    63. Customdelegate *ItemDel=new Customdelegate(ListW); //------------->my class
    64.  
    65. ListW->setItemDelegate(ItemDel);
    66.  
    67. window->show();
    68. [/COLOR]
    To copy to clipboard, switch view to plain text mode 


    Thanks for your help

    Roberto

  7. #7
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Add custom object to QListWidget

    From the docs for QItemDelegate::paint():
    When reimplementing this function in a subclass, you should update the area held by the option's rect variable, using the option's state variable to determine the state of the item to be displayed, and adjust the way it is painted accordingly.
    So try setting your QRect to option.rect instead of (10, 10 , 30, 20).

    Not quite sure what you're trying to do with this line:

    Qt Code:
    1. objState=QStyle::State_Enabled;
    To copy to clipboard, switch view to plain text mode 

  8. #8
    Join Date
    Nov 2015
    Posts
    7
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Add custom object to QListWidget

    Thanks Mr stranz

    yes..now i design the items using a relative position, and im able to introduce 2 line of text + design
    what i want around them....It very intresting to design what i want.

    The way i introduce text is the more general way, using model :

    Qt Code:
    1. QListView *ListW= new QListView(window);
    2.  
    3. Customdelegate *ItemDel=new Customdelegate(ListW);//connect delegate to view
    4. QStandardItemModel *Mod= new QStandardItemModel();//connect model to view
    5.  
    6.  
    7. ListW->setItemDelegate(ItemDel);
    8. ListW->setModel(Mod);
    9.  
    10.  
    11.  
    12. //Create and Add item1 to Mod
    13.  
    14.  
    15. Item1->setData("ciao1",Qt::DisplayRole);
    16.  
    17. Mod->appendRow(Item1);
    18.  
    19.  
    20. //Create and Add item2 to Mod
    21.  
    22.  
    23.  
    24. Item2->setData("ciao2",Qt::DisplayRole);
    25.  
    26. Mod->appendRow(Item2);
    To copy to clipboard, switch view to plain text mode 

    and in the delegate paint(), i get them using index & role

    Qt Code:
    1. QString headerText = qvariant_cast<QString>(index.data(Qt::DisplayRole));
    2. .....
    3. ......
    To copy to clipboard, switch view to plain text mode 

    and all what i aspect is working.....2 label in 2 line + a big rectangle around....Beautiful.

    My next question is:I want add button (for now to be simple, we dont speak about signal&slot)

    My idea was to add buttons to -Mod-, create a custom- role-,and identify button in paint() via my role.
    But i cant introduce any button in model.

    Next idea is to design button in the paint(); but this means also that in future i need to connect signal&slot
    at every paint..a lot of computation.

    What is your suggestion ?

    Thanks

    Roberto
    Last edited by robzanab; 26th November 2015 at 13:33.

  9. #9
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Add custom object to QListWidget

    but this means also that in future i need to connect signal&slot at every paint..a lot of computation.
    No, no, no, don't ever do that. Paint is for painting, nothing else.

    And you do not want to put anything like a button in the *model*. The model is independent of any place it is displayed. That's the whole purpose of the Model-View architecture, to separate those two things.

    In your delegate, you can use QStylePainter to draw the image of a pushbutton into the delegate's QRect (or part of it). You can also reimplement QAbstractItemDelegate::editorEvent() and watch for mouse events in the rect occupied by your pushbutton image. You could use those to simulate a click on your pushbutton, and emit a custom "clicked" signal that contains the model index (sent into editorEvent()) as a parameter. That lets you handle the signal in your own slot, and you'll have the index of the list widget element that triggered it.

  10. #10
    Join Date
    Nov 2015
    Posts
    7
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Add custom object to QListWidget

    Thanks Mr Stranz....

    editorEvent work good...If I press mouse on any text line i visualiza (2 line ),then i get the event and im able to understand what of the 2 i pressed.
    In particolar, the first line is easy to click, the second line no so precise.


    What I used for draw text is painter->drawtext(rect, text) , and rect is bad created...I have no control of it..

    ..This is part of code ,inside paint() event:

    Qt Code:
    1. ......
    2. QRect headerRect=option.rect ; //<--------
    3.  
    4. headerRect.setLeft(40);
    5.  
    6.  
    7. headerRect.setTop(headerRect.top()+150);
    8. headerRect.setBottom(headerRect.top()+130);
    9.  
    10. ....
    11. painter->drawText( headerRect ,headerText);
    To copy to clipboard, switch view to plain text mode 

    I cant manage this headerrect....I want that next headerrect start +150 compared to previous, hight 130.
    But i cant increase the distance bettwen 2 text....

    So question are:

    -why create a rectangle using option.rect().....Who is filling option object ?
    -why distance dont work.

    Slowly slowly all seems more clear...thanks
    Roberto

  11. #11
    Join Date
    Nov 2015
    Posts
    7
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Add custom object to QListWidget

    Hello

    I dont get any reply.

    Simplified question is:

    -Why inside the paint event, we must create a rectangle using - option.rect -

    Thanks

    Roberto

    Hello

    I dont get any reply.

    Simplified question is:

    -Why inside the paint event, we must create a rectangle using - option.rect -

    Thanks

    Roberto

Similar Threads

  1. QListWidget Custom item is not there
    By Archa4 in forum Newbie
    Replies: 2
    Last Post: 6th May 2011, 14:24
  2. Custom Items for QListWidget
    By Archa4 in forum Newbie
    Replies: 10
    Last Post: 2nd February 2011, 13:51
  3. How to add a custom item in a QlistWidget??
    By druidamix in forum Qt Programming
    Replies: 1
    Last Post: 16th September 2010, 21:19
  4. Custom scrolbar for QListWidget
    By nrabara in forum Newbie
    Replies: 2
    Last Post: 7th March 2010, 19:04
  5. QListWidget custom numbering ?
    By pshah.mumbai in forum Newbie
    Replies: 5
    Last Post: 14th July 2008, 15:29

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.