Results 1 to 4 of 4

Thread: MVC question

  1. #1
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    69
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default MVC question

    Hi,

    I'm trying to create a front-end GUI for my application. It consist on some lists coded in C++ for me. It have list objects that have other lists, ...

    Now I want to create a TreeView to show the data. I'm reading the Qt doc about MVC. I can see that I could create use a delegate to edit the data of the tree, but I don't know how to change my internal data.

    Another question, the method "createEditor" of the delegate could create a widget specific to the data that have to edit? Imagine that I have a treeItem that is an int, other treeItem that is a float, other that is a QString. Could it be possible?

    My internat data structure is changing by user interaction and I want to show the current state. There are items that will be deleted, others that will be created and inserted in it's properly position.

    Thanks,
    Last edited by ^NyAw^; 18th May 2007 at 17:56.
    Òscar Llarch i Galán

  2. #2
    Join Date
    May 2006
    Posts
    788
    Thanks
    49
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: MVC question

    Quote Originally Posted by ^NyAw^ View Post
    Hi,

    I'm trying to create a front-end GUI for my application. It consist on some lists coded in C++ for me. It have list objects that have other lists, ...

    Now I want to create a TreeView to show the data. I'm reading the Qt doc about MVC. I can see that I could create use a delegate to edit the data of the tree, but I don't know how to change my internal data.
    You can read data from sql text xml CSV to edit you write a model

    QStringListModel is used to store a simple list of QString items.
    QStandardItemModel manages more complex tree structures of items, each of which can contain arbitrary data.
    QDirModel provides information about files and directories in the local filing system.
    QSqlQueryModel, QSqlTableModel, and QSqlRelationalTableModel are used to access databases using model/view conventions.






    Another question, the method "createEditor" of the delegate could create a widget specific to the data that have to edit? Imagine that I have a treeItem that is an int, other treeItem that is a float, other that is a QString. Could it be possible?
    createEditor ....
    You can write each class wo is subclass from QWidget edit or not edit you set a correct flag... button QLabel combobox ecc... to edit or display...

    Qt::ItemFlags flags = QSqlQueryModel::flags(index);
    if (index.column() == 1 || index.column() > 11) {
    flags |= Qt::ItemIsSelectable;
    ..............

    Qt Code:
    1. QWidget *BaseDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
    2. {
    3. if (index.column() == 11 || index.column() == 7 ) {
    4. QDateTimeEdit *editor = new QDateTimeEdit(parent);
    5. editor->setDisplayFormat("dd.MM.yyyy");
    6. editor->setCalendarPopup(true);
    7. return editor;
    8. } else if (index.column() == 0) {
    9. Base_Button *editora = new Base_Button(parent);
    10. connect( editora , SIGNAL(OpenItem(int)), this , SLOT(Omodusa(int)));
    11. /* other connect flat button .... */
    12. return editora;
    13. } else {
    14. return QItemDelegate::createEditor(parent, option, index);
    15. }
    16. }
    To copy to clipboard, switch view to plain text mode 



    My internat data structure is changing by user interaction and I want to show the current state. There are items that will be deleted, others that will be created and inserted in it's properly position.
    ,
    Set flag to edit or only display ...


    Qt Code:
    1. bool SearchModel::setData(const QModelIndex &index, const QVariant &value, int role )
    2. {
    3. if (index.column() < 2 || index.column() > 11) {
    4. return false;
    5. }
    6.  
    7. QModelIndex primaryKeyIndex = QSqlQueryModel::index(index.row(), 0);
    8. int id = data(primaryKeyIndex).toInt();
    9. int lasteditingline = index.row();
    10. clear();
    11. bool ok;
    12. QString saxi;
    13. if (index.column() == 9) {
    14. saxi = QString("update TABLEX set LANGUAGE = '%1' where ID = '%2'").arg(value.toString().toUpper()).arg(id);
    15.  
    16. update query && return true or false...
    To copy to clipboard, switch view to plain text mode 


    Begin to play on MVC examples/sql sqlite3 is running fast and easy ....

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: MVC question

    Quote Originally Posted by ^NyAw^ View Post
    Now I want to create a TreeView to show the data. I'm reading the Qt doc about MVC. I can see that I could create use a delegate to edit the data of the tree, but I don't know how to change my internal data.
    You should reimplement setData() method in your model and change the structures there.

    Another question, the method "createEditor" of the delegate could create a widget specific to the data that have to edit? Imagine that I have a treeItem that is an int, other treeItem that is a float, other that is a QString. Could it be possible?
    You don't have to touch the delegate. The default delegate already handles that.

    I suggest to start with QStandardItemModel and store the data without any additional (custom) data structures. If you don't really need the custom structures for some other reason, the standard item model may be sufficient for you. It implements setData and makes items editable, so you don't have to write a single line of code. The only thing you have to do is to fill the model with data.

  4. #4
    Join Date
    Feb 2007
    Posts
    73
    Thanks
    11
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: MVC question

    If none of these models suite your purpose, you can create your own model, either derived form one of the Qt models, or the base class QAbstractItemModel.

Similar Threads

  1. Access to QSqlTableModel::isDirty Question.
    By patrik08 in forum Qt Programming
    Replies: 3
    Last Post: 12th April 2007, 18:49
  2. Replies: 1
    Last Post: 15th March 2007, 21:45
  3. Variable question
    By MarkoSan in forum General Programming
    Replies: 4
    Last Post: 15th March 2007, 15:59
  4. Question regarding how to paint after zoom.
    By JonathanForQT4 in forum Qt Programming
    Replies: 2
    Last Post: 26th January 2007, 16:34
  5. QThread exit()/quit() question
    By TheKedge in forum Qt Programming
    Replies: 1
    Last Post: 28th August 2006, 15:38

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.