Results 1 to 20 of 47

Thread: A few queries about Model View Programming

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Oct 2007
    Location
    India
    Posts
    162
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    20
    Thanked 6 Times in 5 Posts

    Question A few queries about Model View Programming

    I have a database consisting of a table with some fields. I am using QSqlTableModel to get the data from the database and displaying it in a QTableView. Now, the database has 3 fields i.e. tracks, artist and time. All of the data in these fields is coming from the model and being displayed properly.
    I have added another column to the QTableView consisting of QCheckBox for each row (See attached image). The QCheckBox is not part of the database or model. I am using the following code for attaching QCheckBox to each row:

    Qt Code:
    1. void MyClass::createCheckItems(QTableView *table)
    2. {
    3. for (int i = 0; i < m_pModel->rowCount(); ++i) {
    4. QCheckBox *cbox = new QCheckBox(QObject::tr (""));
    5. // Enable autofill of the widget background to hide the model data
    6. cbox->setAutoFillBackground(TRUE);
    7. // Set up slots
    8. connect(cbox, SIGNAL(stateChanged(int)), this, SLOT(checkBoxClicked(int)));
    9. // Add checkboxes in the 4th column of QTableView
    10. table->setIndexWidget(m_pModel->index(i, 3), cbox);
    11. }
    12. }
    To copy to clipboard, switch view to plain text mode 

    I am able to retrieve the current row and get the track name when the user clicks on a check box.

    My queries:
    1) I am using setIndexWidget() to display checkboxes in the 4th column and my code is working fine. Is it ok or should i use QItemDelegate to add checboxes?
    2) Currently the QTableView displays data directly received from the model (database). But suppose, i want to format the data received from the model before displaying on the QTableView, how can i do it? For e.g. suppose the "time" field is stored as seconds in the database and i want to display it in "minutes:seconds" in the QTableView, how can i do that?
    3) I want to do custom drawing in the "tracks" field, for e.g. the currently playing track might have an icon next to the track name. How to do that?

    Thanks in advance!
    Attached Images Attached Images
    • File Type: png 1.png (24.1 KB, 241 views)

Similar Threads

  1. hierarchical model in a flat view
    By gniking in forum Qt Programming
    Replies: 4
    Last Post: 10th November 2009, 20:17
  2. model View programming problem
    By mismael85 in forum Qt Programming
    Replies: 3
    Last Post: 2nd April 2008, 21:44
  3. Model, View and Proxy
    By No-Nonsense in forum Qt Programming
    Replies: 2
    Last Post: 21st November 2006, 08:50
  4. Model - View Programming doubt.
    By munna in forum Qt Programming
    Replies: 4
    Last Post: 28th April 2006, 13:01
  5. Replies: 6
    Last Post: 20th April 2006, 10:23

Tags for this Thread

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.