这个问题真的无人能解么。。。

加盾男爵 2018-07-20 09:23:37
第二次发这个问题了。。。。前一次N久没人回复就接了贴。。但到现在一直没找到解决方案

问题是有一张表格,显示一组设备的信心。其中一列是设备的开关,用户可以点击这一列来控制设备运行/停止

应为这一列按钮要一直显示在上面,所以我使用了委托,在paint()函数里绘制这个按钮,这里没有使用createEditor()函数,应为这个函数生产的按钮只有双击后才会出现,而我需要的是按钮一直在哪里
现在的问题是,我希望这个按钮有个背景颜色,比如运行时绿色,停止时黑色,单我找不到设背景颜色的方法

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);
}
}

这段代码就是绘制按钮的代码,然后我想着改变painter的背景刷来改变背景色
    if(option.widget != NULL)
{
painter->setBackgroundBrush(QBrush(QColor(Qt::green)));
QApplication::style()->drawControl(QStyle::CE_PushButton,&btn,painter);
}

没有效果。。。然后我有想着在构造函数里生产一个按钮,先设置这个按钮的背景色,然后把按钮给这个QStyleOptionButton
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);
}

结果还是没有任何效果,,
···················分割点·····························
期初这里没人回答,我结了贴去qt论坛上发了个帖子问问,
https://forum.qt.io/topic/92249/qstyleoptionbutton-cloud-be-set-background-color
起初都是建议对painter做各自设置。。都试过,不行

然后我又发了一次
https://forum.qt.io/topic/92808/how-to-change-the-color-of-qstyledoptionbutton-s-background
但这个人给我的代码,我做了一个小例子,结果连按钮都显示不出来了。。。



我在qtcn上也发了个相同的问题,一个回复的人也没有。。。。
...全文
392 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
_Will_ 2018-07-20
  • 打赏
  • 举报
回复
用tableWidget的话不用delegate直接setcellwiget,更简单些
_Will_ 2018-07-20
  • 打赏
  • 举报
回复
引用 5 楼 qazaq408 的回复:
[quote=引用 2 楼 jxgyzhang 的回复:]

你这个按钮只有在鼠标点击后才会出现在表格上,而我希望按钮一直出现在表格上[/quote]
this->ui->tableView->openPersistentEditor(this->_Model->index(i, 0));
加盾男爵 2018-07-20
  • 打赏
  • 举报
回复
引用 2 楼 jxgyzhang 的回复:

你这个按钮只有在鼠标点击后才会出现在表格上,而我希望按钮一直出现在表格上
_Will_ 2018-07-20
  • 打赏
  • 举报
回复
this->ui->tableView->setItemDelegateForColumn(0, this->_PushButtonDelegate);
for(int i=0;i<this->_Model->rowCount();i++)
{
this->ui->tableView->openPersistentEditor(this->_Model->index(i, 0));
}
_Will_ 2018-07-20
  • 打赏
  • 举报
回复

#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();
_Will_ 2018-07-20
  • 打赏
  • 举报
回复

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);
}

huo5896324 2018-07-20
  • 打赏
  • 举报
回复
g_toolButton = new QToolButton(this); g_toolButton->setStyleSheet("QToolButton {border: 1px solid #108ee9;border-radius: 5px;color: rgba(255,255,255,100%);background-color:#108ee9;}"); btn.text = "ASB"; btn.palette = QApplication::palette(); btn.palette.setBrush(QPalette::Button,QBrush(QColor(255,43,43),Qt::SolidPattern)); //btn.palette.setBrush(QPalette::Button,QColor(255,43,43)); btn.palette.setColor(QPalette::ButtonText,QColor(255,143,43)); btn.rect = QRect(10,10,100,100); btn.state |= QStyle::State_Enabled; btn.state |= QStyle::State_Raised; g_toolButton->style()->drawControl(QStyle::CE_PushButton,&btn,&painter,g_toolButton); 楼主重点是你那个QApplication::style(),替换成g_toolButton就可以了,给g_toolButton设置样式你画出来的按钮就带颜色了,drawControl()中的QWidget*对象也要用g_toolButton。

16,215

社区成员

发帖
与我相关
我的任务
社区描述
Qt 是一个跨平台应用程序框架。通过使用 Qt,您可以一次性开发应用程序和用户界面,然后将其部署到多个桌面和嵌入式操作系统,而无需重复编写源代码。
社区管理员
  • Qt
  • 亭台六七座
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧