Results 1 to 11 of 11

Thread: Add custom object to QListWidget

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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 12:33.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,327
    Thanks
    316
    Thanked 871 Times in 858 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.

  3. #3
    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

  4. #4
    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, 13:24
  2. Custom Items for QListWidget
    By Archa4 in forum Newbie
    Replies: 10
    Last Post: 2nd February 2011, 12:51
  3. How to add a custom item in a QlistWidget??
    By druidamix in forum Qt Programming
    Replies: 1
    Last Post: 16th September 2010, 20:19
  4. Custom scrolbar for QListWidget
    By nrabara in forum Newbie
    Replies: 2
    Last Post: 7th March 2010, 18:04
  5. QListWidget custom numbering ?
    By pshah.mumbai in forum Newbie
    Replies: 5
    Last Post: 14th July 2008, 14: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
  •  
Qt is a trademark of The Qt Company.