Russian Qt Forum

Qt => Пользовательский интерфейс (GUI) => Тема начата: ZIkos от Июль 09, 2007, 18:44



Название: Цвет шрифта.
Отправлено: ZIkos от Июль 09, 2007, 18:44
Как поменять в QListViewItem цвет шрифта текста в определенной колонке? Версия 3.3.1.
В 4.2.0 setForeground(color,column).


Название: Цвет шрифта.
Отправлено: Racheengel от Июль 09, 2007, 21:57
переопределить paint() (по моему так метод называется).


Название: Цвет шрифта.
Отправлено: pastor от Июль 09, 2007, 22:25
Попробуй так:

Код:

void Item::paintCell ( QPainter * p, const QColorGroup & cg, int column,
                               int width, int align )
{
    if(column == 0)
    {
        QPen oldPen(p->pen());
        QPen newPen(p->pen());
        newPen.setColor(red);
        p->setPen(newPen);
        QListViewItem::paintCell(p, cg, column, width, align);
        p->setPen(oldPen);
        return;
    }
    QListViewItem::paintCell(p, cg, column, width, align);
}


ЗЫ: В чёмто мог ошибиться, писал по памяти

добавлено спустя 3 минуты:

 Или можно так:
Код:

void Item::paintCell ( QPainter * p, const QColorGroup & cg, int column,
                               int width, int align )
{
    QPen newPen(p->pen());
    if(column == 0)  
        newPen.setColor(red);
    else
        newPen.setColor(black);    
    p->setPen(newPen);  
    QListViewItem::paintCell(p, cg, column, width, align);
}


Название: Цвет шрифта.
Отправлено: Racheengel от Июль 10, 2007, 00:05
Вообще делали подобное в проекте http://www.ii-system.com/soft/insense/insense.htm. Там есть сырцы открытые в архиве. Файл devicemanager_gui.cpp.

У нас выглядело примерно так:

Код:

void DeviceItem::paintCell ( QPainter * p, const QColorGroup & cg, int column, int width, int align )
{
QColorGroup cg2(cg);
if (!(m_device->isActive()))
cg2.setColor(QColorGroup::Text, Qt::gray);

// mark if there are critical issues
if (!(m_device->issues().isEmpty())) {
const DevIssueEntry& last_issue = m_device->issues().last();
if (last_issue.severity == DS_CRITICAL) {
cg2.setColor(QColorGroup::Text, Qt::red);
cg2.setColor(QColorGroup::Highlight, Qt::red);
}
}

if (column) {
QFont font(p->font());
font.setItalic(true);
p->setFont(font);
}

QListViewItem::paintCell(p, cg2, column, width, align);
}


Название: Цвет шрифта.
Отправлено: pastor от Июль 10, 2007, 09:44
Решение предложенное Racheengel будет правильнее моего


Код:
void Item::paintCell( QPainter *p, const QColorGroup &cg,
int column, int width, int alignment )
{
    QColorGroup _cg( cg );
    QColor c = _cg.text();

    if ( column == 0 )
_cg.setColor( QColorGroup::Text, Qt::red );

    QListViewItem::paintCell( p, _cg, column, width, alignment );

    _cg.setColor( QColorGroup::Text, c );
}


Название: Цвет шрифта.
Отправлено: ZIkos от Июль 10, 2007, 22:19
Всем спасибо. Разобрался.