PDA

View Full Version : QListWidget question



rishid
17th January 2008, 15:13
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

high_flyer
17th January 2008, 16:13
but what is the question? :)

rishid
17th January 2008, 16:16
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.

high_flyer
17th January 2008, 16:25
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.

rishid
17th January 2008, 16:55
Do you by any chance have some code/example on inheriting from QListWidgetItem?

Thanks for the help.

high_flyer
17th January 2008, 17:24
You might check the examples that come with Qt.

rishid
17th January 2008, 19:09
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:



#include <QListWidget>
#include <QListWidgetItem>

//#include "ui_NameEntry.h"

class NameEntry : public QListWidgetItem {

public:
QLabel *lblName;

NameEntry(const QString & text, QListWidget * parent = 0) :
QListWidgetItem(text, parent, 1001) {
lblName = new QLabel(this);
lblName->setObjectName(QString::fromUtf8("lblName"));
lblName->setGeometry(QRect(80, 10, 221, 31));
QFont font;
font.setPointSize(14);
lblName->setFont(font);
}

~NameEntry() {

}

private:
//Ui::NameEntryClass ui;
};

jpn
18th January 2008, 09:18
Just add the labels in a QVBoxLayout, install the layout on a plain QWidget and set it inside a QScrollArea.

rishid
18th January 2008, 14:50
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.




// ui.frame is a QScrollArea *

QPushButton * qpb1 = new QPushButton("Jack");
QPushButton * qpb2 = new QPushButton("Jack");
QPushButton * qpb3 = new QPushButton("Jack");
QPushButton * qpb4 = new QPushButton("Jack");
QPushButton * qpb5 = new QPushButton("Jack");


QWidget * qw = new QWidget();
QVBoxLayout * layout = new QVBoxLayout();
layout->addWidget(qpb1);
qw->setLayout(layout);
ui.frame->setWidget(qw);

delete qw;

QWidget * qwx = new QWidget();
QVBoxLayout * layoutx = new QVBoxLayout();
layoutx->addWidget(qpb5);
layoutx->addWidget(qpb2);
layoutx->addWidget(qpb3);
layoutx->addWidget(qpb4);

qwx->setLayout(layoutx);
ui.frame->setWidget(qwx);