Difference between revisions of "Development/Tutorials/Introduction to Goya usage"

Jump to: navigation, search
m (Text replace - "</code>" to "</syntaxhighlight>")
(One intermediate revision by one user not shown)
Line 19: Line 19:
This example consists on a single window that will contain a list view. There will be pushbuttons only in the odd rows.
This example consists on a single window that will contain a list view. There will be pushbuttons only in the odd rows.
<code cppqt>
<syntaxhighlight lang="cpp-qt">
// Basic Goya includes
// Basic Goya includes
#include <goya/kwidgetitemdelegate.h>
#include <goya/kwidgetitemdelegate.h>
Line 180: Line 180:
#include "main.moc"
#include "main.moc"

Latest revision as of 20:51, 29 June 2011

Introduction to the Goya Framework usage
Tutorial Series   Goya Framework
Previous   C++, Qt, Model/View Qt Framework, KDE4 development environment
What's Next   A slightly more complex example
Further Reading   n/a

[edit] Abstract

We are developing some component of our application using Model/View (check prerequisites). At some point on our development, we discover that we actually want to add widgets to our delegate, but the Model/View framework does not provide a powerful and integrated way of doing so. Here is where Goya comes to help out for this task.

We could say Goya is a layer between the view and your delegate that draws widgets with the needed options and that seem to behave as if they were real widgets, but they are fake widgets after all.

Goya is so nice mainly because it integrates pretty well with the Model/View design, and uses the Qt powerful signals and slots. Goya widgets will emit signals when something has happened to them, so you will be able to connect those signals to your app slots, and do fancy stuff without complex stuff.

[edit] A Simple Example

This example consists on a single window that will contain a list view. There will be pushbuttons only in the odd rows.

// Basic Goya includes
#include <goya/kwidgetitemdelegate.h>
// Basic Qt includes
#include <QPainter>
#include <QBoxLayout>
#include <QListView>
#include <QStringListModel>
// Basic KDE includes
#include <kapplication.h>
#include <kaboutdata.h>
#include <kmessagebox.h>
#include <kcmdlineargs.h>
#include <klocalizedstring.h>
#include <kicon.h>
// This is our delegate, it has to inherit KWidgetItemDelegate which inherits
// QAbstractItemDelegate. The delegate needs on its constructor first
// parameter a view, this is different to a regular QAbstractItemDelegate
// because we need to install event filters in order to allow interaction
// with the user.
class MiDelegate
    : public KWidgetItemDelegate
    MiDelegate(QAbstractItemView *itemView, QObject *parent = 0)
        : KWidgetItemDelegate(itemView, parent)
    virtual ~MiDelegate()
    QList<QWidget*> createItemWidgets() const
        return QList<QWidget*>() << new QPushButton();
    void initializeItemWidgets(const QList<QWidget*> widgets,
                               const QStyleOptionViewItem &option,
                               const QModelIndex &index) const
        QPushButton *button = static_cast<QPushButton*>(widgets.at(0));
        button->setText("More Information");
        button->setIconSize(QSize(16, 16));
        connect(button, SIGNAL(clicked(QModelIndex,const Goya::PushButton*)),
                this, SLOT(slotClicked(QModelIndex)));
    void paint(QPainter *painter, const QStyleOptionViewItem &option,
               const QModelIndex &index) const
        if (option.state & QStyle::State_Selected)
            painter->fillRect(option.rect, option.palette.highlight());
        if (option.state & QStyle::State_Selected)
        painter->drawText(option.fontMetrics.height() + option.rect.left(),
                          option.fontMetrics.height() * 3 + 
                          Canvas::sizeHint(option, index).height() +
                          QString("This is the index in row number ") + 
                          QString::number(index.row() + 1));
        KWidgetItemDelegate::paintWidgets(painter, option, index);
    QSize sizeHint() const
        return QSize(600, 60);
private Q_SLOTS:
    void slotClicked(const QModelIndex &index)
        KMessageBox::information(0, "More information clicked on row " + 
                                 QString::number(index.row() + 1),
                                 "Button clicked");
int main(int argc, char **argv)
    KAboutData aboutData("goyatest",
                         ki18n("Goya Test "),
                         ki18n("A test for the Goya subsystem"),
                         ki18n("(c) Rafael Fernández López, 2007"),
                         ki18n("A test for the Goya subsystem"),
    KCmdLineArgs::init(argc, argv, &aboutData);
    KApplication app;
    QWidget *widget = new QWidget();
    QVBoxLayout *layout = new QVBoxLayout;
    widget->resize(800, 600);
    QListView *listView = new QListView();
    QStringListModel *model = new QStringListModel();
    MiDelegate *delegate = new MiDelegate(listView);
    QListView *listView2 = new QListView();
    QStringListModel *model2 = new QStringListModel();
    MiDelegate *delegate2 = new MiDelegate(listView2);
    for (int i = 0; i < 1000; ++i)
        model->setData(model->index(i, 0), QString::number(i));
    for (int i = 0; i < 10; ++i)
        model2->setData(model2->index(i, 0), QString::number(i));
    layout->addWidget(new QPushButton("Above Button"));
    layout->addWidget(new QPushButton("Below Button"));
    return app.exec();
#include "main.moc"

This page was last modified on 29 June 2011, at 20:51. This page has been accessed 16,322 times. Content is available under Creative Commons License SA 3.0 as well as the GNU Free Documentation License 1.2.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V.Legal