QT 总结【不断补充】

QTableView
在点击某一行时,header会高亮显示,字体加粗,很不好看,可以用:
ui->tableview->horizontalHeader()->setHighlightSections(false); 去掉高亮。

在调用setItem时 会new很多item,造成内存泄露。而clearContents()是可以delete掉所有item的。具体可以参见源码。
void QTableModel::clearContents()
{
    beginResetModel();
    for (int i = 0; i < tableItems.count(); ++i) {
        if (tableItems.at(i)) {
            tableItems.at(i)->view = 0;
            delete tableItems.at(i);
            tableItems[i] = 0;
        }
    }
    endResetModel();
}


QSpinBox
去掉右边的上下箭头:
spinbox->setButtonSymbols(QAbstractSpinBox::NoButtons);此方法还可以将箭头换成加减符号。

QAbstractItemDelegate
在写QTableView的时候会继承QItemDelegate。重新实现createEditor()函数,此函数会new一个editor,有人可能会担心造成内存泄露,所以想要试图delete它。其实qt提供了一个函数自动delete掉它,就是destoryEditor();看源代码:
/*!
    Called when the \a editor is no longer needed for editing the data item
    with the given \a index and should be destroyed. The default behavior is a
    call to deleteLater on the editor. It is possible e.g. to avoid this delete by
    reimplementing this function.

    \since 5.0
    \sa createEditor()
*/
void QAbstractItemDelegate::destroyEditor(QWidget *editor, const QModelIndex &index) const
{
    Q_UNUSED(index);
    editor->deleteLater();
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值