16,215
社区成员
发帖
与我相关
我的任务
分享
void SwitchDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
{
QStyleOptionButton btn;
btn.rect = option.rect;
btn.state |= QStyle::State_Enabled;
btn.text = index.model()->data(index).toBool() ? tr("Running") : tr("Stop");
if(option.widget != NULL)
{
QApplication::style()->drawControl(QStyle::CE_PushButton,&btn,painter);
}
}
if(option.widget != NULL)
{
painter->setBackgroundBrush(QBrush(QColor(Qt::green)));
QApplication::style()->drawControl(QStyle::CE_PushButton,&btn,painter);
}
gb_PushButton = new QPushButton;
gb_PushButton->setStyleSheet(tr("background-color:green"));
if(option.widget != NULL)
{
QApplication::style()->drawControl(QStyle::CE_PushButton,&btn,painter,gb_PushButton);
}
#include <QStyledItemDelegate>
#include <QPushButton>
#include <QEvent>
#include <QDebug>
class PushButtonDelegate :public QStyledItemDelegate
{
Q_OBJECT
signals:
void SignalClicked(const int &rowIndex);
public:
PushButtonDelegate(const QString &text);
~PushButtonDelegate();
protected:
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 ;
bool editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index);
bool event(QEvent * e);
private:
QString _Text;
private slots:
void SlotBtnClicked();
PushButtonDelegate::PushButtonDelegate(const QString &text)
{
this->_Text = text;
}
PushButtonDelegate::~PushButtonDelegate()
{
}
QWidget *PushButtonDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QPushButton *editor = new QPushButton(this->_Text, parent);
editor->setStyleSheet(tr("QPushButton{background-color:black;color: white;"
"border-radius: 10px; border: 2px groove gray;border-style: outset;}"
"QPushButton:hover{background-color:white; color: black;}"
"QPushButton:pressed{background-color:rgb(85, 170, 255);"
"border-style: inset; }"));
editor->setProperty("row", index.row());
connect(editor, &QPushButton::clicked, this, &PushButtonDelegate::SlotBtnClicked);
return editor;
}
void PushButtonDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
}
void PushButtonDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
}
void PushButtonDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
editor->setGeometry(option.rect);
}
bool PushButtonDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index)
{
qDebug()<<"editorEvent"<<event->type();
return QStyledItemDelegate::editorEvent(event,model,option,index);
}
bool PushButtonDelegate::event(QEvent *e)
{
qDebug()<<"event"<<e->type();
return QStyledItemDelegate::event(e);
}
void PushButtonDelegate::SlotBtnClicked()
{
QPushButton *btn = (QPushButton *) sender();
int row = btn->property("row").toInt();
emit this->SignalClicked(row);
}