2011-05-08 74 views
1

我从来没有在Qt中做过任何项目代表,我认为文档没有很好地解释更复杂的代表。如何在Qt中创建Symbian样式列表视图

我需要创建2款的Symbian(^ 3)风格名单

类型1:

Delegate style 1

这是常见的导航列表,图标和较低的标签是可选的。

类型2:

Delegate style 2

这是为设置的列表,其中,所述按钮可以是一个肘节(开/关) - 按钮或执行上下文菜单等

我将如何继续创建这些项目代表?

最好的问候, 鼠

回答

2

我不得不做出类似的东西一次。这是我做到的。

我的委托类声明。正如你可以看到它有一个成员:QLabel *标签。您可以根据需要添加另一个标签或按钮。

class MyItemDelegate : public QStyledItemDelegate 
{ 
public: 
    explicit MyItemDelegate(QObject *parent = 0); 
    ~MyItemDelegate(); 
protected: 
    void paint(QPainter *painter, 
       const QStyleOptionViewItem &option, const QModelIndex &index) const; 
    QSize sizeHint(const QStyleOptionViewItem &option, 
        const QModelIndex &index) const; 
private: 
    QLabel *label; 
}; 

我的paint()和sizeHint()方法。

QSize MyItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const 
{ 
    if(!index.isValid()) 
     return QSize(); 
    QVariant data = index.data(Qt::DisplayRole); 

    label->setText(data.toString()); 
    label->resize(label->sizeHint()); 
    QSize size(option.rect.width(), label->height()); 
    return size; 
} 

void MyItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const 
{ 
    if(!index.isValid()) 
     return; 
    QVariant data = index.data(Qt::DisplayRole); 

    // Not necessary to do it here, as it's been already done in sizeHint(), but anyway. 
    label->setText(data.toString()); 

    painter->save(); 

    QRect rect = option.rect; 

    // This will draw a label for you. You can draw a pushbutton the same way. 
    label->render(painter, QPoint(rect.topLeft().x(), rect.center().y() - label->height()/2), 
        QRegion(label->rect()), QWidget::RenderFlags()); 

    painter->restore(); 
} 

希望这是你一直在寻找。祝你好运!

+0

这正是我一直在寻找的!谢谢! – Gerstmann 2011-05-11 06:38:48

0

你有2种选择,

1)QML - 这在我看来是最好的方式,更容易达到你正在尝试做的。 Link to Example

这将向您展示如何使用委托进行listview。

2)QItemDelegate - 类别QItemDelegate然后分配该委托到ListView, Link to QItemDelegate

+0

QML不是一种选择,因为我想保持原生的外观和感觉。我知道我需要继承QItemDelegate,问题是没有文档容易让我理解涉及多个类型的UI元素的更复杂的代表。 – Gerstmann 2011-05-09 04:56:17