QT 总结【不断补充】

来源:互联网 发布:淘宝官方企业店 旗舰店 编辑:程序博客网 时间:2024/05/16 17:14
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();}




0 0