Qt4.7 QPainter not active

Qt4.7 QPainter not active,第1张

由于QPlainTextEdit继承自QAbstractScrollArea,与普通的直接继承自QWidget的控件存在差异,QAbstractScrollArea中包含一个视窗,该视窗可以通过viewport函数来获取,刚好所有的绘制 *** 作都是在该视窗内部的,所以你应该在视窗内部进行绘制,而不是外围的widget中进行绘制(有点绕,还是看下面的代码)

假设你在头文件有如下定义:

//editorh

class Editor : public QPlainTextEdit

{

Q_OBJECT

public:

Editor(QWidget parent = 0):QPlainTextEdit(parent){}

virtual ~Editor(){}

//

protected:

void paintEvent(QPaintEvent e);

//

};

源文件内容如下

void Editor::paintEvent(QPaintEvent e)

{

//通过viewport函数获取视窗并进行绘制

//我只是简单的填充了一种颜色。

QPainter painter(viewport());

painterfillRect(rect(),Qt::cyan);

//调用QPlainTextEdit默认的绘制函数,不调用的话

//后果会很严重的。

QPlainTextEdit::paintEvent(e);

}

虽然不明白你为嘛要重载paintEvent不过我想说,如果只是为了更改背景为目的,那还是用style sheet,方便!只要不要求速度,那么就用style sheet

首先,创建一个combobox代理

class ComboDelegate : public QItemDelegate

{

Q_OBJECT

public:

ComboDelegate(QObject parent = 0);

QWidget createEditor(QWidget parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;

void setEditorData(QWidget editor, const QModelIndex &index) const;

void setModelData(QWidget editor, QAbstractItemModel model, const QModelIndex &index) const;

void updateEditorGeometry(QWidget editor, const QStyleOptionViewItem &option, const QModelIndex &index) const;

};

实现里面声明的方法,具体参考帮助文档。

然后,在表中,为某一列设置代理

比如,第二列

pComboDelegate = new ComboDelegate();

pTable->setItemDelegateForColumn(1, pComboDelegate);

这样,每行的第二列,需要编辑时,双击,就会出现combobox了

QVariant headerData ( int section,Qt::Orientation orientation, int role = Qt::DisplayRole ) const 获取水平头或垂直头标题

bool setHeaderData ( int section,Qt::Orientation orientation, const QVariant & value, int role = Qt::EditRole ) 设置水平头或垂直头标题

int rowCount ( const QModelIndex & parent= QModelIndex() ) const // 返回行数

int columnCount ( const QModelIndex &index = QModelIndex() ) const // 返回列数

virtual bool removeColumns ( int column, int count, const QModelIndex & parent = QModelIndex() ) //model->removeColumns (0)删除第一列

bool QSqlTableModel::submitAll (),//提交所有被修改的数据,然后修改的数据被保存在数据库中

void QSqlTableModel::revertAll () //撤销所有的修改,如果数据库已经被提交了修改,就不能通过撤销修改改回来了

virtual void revertRow ( int row ) //恢复指定行的改变

void QSqlTableModel::setFilter ( const QString & filter ) //筛选,按照字符串filter对数据库进行筛选,相当于SQL中的WHERE语句

bool QSqlTableModel::select () //在筛选和排序的条件下,将数据库中符合要求的在mode表格中显示出来

void QSqlTableModel::setSort ( int column, Qt::SortOrder order ) //排序 *** 作。按照列和Qt::SortOrder排序。Qt::SortOrder有升序和降序

bool insertRow ( int row, const QModelIndex & parent = QModelIndex() ) //插入行

bool insertColumn ( int column, constQModelIndex & parent = QModelIndex() ) // 插入列

model->setEditStrategy(QSqlTableModel::OnManualSubmit); //设置保存策略为手动提交

这个需要重写一些model的方法

当然你需要编辑也要实现自己的delegate

class MyTableDelgate : public QStyledItemDelegate

{

Q_OBJECT

public:

explicit MyTableDelgate(QObject parent = 0);

protected:

//basic function for a read-only delegate, you can draw anything with the painter

void paint(QPainter painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;

QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const;

//edit 5 function

QWidget createEditor(QWidget parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;

void setEditorData(QWidget editor, const QModelIndex &index) const;

void setModelData(QWidget editor, QAbstractItemModel model, const QModelIndex &index) const;

void updateEditorGeometry ( QWidget editor, const QStyleOptionViewItem & option, const QModelIndex & index ) const;

private:

QLineEdit m_LineEdit;

};

model

class MyTableDelgate : public QStyledItemDelegate

{

Q_OBJECT

public:

explicit MyTableDelgate(QObject parent = 0);

protected:

//basic function for a read-only delegate, you can draw anything with the painter

void paint(QPainter painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;

QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const;

//edit 5 function

QWidget createEditor(QWidget parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;

void setEditorData(QWidget editor, const QModelIndex &index) const;

void setModelData(QWidget editor, QAbstractItemModel model, const QModelIndex &index) const;

void updateEditorGeometry ( QWidget editor, const QStyleOptionViewItem & option, const QModelIndex & index ) const;

};

//freezetablewidgetcpp

#include <QtGui>

#include "freezetablewidgeth"

FreezeTableWidget::FreezeTableWidget(QAbstractItemModel model)

{

setModel(model);

frozenTableView = new QTableView(this);

init();

connect(horizontalHeader(),SIGNAL(sectionResized ( int ,int,int )), this,

SLOT(updateSectionWidth(int, int, int)));

connect(frozenTableView->verticalScrollBar(), SIGNAL(valueChanged(int)),

verticalScrollBar(), SLOT(setValue(int)));

connect(verticalScrollBar(), SIGNAL(valueChanged(int)),

frozenTableView->verticalScrollBar(), SLOT(setValue(int)));

}

FreezeTableWidget::~FreezeTableWidget()

{

delete frozenTableView;

}

以上就是关于Qt4.7 QPainter not active全部的内容,包括:Qt4.7 QPainter not active、如何在QTableView中的一列或一行上添加控件、如何在QTableView中显示SQL查询结果等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/10082445.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-05
下一篇2023-05-05

发表评论

登录后才能评论

评论列表(0条)

    保存