English 中文(简体)
如何在QHeader绘制图 使用代表的观点部分?
原标题:HowTo draw pixmap in QHeaderView section using delegate?

<>Prostrong>Problem:我需要绘制一份小的地图,以QHeaderView

“entergraph

我的理解是,这样做有两种方式:

  1. 重新实施<条码>QHeaderView spaint Section (方法。

  2. 代表来自<代码>QStyledItemDelegate 类别和补充内容paint()。

如果我尝试了第(1)条备选案文,该文本如下:filter pixmap,根本不显示:

void DecorativeHeaderView::paintSection( QPainter* painter, const QRect& rect, int logicalIndex ) const
{
     if( !rect.isValid() )
     {
          return;
     }

     // get the state of the section
     QStyleOptionHeader option;
     initStyleOption( &option );

     // setup the style options structure
     option.rect = rect;
     option.section = logicalIndex;
     option.iconAlignment = Qt::AlignVCenter | Qt::AlignHCenter;

     QVariant variant = model()->headerData( logicalIndex, orientation(), Qt::DecorationRole );
     option.icon = qvariant_cast< QIcon >( variant );
     if( option.icon.isNull() )
     {
          option.icon = qvariant_cast< QPixmap >( variant );
     }

     // draw the section
     if( !option.icon.isNull() )
     {
          style()->drawControl( QStyle::CE_Header, &option, painter, this );
     }
     else
     {
          QHeaderView::paintSection( painter, rect, logicalIndex );

// HERE is where I m trying to draw my filter picture!!!

          if( logicalIndex == filteredLogicalIndex_ )
          {
               QPixmap pixmap( ":/spreadsheet/images/spreadsheet/filter_icon_table.png" );
               int x = rect.right() - pixmap.width();
               int y = rect.top() + ( rect.height() - pixmap.height() ) / 2;

               painter->drawPixmap( QPoint( x, y ), pixmap );
          }
     }
}

The (2) variant is this:

class HeaderDelegate : public QStyledItemDelegate
{
     Q_OBJECT
     Q_DISABLE_COPY( HeaderDelegate )

public:
     HeaderDelegate( QObject* parent = 0 ) : QStyledItemDelegate( parent ) {}
     virtual ~HeaderDelegate() {}

     virtual void paint( QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index ) const;

}; // HeaderDelegate

void HeaderDelegate::paint( QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index ) const
{

// THIS method never starts!!!

     if( index.column() == 2 )
     {
          QPixmap pixmap( ":/spreadsheet/images/spreadsheet/filter_icon_table.png" );
          int x = option.rect.right() - pixmap.width();
          int y = option.rect.top() + ( option.rect.height() - pixmap.height() ) / 2;

          painter->save();
          painter->drawPixmap( QPoint( x, y ), pixmap );
          painter->restore();
     }

     QStyledItemDelegate::paint( painter, option, index );
}

DecorativeHeaderView::DecorativeHeaderView( Qt::Orientation orientation, QWidget* parent /* = 0 */ )
     : QHeaderView( orientation, parent )
     , filteredLogicalIndex_( -1 )
{
     setItemDelegate( new HeaderDelegate( this ) );
}

代表创建,但职能不开始采用<条码>。

任何帮助?

Thanks!

问题回答

它像现在这样看不出。

摘自 重要文件:

注:每名负责人提供每一科的数据,而不依赖代表。 因此,称头盔职能将无效果。

How to have dynamic pixmap in a QHeaderView item

But you may override QHeaderView::paintSection method. subclassing-QHeaderView

class HeaderView : public QHeaderView {
    Q_OBJECT

public:
    HeaderView(Qt::Orientation orientation, QWidget * parent = 0)
        : QHeaderView(orientation, parent), p("333222.jpeg") {
    }

    int sectionSizeHint ( int /*logicalIndex*/ ) const { return p.width(); }

protected:
    void paintSection(QPainter * painter, const QRect & rect, int /*logicalIndex*/) const {
        painter->drawPixmap(rect, p);
    }

private:
    QPixmap p;
};




相关问题
Qt: Do events get processed in order?

If I had a class A, where one of its functions does: void A::func() { emit first_signal(); emit second_signal(); } Assuming that a class B has 2 slots, one connected to first_signal, and the ...

How to determine how much free space on a drive in Qt?

I m using Qt and want a platform-independent way of getting the available free disk space. I know in Linux I can use statfs and in Windows I can use GetDiskFreeSpaceEx(). I know boost has a way, ...

Drag & drop with .ui files

I m having big troubles with drag & drop. I ve created a new Qt Designer Form Class in which I have one QListWidget and one QWidget. Now I want to enable dragging & dropping between these two ...

Link errors on Snow Leopard

I creating a small desktop application using Qt and Poco on Mac OS X Snow Leopard. Qt works fine, but once I started linking with Poco I get the following warning: ld: warning: in /Developer/SDKs/...

Showing two windows in Qt4

My friend and I have each created parts of a GUI using Qt 4. They both work independently and I am trying to integrate his form with the my main window. As of now this is the code I am using to try ...

Qt equivalent of .NET data binding?

Is there an equivalent of .NET s data binding in Qt? I want to populate some combo boxes and other widgets with QStrings that refer to specific entities in my database. However, it would be cleaner ...

热门标签