Results 1 to 20 of 20

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

Hybrid View

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

    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

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

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

    Help me please...

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

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

    I really need this, please help me.

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

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

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

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

    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 01:31.

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

    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?

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

    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 ?

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

    plopes21 (2nd May 2012)

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

    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 

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

    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

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

    plopes21 (2nd May 2012)

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

    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 17:19.

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

    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

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

    plopes21 (2nd May 2012)

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

    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

Similar Threads

  1. Replies: 9
    Last Post: 26th May 2011, 13:29
  2. Custom Model? Custom View? Custom Delegate?
    By Doug Broadwell in forum Newbie
    Replies: 4
    Last Post: 11th February 2010, 21:23
  3. Data structure reference in Model/View Framework
    By jimc1200 in forum Qt Programming
    Replies: 4
    Last Post: 14th September 2009, 21:23
  4. Model/View framework: streaming data in a QTableView
    By yannickt in forum Qt Programming
    Replies: 6
    Last Post: 24th October 2008, 01:06
  5. Model-view: Creating a custom view
    By taboom in forum Qt Programming
    Replies: 5
    Last Post: 17th August 2007, 21: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
  •  
Qt is a trademark of The Qt Company.