Results 1 to 9 of 9

Thread: QListWidget question

  1. #1
    Join Date
    Jan 2008
    Posts
    27
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QListWidget question

    Hello,

    I am just learning Qt and have a question regarding the QListWidget. I am trying to build one with a user-made widget list.

    I might not be using the correct widget for my purpose maybe someone can suggest a better one. I want to essentially create a list of items using a custom widget I built for the items. I also need to select the items and bring up more details about it, so that is why I chose a ListView

    I have attached an image of what I want it to look like. The thing on the right is supposed to be a scrollbar.

    Thanks for the help.
    RishiD
    Attached Images Attached Images

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QListWidget question

    but what is the question?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Jan 2008
    Posts
    27
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QListWidget question

    Guess I am trying to figure out what type of layout / container to use that will work for me, that will provide a layout similar to the attached image above?

    Only requirements I have for the container are basically, lets me add a custom designed widget to it, will vertically scroll when needed, and allow me to perform an action each item is clicked.

    After some more reading I am thinking of creating a widget that inherits from QListWidgetItem and that should let me use my custom widget in a QListWidget.

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QListWidget question

    After some more reading I am thinking of creating a widget that inherits from QListWidgetItem and that should let me use my custom widget in a QListWidget.
    I would say so too.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. The following user says thank you to high_flyer for this useful post:

    rishid (17th January 2008)

  6. #5
    Join Date
    Jan 2008
    Posts
    27
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QListWidget question

    Do you by any chance have some code/example on inheriting from QListWidgetItem?

    Thanks for the help.

  7. #6
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QListWidget question

    You might check the examples that come with Qt.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  8. #7
    Join Date
    Jan 2008
    Posts
    27
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QListWidget question

    I have been messing around with it and may have found a problem. Since QListWidgetItem is not a derived from QWidget, I cannot seem to create a custom UI for it. Is this the right container to use? I wish vboxLayout had a scrollbar feature because that would work perfectly.

    Note: this does not work because QLabel constructor only accepts a QWidget as it's argument, where "this" does not work for that.

    Here is my code so far:

    Qt Code:
    1. #include <QListWidget>
    2. #include <QListWidgetItem>
    3.  
    4. //#include "ui_NameEntry.h"
    5.  
    6. class NameEntry : public QListWidgetItem {
    7.  
    8. public:
    9. QLabel *lblName;
    10.  
    11. NameEntry(const QString & text, QListWidget * parent = 0) :
    12. QListWidgetItem(text, parent, 1001) {
    13. lblName = new QLabel(this);
    14. lblName->setObjectName(QString::fromUtf8("lblName"));
    15. lblName->setGeometry(QRect(80, 10, 221, 31));
    16. QFont font;
    17. font.setPointSize(14);
    18. lblName->setFont(font);
    19. }
    20.  
    21. ~NameEntry() {
    22.  
    23. }
    24.  
    25. private:
    26. //Ui::NameEntryClass ui;
    27. };
    To copy to clipboard, switch view to plain text mode 

  9. #8
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QListWidget question

    Just add the labels in a QVBoxLayout, install the layout on a plain QWidget and set it inside a QScrollArea.
    J-P Nurmi

  10. #9
    Join Date
    Jan 2008
    Posts
    27
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QListWidget question

    Quote Originally Posted by jpn View Post
    Just add the labels in a QVBoxLayout, install the layout on a plain QWidget and set it inside a QScrollArea.
    Ok, I will try this next. Thanks for the tip.

    Edit: Got it working, only problem with it is that I cannot dynamically add or remove widgets from the box layout. Have to re-create the entire widget and all child widgets each time. I will have to try to find a better way. Here is the code I used for future knowledge.

    Qt Code:
    1. // ui.frame is a QScrollArea *
    2.  
    3. QPushButton * qpb1 = new QPushButton("Jack");
    4. QPushButton * qpb2 = new QPushButton("Jack");
    5. QPushButton * qpb3 = new QPushButton("Jack");
    6. QPushButton * qpb4 = new QPushButton("Jack");
    7. QPushButton * qpb5 = new QPushButton("Jack");
    8.  
    9.  
    10. QWidget * qw = new QWidget();
    11. QVBoxLayout * layout = new QVBoxLayout();
    12. layout->addWidget(qpb1);
    13. qw->setLayout(layout);
    14. ui.frame->setWidget(qw);
    15.  
    16. delete qw;
    17.  
    18. QWidget * qwx = new QWidget();
    19. QVBoxLayout * layoutx = new QVBoxLayout();
    20. layoutx->addWidget(qpb5);
    21. layoutx->addWidget(qpb2);
    22. layoutx->addWidget(qpb3);
    23. layoutx->addWidget(qpb4);
    24.  
    25. qwx->setLayout(layoutx);
    26. ui.frame->setWidget(qwx);
    To copy to clipboard, switch view to plain text mode 
    Last edited by rishid; 18th January 2008 at 17:37.

Similar Threads

  1. Access to QSqlTableModel::isDirty Question.
    By patrik08 in forum Qt Programming
    Replies: 3
    Last Post: 12th April 2007, 18:49
  2. Replies: 1
    Last Post: 15th March 2007, 21:45
  3. Question regarding how to paint after zoom.
    By JonathanForQT4 in forum Qt Programming
    Replies: 2
    Last Post: 26th January 2007, 16:34
  4. QThread exit()/quit() question
    By TheKedge in forum Qt Programming
    Replies: 1
    Last Post: 28th August 2006, 15:38

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.