Change font color of Qtableview text when onclick()



  • Hi,
    i want to implement QTableview ondoubleclick() method .
    the method only takes :
    @
    const QModelIndex &index
    @

    and i have the model for the qtableview .

    how can i edit the text color in a certain cell in the qtableview inside the ondoubleclick() ?


  • Lifetime Qt Champion

    Hi,

    Do you mean have a color editor or just change the color of the cell ?



  • sorry for late reply ,
    i don't exactly know what a color editor is , but what i want is to be able to change the text color in the cells of the tableview , the default know is black .
    lets say i have a column of IDs , and one of the ids is 123456 , this is the text i want to change its color 123456 .



  • Hi,

    If you want to change the color of the letters shown in table, you should check out setting the Qt::ForegroundRole on the cell you want to be colored.



  • thanks that worked , i just used :

    @
    QStandardItem *col_2 = new QStandardItem(QString(tr((*it)->description.c_str())));
    col_2->setForeground(Qt::red);
    @

    now its a bit tricky , is it possible to color some of the text ? not the whole cell's ForegroundRole ?


  • Lifetime Qt Champion

    For that second port you should rather create a custom QStyledItemDelegate where you'll be able to customize the painting to your needs


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.