Results 1 to 5 of 5

Thread: QTableView - model / view pattern - layout, edit

  1. #1
    Join Date
    Jul 2009
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QTableView - model / view pattern - layout, edit

    Hi folks!

    I've started using Qt couple of days ago and have some issues I'm not able to solve anyhow and I would really appreciate someone's help.

    I'm using mode / view pattern with QTableView, showing and editing my data works well, but:

    1. How do I add (or delete) a row in the QTableView visual interface? I did it with some buttons and signals to my model but is there any other possibility?

    2. My QTableView is expanding dynamically and I want the row to use all possible space but not more so I'll have to scroll left or right. I tried returning different values from data(..) and headerData(..) functions to Qt::SizeHintRole arguments but with no effect.

    3. I'm quite familiar with model / view pattern but how can help me a delegate?

    Thanks for any response or tip.

  2. #2
    Join Date
    Oct 2008
    Location
    Europe
    Posts
    37
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTableView - model / view pattern - layout, edit


  3. #3
    Join Date
    Jul 2009
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTableView - model / view pattern - layout, edit

    Thank for your quick response.

    I went through this text and the result is this.

    ad 1: I implemented insertRows(..) function but how to make the QTableView to call it while running?

    ad 2: I fixed returning Qt::SizeHintRole however it's never called in function data(..) and returning proper values in headerData(..) doesn't make sense because it resize only the header columns not the geometry of data columns at all.

  4. #4
    Join Date
    Jul 2009
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTableView - model / view pattern - layout, edit

    So I probably partly solved #1 by overriding QTableView to my own class and reimplementing some event handlers.

    But I still need to know how to tell rows / columns to use all available space but not more -(

  5. #5
    Join Date
    Jul 2009
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTableView - model / view pattern - layout, edit

    So I solved almost everything by overriding the QTableView class.

    To add or delete a row I overrode the keyPressEvent(..) and implemented insertRows(..) in the model.

    To dynamically resize columns by percents I did following.

    in the model->data(..)
    {
    ...
    case Qt::SizeHintRole:
    switch(index.column())
    {
    case 0:
    return QVariant(40);
    break;

    case 1:
    return QVariant(40);
    break;

    case 2:
    //in the last column is used the rest of available space, 20+- in this case
    return QVariant(0);
    }
    ...
    }

    the overrode resizeEvent(..)
    {
    int count = model()->columnCount();
    int total = 0;

    for (int i = 0; i < count - 1; i++)
    {
    const QModelIndex index = model()->index(0, i);
    QVariant hint = model()->data(index, Qt::SizeHintRole);
    int size = (width() / 100) * hint.toInt();
    horizontalHeader()->resizeSection(i, size);
    total = total + size;
    }
    //used magical constant 5 for the last section
    horizontalHeader()->resizeSection(count - 1, width() - total - 5);
    }

    I used Qt::SizeHintRole cause it's never used by QTableView itself.

Similar Threads

  1. Replies: 1
    Last Post: 18th November 2009, 23:21
  2. hierarchical model in a flat view
    By gniking in forum Qt Programming
    Replies: 4
    Last Post: 10th November 2009, 20:17
  3. Model / View - Design Question
    By antarctic in forum Qt Programming
    Replies: 8
    Last Post: 8th June 2009, 07:39
  4. A few queries about Model View Programming
    By montylee in forum Qt Programming
    Replies: 46
    Last Post: 2nd March 2009, 08:36
  5. Replies: 9
    Last Post: 7th November 2006, 15:10

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.