How to set Bold font to specific row in table widget view

I want my font to be bold at a specific column column position of my table widget. I loved it, but I needed to rest.

QFont font("Helvetica", 12, QFont::Bold);
overviewTable->item(2,2)->setFont(font);

      

Please, help

+2


a source to share


2 answers


I think it's okay. This is what the docs said:

void QTableWidgetItem::setFont ( const QFont & font )

      

Sets the font used to display the element text for this font.

Perhaps your overview is Table const?



ADDED:

This option works fine for my Qt 4.6:

tableWidget = new QTableWidget(12, 3, this);

for (int i = 0; i < 12; i++) {
    for (int j = 0; j < 3; j++) {
        QTableWidgetItem *newItem = new QTableWidgetItem(tr("%1").arg(
            (i+1)*(j+1)));
        tableWidget->setItem(i, j, newItem);
    }
}

QFont font;
font.setBold(true);

tableWidget->item(2, 2)->setFont(font);

      

+4


a source


You might be getting a break because you didn't call setItem()

to set an element for cell (2, 2) before using overviewTable->item(2,2)

. As the Qt doc says,

QTableWidgetItem * QTableWidget :: item (int row, int column) const

Returns the item for the given row and column, if set; otherwise it returns 0.

That is, yours overviewTable->item(2,2)

probably returns 0, so it makes the call Segmentation fault

in call setFont()

.



So, your font customization tools are perfectly correct. You just need to call setItem () first, as mosg's answer suggests.

ADDED:

if yours overviewTable

is this QTableWidget

created in Qt Designer, then in the constructor double click on the cell (just to enter edit mode, you don't need to type anything) will have the effect of calling setItem()

for that cell. Later in your code, you can use the function directly item()

without calling first setItem()

.

0


a source







All Articles