Results 1 to 20 of 20

Thread: Custom ListView. Using the model / view framework.

  1. #1
    Join Date
    Apr 2012
    Location
    Porto
    Posts
    38
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Custom ListView. Using the model / view framework.

    Good morning people,

    I have to do a custom listview. But I do not understand how to make the view / model of the QT framework.
    I want to do something like this:
    http://qt-apps.org/content/show.php/...?content=99087
    The application has a custom listview. Have I downloaded the source code, but can not understand how he does. He liked to do as he did, a "item.ui" where he designed to put your item in the listview.

    Regards, Pedro Lopes

  2. #2
    Join Date
    Nov 2011
    Posts
    79
    Thanks
    5
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Custom ListView. Using the model / view framework.

    You have to implement QAbstractListModel to provide data for your list and also inplement from QAbstractItemDelegate to provide custom view of items.
    You can find lot of examples in Qt Assistant

  3. The following user says thank you to folibis for this useful post:

    plopes21 (27th April 2012)

  4. #3
    Join Date
    Apr 2012
    Location
    Porto
    Posts
    38
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Custom ListView. Using the model / view framework.

    Thank you for your response, I will do something and if not put to understand something here.

    thank folibis

  5. #4
    Join Date
    Apr 2012
    Location
    Porto
    Posts
    38
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Custom ListView. Using the model / view framework.

    Good evening,
    So far did this:
    Files .h
    mainwindow.h
    tarefaDelegate.h
    tarefa.h
    tarefaModel.h
    tarefaWidget.h

    Files .cpp
    main.cpp
    mainwindow.cpp
    tarefaDelegate.cpp
    tarefaModel.cpp
    tarefaWidget.cpp
    tarefa.cpp

    Forms
    mainwindow.ui
    tarefaWidget.ui (picture below)

    itemList.jpg

    First want to make the Model. For now I have this
    Qt Code:
    1. #ifndef TAREFAMODEL_H
    2. #define TAREFAMODEL_H
    3. #include <QAbstractListModel>
    4. #include <tarefa.h>
    5. #include <QList>
    6. #include <QListView>
    7.  
    8. class TarefaModel : public QAbstractListModel
    9. {
    10. Q_OBJECT
    11. public:
    12. TarefaModel(QList<Tarefa> &tarefas, QObject *parent = 0)
    13. : QAbstractListModel(parent), listatarefas(tarefas) {}
    14.  
    15.  
    16. int rowCount(const QModelIndex &parent=QModelIndex() ) const;
    17. QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const;
    18.  
    19. bool insertRows(int position, int rows, const QModelIndex &index = QModelIndex());
    20. bool removeRows(int position, int rows, const QModelIndex &index = QModelIndex());
    21.  
    22. private:
    23. QList<Tarefa> listatarefas;
    24. };
    25.  
    26.  
    27.  
    28. #endif // TAREFAMODEL_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include "tarefamodel.h"
    2. #include <QTime>
    3. #include <tarefa.h>
    4. #include <QList>
    5.  
    6.  
    7.  
    8. int TarefaModel::rowCount(const QModelIndex& parent) const
    9. {
    10. return this->listatarefas.count();
    11. }
    12.  
    13.  
    14. QVariant TarefaModel::data(const QModelIndex &index, int role) const
    15. {
    16. if (!index.isValid())
    17. return QVariant();
    18.  
    19. if (index.row() >= listatarefas.size())
    20. return QVariant();
    21.  
    22. if (role == Qt::DisplayRole)
    23. return listatarefas.at(index.row());
    24. else
    25. return QVariant();
    26. }
    27.  
    28. bool TarefaModel::insertRows(int position, int rows, const QModelIndex &parent)
    29. {
    30. beginInsertRows(QModelIndex(), position, position+rows-1);
    31.  
    32. for (int row = 0; row < rows; ++row) {
    33. Tarefa *tarefa1 = new Tarefa("primeira tarefa");
    34. listatarefas.insert(position, *tarefa1);
    35. }
    36.  
    37. endInsertRows();
    38. return true;
    39. }
    40.  
    41. bool TarefaModel::removeRows(int position, int rows, const QModelIndex &parent)
    42. {
    43. beginRemoveRows(QModelIndex(), position, position+rows-1);
    44.  
    45. for (int row = 0; row < rows; ++row) {
    46. listatarefas.removeAt(position);
    47. }
    48.  
    49. endRemoveRows();
    50. return true;
    51. }
    To copy to clipboard, switch view to plain text mode 

    Tarefa.h
    Qt Code:
    1. #ifndef TAREFA_H
    2. #define TAREFA_H
    3.  
    4. #include <QString>
    5. using namespace std;
    6.  
    7. class Tarefa{
    8. private:
    9. QString name;
    10. QString context;
    11. QString project;
    12.  
    13. public:
    14. //construtor
    15. Tarefa(QString name);
    16.  
    17. void setName(QString name);
    18. void setContext(QString context);
    19. void setProject(QString project);
    20.  
    21.  
    22. QString getName();
    23. QString getContext();
    24. QString getProjec();
    25. };
    26.  
    27. #endif // TAREFA_H
    To copy to clipboard, switch view to plain text mode 

    Is anything wrong?

    thanks for all!

  6. #5
    Join Date
    Nov 2011
    Posts
    79
    Thanks
    5
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Custom ListView. Using the model / view framework.

    if your list is read only so rowCount() and data() will be enough.

  7. #6
    Join Date
    Apr 2012
    Location
    Porto
    Posts
    38
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Custom ListView. Using the model / view framework.

    I have the following ui's:

    listview
    qt1.jpg

    items
    qt2.jpg

    I have to use the class QAbstractItemDelegate like you said. But I do not know how to use. This class does what?

    I do not understand very well. Thanks

  8. #7
    Join Date
    Apr 2012
    Location
    Porto
    Posts
    38
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Custom ListView. Using the model / view framework.

    Help me please...

  9. #8
    Join Date
    Apr 2012
    Location
    Porto
    Posts
    38
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Custom ListView. Using the model / view framework.

    I really need this, please help me.

  10. #9
    Join Date
    Apr 2012
    Location
    Porto
    Posts
    38
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Custom ListView. Using the model / view framework.

    Atão pah!!! Respondei-me a essa cena majé!! =)

  11. #10
    Join Date
    Nov 2011
    Posts
    79
    Thanks
    5
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Custom ListView. Using the model / view framework.

    Ok, you had implemented QAbstractListModel so now you have common list which can display rows of text.
    But you need different view of your items.
    For than reason you need to inplement QAbstractItemDelegate and set it to your model.
    You new class have to implement 2 methods:
    - void paint ( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const
    - QSize sizeHint ( const QStyleOptionViewItem & option, const QModelIndex & index ) const;
    sizeHint returns bounds of item, specified by index and inside paint() you just paint your item as you want with painter.

    very simple.
    You can find an example in Qt help, Pixelator or something like that

    P.S. I see now than you item have controls. but you cannot insert control directly in the list.
    Anyway you can draw it with
    void QStyle::drawControl ( ControlElement element, const QStyleOption * option, QPainter * painter, const QWidget * widget = 0 ) const
    Last edited by folibis; 2nd May 2012 at 00:31.

  12. #11
    Join Date
    Apr 2012
    Location
    Porto
    Posts
    38
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Custom ListView. Using the model / view framework.

    Hello my friend,

    I now have this:
    mainwindow.jpg

    and if you do this code mainwindow.cpp

    Qt Code:
    1. TaskModel model(5);
    2. QListView list;
    3. list.setModel( &model );
    4. list.show();
    To copy to clipboard, switch view to plain text mode 

    a list appears with the correct values​​.

    but if you do this
    Qt Code:
    1. TarefaModel model(5);
    2. ui->listView->setModel(&model);
    To copy to clipboard, switch view to plain text mode 

    the program unexpectedly quits, why?

  13. #12
    Join Date
    Nov 2010
    Posts
    82
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Custom ListView. Using the model / view framework.

    because your model is destroyed at the end of the function.
    if you put ui->listView->setModel(new TarefaModel(5, this));
    does it work ?

  14. The following user says thank you to Le_B for this useful post:

    plopes21 (2nd May 2012)

  15. #13
    Join Date
    Apr 2012
    Location
    Porto
    Posts
    38
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Custom ListView. Using the model / view framework.

    Your code worked.

    I have another problem. When I click on the pushbutton 'Add Task' will want to add a task list that is declared in taskModel.
    I have this code but does not work ...

    Qt Code:
    1. void MainWindow::on_addTarefa_clicked()
    2. {
    3. Task t1(ui->lineNameTask->text());
    4. // t1.setContext(ui->LineContext->text());
    5. // t1.setProject(ui->lineProject->text());
    6. TaskModel model(5);
    7. model.addTask(t1);
    8.  
    9. // TaskModel::taskList.push_back(t1);
    10. }
    To copy to clipboard, switch view to plain text mode 


    Qt Code:
    1. void TaskModel::addTask(const Task &task){
    2. beginInsertRows(QModelIndex(), rowCount(),rowCount());
    3. qDebug("hello");
    4. // taskList<< task;
    5. taskList.push_back(task);
    6. endInsertRows();
    7. }
    To copy to clipboard, switch view to plain text mode 

  16. #14
    Join Date
    Nov 2010
    Posts
    82
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Custom ListView. Using the model / view framework.

    same thing here.
    you need to control the life of your objects:

    if you write:
    {
    TarefaModel model(5);
    //it lives
    }
    //it s dead

    so in your exemple you create a whole new model add an item to it and the it is destroyed at the end of your function
    1) why do you recreate a new model every time : create one model and control the content is way better.
    2) if you need to create your models use new and pass this as the parent so it will be destroyed by the parent

  17. The following user says thank you to Le_B for this useful post:

    plopes21 (2nd May 2012)

  18. #15
    Join Date
    Apr 2012
    Location
    Porto
    Posts
    38
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Custom ListView. Using the model / view framework.

    I managed to add.
    Now I am working with QAbstractItemDelegate.

    I want to use this item that I did:
    item.jpg

    I really have to use QAbstractItemDelegate?

    How do I set up my item with QAbstractItemDelegate?

    Thanks
    Last edited by plopes21; 2nd May 2012 at 16:19.

  19. #16
    Join Date
    Nov 2010
    Posts
    82
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Custom ListView. Using the model / view framework.

    i think it s the way but i don't know much about delegates (i switched to QML it is way easier for that)
    but this exemple sould help you:
    http://qt-project.org/doc/qt-4.8/ite...rdelegate.html

  20. The following user says thank you to Le_B for this useful post:

    plopes21 (2nd May 2012)

  21. #17
    Join Date
    Apr 2012
    Location
    Porto
    Posts
    38
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Custom ListView. Using the model / view framework.

    I'll try to solve this problem and will post here some questions that will arise.

    Thanks Le_B

  22. #18
    Join Date
    Apr 2012
    Location
    Porto
    Posts
    38
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Custom ListView. Using the model / view framework.

    How can I put a distance between the margin items?

    I like this:

    itemsn.jpg

  23. #19
    Join Date
    Apr 2012
    Location
    Porto
    Posts
    38
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Custom ListView. Using the model / view framework.

    answer me please.

  24. #20
    Join Date
    May 2012
    Posts
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Custom ListView. Using the model / view framework.

    Hi plopes21,

    I think you need to add Vertical Layout to all those labels in tarefaWidget.ui.

    I am working something quite close to what you are doing and would you mind to upload your project files to have a look on how you put everything into one piece?
    Thanks

Similar Threads

  1. Replies: 9
    Last Post: 26th May 2011, 12:29
  2. Custom Model? Custom View? Custom Delegate?
    By Doug Broadwell in forum Newbie
    Replies: 4
    Last Post: 11th February 2010, 20:23
  3. Data structure reference in Model/View Framework
    By jimc1200 in forum Qt Programming
    Replies: 4
    Last Post: 14th September 2009, 20:23
  4. Model/View framework: streaming data in a QTableView
    By yannickt in forum Qt Programming
    Replies: 6
    Last Post: 24th October 2008, 00:06
  5. Model-view: Creating a custom view
    By taboom in forum Qt Programming
    Replies: 5
    Last Post: 17th August 2007, 20:36

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.