qtableview的单元格怎么放一个button
答案:2 悬赏:80 手机版
解决时间 2021-02-01 01:02
- 提问者网友:献世佛
- 2021-01-31 08:14
qtableview的单元格怎么放一个button
最佳答案
- 五星知识达人网友:酒安江南
- 2021-01-31 08:50
在头文件中声明
qvariant data(const qmodelindex & item, int role) const;
在源文件中写代码
qvariant setuptaskdialog::data(const qmodelindex & item, int role) const
{
qvariant value=qsqlrelationaltablemodel::data(item,role);
if(role == qt::textalignmentrole)
{
value = int(qt::alignhcenter | qt::alignvcenter);
}
return value;
}
qvariant data(const qmodelindex & item, int role) const;
在源文件中写代码
qvariant setuptaskdialog::data(const qmodelindex & item, int role) const
{
qvariant value=qsqlrelationaltablemodel::data(item,role);
if(role == qt::textalignmentrole)
{
value = int(qt::alignhcenter | qt::alignvcenter);
}
return value;
}
全部回答
- 1楼网友:玩家
- 2021-01-31 09:36
#ifndef BUTTONDELEGATE_H
#define BUTTONDELEGATE_H
#include <QItemDelegate>
class ButtonDelegate : public QItemDelegate
{
Q_OBJECT
public:
explicit ButtonDelegate(QObject *parent = 0);
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;
bool editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index);
signals:
public slots:
private:
QMap<QModelIndex, QStyleOptionButton*> m_btns;
};
#endif // BUTTONDELEGATE_H
#include "buttondelegate.h"
#include <QApplication>
#include <QMouseEvent>
#include <QDialog>
#include <QPainter>
#include <QStyleOption>
#include <QDesktopWidget>
ButtonDelegate::ButtonDelegate(QObject *parent) :
QItemDelegate(parent)
{
}
void ButtonDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QStyleOptionButton* button = m_btns.value(index);
if (!button) {
button = new QStyleOptionButton();
button->rect = option.rect.adjusted(4, 4, -4, -4);
button->text = "X";
button->state |= QStyle::State_Enabled;
(const_cast<ButtonDelegate *>(this))->m_btns.insert(index, button);
}
painter->save();
if (option.state & QStyle::State_Selected) {
painter->fillRect(option.rect, option.palette.highlight());
}
painter->restore();
QApplication::style()->drawControl(QStyle::CE_PushButton, button, painter);
}
bool ButtonDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index)
{
if (event->type() == QEvent::MouseButtonPress) {
QMouseEvent* e =(QMouseEvent*)event;
if (option.rect.adjusted(4, 4, -4, -4).contains(e->x(), e->y()) && m_btns.contains(index)) {
m_btns.value(index)->state |= QStyle::State_Sunken;
}
}
if (event->type() == QEvent::MouseButtonRelease) {
QMouseEvent* e =(QMouseEvent*)event;
if (option.rect.adjusted(4, 4, -4, -4).contains(e->x(), e->y()) && m_btns.contains(index)) {
m_btns.value(index)->state &= (~QStyle::State_Sunken);
QDialog *d = new QDialog();
d->setGeometry(0, 0, 200, 200);
d->move(QApplication::desktop()->screenGeometry().center() - d->rect().center());
d->show();
}
}
}
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯