Active Topics

 


Reply
Thread Tools
mikhail_ramalho's Avatar
Posts: 76 | Thanked: 87 times | Joined on Mar 2010
#1
Hello guys, I’m trying to create a generic list using QGraphicsWidget and QGraphicsLinearLayout but I’m having some problem as it doesn’t scroll when I activate the QtScroller.

Here is the code:

Code:
#include <QtCore>
#include <QtGui>
#include <QtScroller>
#include <QtScrollEvent>
#include <QtScrollPrepareEvent>
 
class KineticList : public QGraphicsWidget
{
    Q_OBJECT
 
public:
    enum Orientation { Horizontal, Vertical };
 
    KineticList(Orientation orientation, QSizeF size, QGraphicsWidget *parent = 0)
        : QGraphicsWidget(parent)
    {
        this->setMinimumSize(size);
        this->setFlag(QGraphicsItem::ItemHasNoContents, true);
        this->setFlag(QGraphicsItem::ItemClipsChildrenToShape, true);
        this->setFlag(QGraphicsItem::ItemClipsToShape, true);
 
        if (orientation == KineticList::Horizontal) {
            _layout = new QGraphicsLinearLayout(Qt::Horizontal);
            _layout->setMinimumHeight(size.height());
        } else {
            _layout = new QGraphicsLinearLayout(Qt::Vertical);
            _layout->setMinimumWidth(size.width());
        }
        _layout->setSpacing(3);
        _layout->setContentsMargins(2, 0, 2, 0);
 
        _list = new QGraphicsWidget(this);
        _list->setLayout(_layout);
    }
 
    void addItem(QGraphicsWidget *item)
    {
        _layout->insertItem(0, item);
    }
 
private:
    QGraphicsWidget *_list;
    QGraphicsLinearLayout *_layout;
};
 
class RectWidget : public QGraphicsWidget
{
    Q_OBJECT
 
public:
 
    RectWidget(const QString &text;, qreal width, qreal height, QBrush brush, QGraphicsItem *parent = 0)
        : QGraphicsWidget(parent)
        , m_text(text)
        , m_rect(0, 0, width, height)
        , m_pen(brush.color().lighter(), 3.0)
        , m_brush(brush)
    {
        setFlag(QGraphicsItem::ItemClipsToShape, true);
        setMinimumSize(width, height);
        setMaximumSize(width, height);
        setGeometry(0, 0, width, height);
    }
 
    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    {
        Q_UNUSED(option);
        Q_UNUSED(widget);
        painter->setPen(m_pen);
        painter->setBrush(m_brush);
        painter->drawRect(m_rect);
 
        painter->setPen(Qt::black);
        QFont f;
        f.setPixelSize(m_rect.height());
        painter->setFont(f);
        painter->drawText(m_rect, Qt::AlignCenter, m_text);
    }
 
    QString m_text;
    QRectF m_rect;
    QPen m_pen;
    QBrush m_brush;
};
 
#define NUM_ITEMS 100
 
int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
 
    QGraphicsScene scene(0, 0, 800, 480);
    QGraphicsView view(&scene;);
 
    view.setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    view.setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    view.setFrameStyle(0);
    view.show();
 
    QPalette p = view.viewport()->palette();
    p.setColor(QPalette::Base, Qt::transparent);
    view.viewport()->setPalette(p);
 
    KineticList *list = new KineticList(KineticList::Vertical, QSizeF(200, 200));
    scene.addItem(list);
 
    for(int i = 0; i < NUM_ITEMS; ++i) {
        QColor color =  QColor(255*i/NUM_ITEMS, 255*(NUM_ITEMS-i)/NUM_ITEMS, 127*(i%2)+64*(i/2%2));
        QString text = QLatin1String("Item #") + QString::number(i);
        RectWidget *rect = new RectWidget(text, 200, 30, QBrush(color));
        list->addItem(rect);
    }
 
    QtScrollerProperties sp;
    QtScrollerProperties::setDefaultScrollerProperties(sp);
 
    QtScroller *scroller = QtScroller::scroller(list);
    scroller->setScrollerProperties(sp);
    scroller->grabGesture(list, QtScroller::LeftMouseButtonGesture);
 
    return app.exec();
}

I created it based on the graphicsview example on qtscroller repository.

Maybe there is some problem using QGraphicsLinearLayout? I’m using it because it’s easier to erase item as it auto arrange them on the widget.

Thanks
__________________
TweeGo v0.6.5-1 just released!

Download it at Maemo Garage or at extras-devel
 
Reply


 
Forum Jump


All times are GMT. The time now is 04:10.