在Qt中,我想定製QT Symbian中的listview。所以爲了定製目的,我爲listview設置了委託。我也處理繪畫事件..問題是文字顏色。錯誤代碼
從列表視圖中選擇項目時,我沒有得到默認的文本顏色。
,即下面的示例代碼,我有兩個字符串即「項」(主字符串)和「測試」(子字符串)
主弦與默認選擇的顏色繪製,但子字符串不能用與主要字符串相同的顏色繪製。
我認爲我們需要處理選擇和獲取調色板顏色..我沒有得到確切的方式。
請給我解決方案。
這裏是代碼
class SettingDeligate : public QStyledItemDelegate
{
// Q_OBJECT
public:
explicit SettingDeligate(QObject* parent = 0);
virtual ~SettingDeligate();
enum ItemDataRole {SubheaderTextRole = Qt::UserRole + 100};
QSize sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const;
void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const;
};
SettingDeligate::SettingDeligate(QObject* parent)
: QStyledItemDelegate(parent)
{
}
SettingDeligate::~SettingDeligate()
{
}
QSize SettingDeligate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const
{
QIcon icon = qvariant_cast<QIcon>(index.data(Qt::DecorationRole));
QString line1 = index.data(Qt::DisplayRole).toString();
QString line2 = index.data(SubheaderTextRole).toString();
int textW = qMax(option.fontMetrics.width(line1), option.fontMetrics.width(line2));
//QSize iconSize = icon.actualSize(option.decorationSize);
QSize iconSize(27,19);
return QSize(qMax(textW, iconSize.width()) + 4,
iconSize.height() + 2 + option.fontMetrics.lineSpacing() * 2 + 4);
}
void SettingDeligate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
{
QStyledItemDelegate::paint(painter,option,index);
QString headerText(qvariant_cast<QString>(index.model()->data(index, SubheaderTextRole)));
QPoint point1 (option.rect.topLeft());
painter->drawText(point1.x()+25,point1.y()+25,headerText);
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QListView *MylistView = new QListView();
MylistView->setViewMode(QListView::ListMode);
MylistView->setMovement(QListView::Free);
MylistView->setItemDelegate(new (SettingDeligate));
MylistView->setSelectionMode(QAbstractItemView::SingleSelection);
QStandardItemModel *model = new QStandardItemModel();
MylistView->setModel(model);
QStandardItem *item = new QStandardItem("item1");
item->setData("Test1", SettingDeligate::SubheaderTextRole);
model->appendRow(item);
QStandardItem *item1 = new QStandardItem("item2");
item1->setData("Test2", SettingDeligate::SubheaderTextRole);
model->appendRow(item1);
QStandardItem *item2 = new QStandardItem("item3");
item2->setData("Test3", SettingDeligate::SubheaderTextRole);
model->appendRow(item2);
QDesktopWidget* desktopWidget = QApplication::desktop();
//get client rect.
QRect clientRect = desktopWidget->availableGeometry();
MylistView->setGeometry(clientRect);
MylistView->show();
return a.exec();
}
請幫我
感謝ü
你怎麼禁用Q_OBJECT宏? – bgs 2010-03-02 17:39:09
@Karol,Q_OBJECT宏只有當我們使用QT中的使用信號和插槽時才需要..對於元對象.. 因爲我沒有在這裏使用任何信號插槽..所以它不是必需的 – Naruto 2010-03-03 04:55:28