注意,给View换字体是直接换。在Delegate里换的只是某一列的字体

class delegate : public QStyledItemDelegate
{
public:
delegate(QObject* parent = 0) : QStyledItemDelegate(parent)
{}

void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
{
QStyledItemDelegate::paint(painter, option, index);
}
};

int main(int argc, char* argv[])
{
QApplication a(argc, argv);

QStringList list;
list << "a" << "b" << "c";

QListWidget w;
w.setFont(QFont("Courier", 30));
delegate d;
w.setItemDelegate(&d);
w.addItems(list);
w.show();

QListView lv;
lv.setFont(QFont("Courier", 30));
lv.setItemDelegate(&d);
QStringListModel m;
m.setStringList(list);
lv.setModel(&m);
lv.show();


return a.exec();
}

参考:http://www.qtcentre.org/archive/index.php/t-29871.html

-----------------------------------------------------------------------

Delegate里也能换字体,但换的是progress自己的字体(如果用到的话),并不对整个view起作用,例如:

void ProgressBarDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
{  
    if (index.column() == 3) {    
    else {
        // good 这里换字体是换进度条(第三列)的字体,但是能起作用
        painter->save();
        painter->setFont(QFont("Times", 10, QFont::Bold));
        return QStyledItemDelegate::paint(painter, option, index);
        painter->restore();
    }
}

------------------------------------------------------------------------

另外,设置QTableView的行高是:

QTtableView的verticalHeader()->setDefaultSectionSize(15),既可以放在构造函数里,也可以构造完毕后调用。

相关文章:

  • 2022-01-07
  • 2021-08-14
  • 2021-05-08
  • 2021-07-17
  • 2021-07-26
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-01-28
  • 2022-01-04
  • 2022-12-23
  • 2021-11-09
  • 2021-09-24
  • 2022-12-23
相关资源
相似解决方案