Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何在QGraphicscene中更改QLineEdit的背景色_C++_Qt_Qgraphicsscene_Qlineedit - Fatal编程技术网

C++ 如何在QGraphicscene中更改QLineEdit的背景色

C++ 如何在QGraphicscene中更改QLineEdit的背景色,c++,qt,qgraphicsscene,qlineedit,C++,Qt,Qgraphicsscene,Qlineedit,我有一个qgraphicscene并添加了一个QlineEdit,但改变颜色根本不起作用 QGridLayout *layout = new QGridLayout(this); QGraphicsView *view = new QGraphicsView(this); QGraphicsScene *scene = new QGraphicsScene(this); QWidget *widget = new QWidget(); QGridLayout *widgetLayout = n

我有一个
qgraphicscene
并添加了一个
QlineEdit
,但改变颜色根本不起作用

QGridLayout *layout = new QGridLayout(this);
QGraphicsView *view = new QGraphicsView(this);
QGraphicsScene *scene = new QGraphicsScene(this);

QWidget *widget = new QWidget();
QGridLayout *widgetLayout = new QGridLayout(this);
QLineEdit *le1 = new QLineEdit(widget);
QLineEdit *le2 = new QLineEdit(widget);
widgetLayout->addWidget(le1,1,0);
widgetLayout->addWidget(le2,2,0);
widget->setLayout(widgetLayout);



QPalette paletteRed = le1->palette();
paletteRed.setColor(QPalette::Background,Qt::red);

QPalette paletteGreen = le1->palette();
paletteGreen.setColor(QPalette::Background,Qt::green);


le1->setAutoFillBackground(true);
le1->setPalette(paletteRed); // not working
widget->setPalette(paletteGreen); // working


view->setScene(scene);
scene->addWidget(widget);

ui->centralWidget->setLayout(layout);
layout->addWidget(view);
如果小部件在场景中,我是否必须触发类似于
update()
(也无法获得另一种颜色)的事件

编辑:

创建了新的示例代码

我知道这在正常的
QWidget
中有效。实际上,如果我将
QLineEdit
放在一个正常的
QFrame
etc中,但是
qgraphicscene
中,代码工作正常。在这种特殊情况下,它不起作用。文本和突出显示颜色等也很好。但背景/基础/等等并非如此

通过Qpalete设置背景色不适合我的小部件,为什么

通常是
autoFillBackground
属性未设置为true以允许自己设置背景

QPalette palette = pWidget->palette(); // fixed it (need to initialize)
palette.setColor(pWidget->backgroundRole(), bkgndColor); // for background (fixed)
palette.setColor(pWidget->foregroundRole(), fgrndColor); // for foreground
pWidget->setAutoFillBackground(true); // to allow to fill the background
pWidget->setPalette(palette);

通过样式表设置背景也可能有效,因为它强制
autoFillBackground==true
模式。

我想建议对AlexanderVX的答案稍加修改。在第一行,我会写:

QPalette palette = pWidget->palette();
只是为了确保您调整了基本对象调色板所需的内容


问候。

我有一个类似的问题,背景颜色只是给轮廓上色:


问题是有一个边界图像集。设置
边框图像后:无背景颜色显示了

颜色更改代码在哪里?更改
QPallette
?尝试使用qt样式的表单。我不能更改任何内容,我可以更改
文本
颜色,但我不能更改“背景”或“基础”。您解决了问题吗?在文本编辑中放置lineedit时,我遇到了同样的问题。编辑
qpalete::Background
应替换为
pWidget->backgroundRole()
。这里的信息:@JonHarper是的,最好得到实际的背景角色。然后在我自己的代码中进行修复。所以规则@AlexanderVX这不管用。它在正常情况下工作,如
QFrame
,如果在普通小部件中使用,也适用于
QLineInput
。但如果我输入一个
qgraphicscene
它不是working@J.W.但是,代码中有一个可疑的东西:QPalette-pal(QApplication::palete());您可以尝试使用QLineEdit自己的调色板吗?@J.W.尝试捕获从
addWidget
返回的指向
QGraphicsProxyWidget
的指针,并在其上设置调色板。