2011-05-08 19 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

相關問題