Help me please...
I really need this, please help me.
Atão pah!!! Respondei-me a essa cena majé!! =)
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.
Hello my friend,
I now have this:
mainwindow.jpg
and if you do this code mainwindow.cpp
Qt Code:
TaskModel model(5); QListView list; list.setModel( &model ); 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:
TarefaModel model(5); ui->listView->setModel(&model);To copy to clipboard, switch view to plain text mode
the program unexpectedly quits, why?
because your model is destroyed at the end of the function.
if you put ui->listView->setModel(new TarefaModel(5, this));
does it work ?
plopes21 (2nd May 2012)
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:
void MainWindow::on_addTarefa_clicked() { Task t1(ui->lineNameTask->text()); // t1.setContext(ui->LineContext->text()); // t1.setProject(ui->lineProject->text()); TaskModel model(5); model.addTask(t1); // TaskModel::taskList.push_back(t1); }To copy to clipboard, switch view to plain text mode
Qt Code:
void TaskModel::addTask(const Task &task){ qDebug("hello"); // taskList<< task; taskList.push_back(task); endInsertRows(); }To copy to clipboard, switch view to plain text mode
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
plopes21 (2nd May 2012)
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.
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
plopes21 (2nd May 2012)
I'll try to solve this problem and will post here some questions that will arise.
Thanks Le_B
Bookmarks