×

Discussion Board

Results 1 to 7 of 7

Hybrid View

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    10

    Updating size of TableView with delegate on rotation

    I have a custom view delegate to right-align some text and I'm having a problem when the device (Nokia N900) is rotated. The problem seems to be that the QStyleOptionViewItem parameter in the delegate's paint function does not contain the correct rect to represent the size. When the device is rotated, all right-aligned text ends up outside the window and must be scrolled over to bee seen. If the window is opened after the device is first rotated, everything does however work as expected. It seems like the size of the QStyleOptionViewItem's rect member is not updated when the table and the window resizes. As if it is only updated and stored the first time the window is shown.

    Is there a way that I may force the delegate/view to update the size of this rectangle corresponding to the new width/height after rotation? Upon rotation I've tried to call the view's resizeColumnsToContente, updateGeometry and reset()-functions as well as the model's reset() function without any effect.

    The code in question is this:
    Code:
    void DepartureListDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
                                  const QModelIndex &index) const {
        QStyledItemDelegate::paint(painter, option, index);
    
        Departure *e = qVariantValue<Departure *>(index.data());
    
        QFont font = option.font;
        QRect rect = option.rect;
        rect.adjust(10, 10, -20, -7);
    
        painter->save();
    
        painter->drawText(rect, Qt::AlignTop | Qt::AlignLeft, e->lineNumber + " " + e->lineDestination);
        painter->drawText(rect, Qt::AlignTop | Qt::AlignRight, e->arrivalDifference + " min");
    
        painter->setPen(option.palette.mid().color());
        font.setPointSizeF(font.pointSizeF() * 0.70);
        painter->setFont(font);
    
        painter->drawText(rect, Qt::AlignBottom | Qt::AlignLeft, e->arrivalTime);
    
        painter->restore();
    }
    If there are any other ways to right align some of the text using a delegate I would love to hear about those as well
    Last edited by sdragly; 2010-06-02 at 13:46.

  2. #2
    Super Contributor
    Join Date
    Oct 2009
    Posts
    4,326

    Re: Updating size of TableView with delegate on rotation

    You probably did something bad in sizeHint, I was not able to reproduce the problem
    main.cpp
    Code:
    #include <QtGui>
    #include "rotatinglistview.h"
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
    
        QStandardItemModel model;
    
        for (int i = 0; i < 10; ++i) {
            QStandardItem *item = new QStandardItem(QString("item %1").arg(i));
            item->setTextAlignment(Qt::AlignRight);
            model.appendRow(item);
        }
    
        RotatingListView list;
        list.setViewMode(QListView::ListMode);
        list.setMovement(QListView::Free);
        list.setSelectionMode(QAbstractItemView::SingleSelection);
        list.setModel(&model);
    
        list.show();
    
        QTimer::singleShot(5000, &list, SLOT(rotate()));
    
        return a.exec();
    }
    rotatinglistview.h
    Code:
    #include "rotatinglistview.h"
    
    RotatingListView::RotatingListView(QWidget *parent) : QListView(parent), orientation(Qt::Horizontal)
    {
    }
    
    void RotatingListView::rotate()
    {
        if (orientation == Qt::Horizontal) {
            setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
            orientation = Qt::Vertical;
        } else {
            setAttribute(Qt::WA_Maemo5PortraitOrientation, false);
            orientation = Qt::Horizontal;
        }
    }
    rotatinglistview.cpp:
    Code:
    #ifndef ROTATINGLISTVIEW_H
    #define ROTATINGLISTVIEW_H
    
    #include <QListView>
    
    class RotatingListView : public QListView
    {
        Q_OBJECT
    
    public:
        RotatingListView(QWidget *parent=0);
    
    public slots:
        void rotate();
    
    private:
        Qt::Orientation orientation;
    };
    
    #endif //ROTATINGLISTVIEW_H

  3. #3
    Registered User
    Join Date
    Jun 2010
    Posts
    10

    Thumbs up Re: Updating size of TableView with delegate on rotation

    Thanks a lot for testing!

    Could it be because I haven't overloaded sizeHint() in the delegate at all?

    I haven't seen sizeHint being overloaded in any of the examples I have been looking at, so I don't really know what I should put in there. Do you know how to get the size (or sizeHint) of the tableview itself within the delegate's functions? If not, I'll see if I can figure it out when I get back home

    Again, thanks. And you are experiencing the same as me; when using QStandardItem and no custom delegate, the width of each table cell is correct. I've been doing the same thing until I tried to write a custom delegate. I wanted to have both left and right-aligned text in the same table cell (like in the e-mail application on N900), and I don't think that is possible without a custom delegate. I might be wrong, though.

  4. #4
    Registered User
    Join Date
    Jun 2010
    Posts
    10

    Talking Re: Updating size of TableView with delegate on rotation

    Thanks a ton! The missing sizeHint() was the issue. I was able to use the rectangle from QStyleOptionViewItem in the sizeHint to return the size needed. I also had to set the resize mode of the tableView's horizontal header to QHeaderView::Stretch, and remove the parameter which resized the last section.

    I added the following to make it work:
    Code:
    QSize DepartureListDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) {
        return QSize(option.rect.width(), option.rect.height());
    }
    This is just perfect! I've been looking for this for a quite a while now

  5. #5
    Super Contributor
    Join Date
    Oct 2009
    Posts
    4,326

    Re: Updating size of TableView with delegate on rotation

    Quote Originally Posted by sdragly View Post
    Code:
        return QSize(option.rect.width(), option.rect.height());
    Code:
    return option.rect.size();

  6. #6
    Registered User
    Join Date
    Jun 2010
    Posts
    10

    Re: Updating size of TableView with delegate on rotation

    Quote Originally Posted by divanov View Post
    Code:
    return option.rect.size();
    That is for sure a prettier way to do it

    Actually, I decided to do some more testing to make sure I ruled out the correct error, and I just figured that I didn't. It wasn't the missing sizeHint that caused the problem, but the fact that I had set "horizontalHeaderStretchLastSection" to true for the tableView in Qt Creator.

    Everything works fine now without the sizeHint function (I've commented it out) and with the resizeMode set to QHeaderView::Stretch instead of using the "horizontalHeaderStretchLastSection" property.

    You got me looking in the right direction, though

  7. #7
    Super Contributor
    Join Date
    Oct 2009
    Posts
    4,326

    Re: Updating size of TableView with delegate on rotation

    Email client is Gtk2+ application and it's using GtkTreeView with several columns. Would you modify my test application to demonstrate the problem?

Similar Threads

  1. Weird Problem with find box Kern-Exec-3
    By sohilr in forum Symbian
    Replies: 7
    Last Post: 2007-12-05, 04:10
  2. KERN-EXEC 3 with CEikEdwin
    By acarrion in forum Symbian User Interface
    Replies: 3
    Last Post: 2007-10-17, 14:43
  3. Audio Input Stream on N80
    By tkaihock in forum Symbian Media (Closed)
    Replies: 9
    Last Post: 2006-06-30, 09:15
  4. Replies: 0
    Last Post: 2006-03-01, 11:25
  5. User break point error in Grid
    By ag24master in forum Symbian User Interface
    Replies: 0
    Last Post: 2003-08-07, 08:20

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •