I am using QPainter on QPixmap as the painting framework. And QGraphicsScene is holding the QPixmap. The painting works well. But the problem is when I change the the background-color of the QGraphicsView it does not get reflected on the screen. I tried by QPixmap::fill(Qt::tranparent). But it didnt work.
How can I achieve this kind of transparent QPixmap?
Thanx in advance?
int main(int argc, char **argv){
QApplication a(argc, argv);
QMainWindow *win = new QMainWindow();
win->resize(600,600);
win->move(80,80);
win->show();
QGraphicsScene *scene = new QGraphicsScene(win);
QGraphicsView view(scene,win);
view.resize(590,590);
view.setBackgroundBrush(QColor(rand()%255,rand()%255, rand()%255, 255));
view.show();
QPixmap *pix = new QPixmap(600,600);
pix->fill(&view,QPoint(0,0));
QGraphicsPixmapItem *item = scene->addPixmap(*pix);
QPainter *painter = new QPainter(pix);
int count=10;
while(count){
painter->setPen((*new QColor(rand()%255,rand()%255, rand()%255, 255)));
painter->setBrush(QColor(rand()%255,rand()%255, rand()%255, 255));
painter->drawRect(rand()%300, rand()%300, rand()%300, rand()%300);
item->setPixmap(*pix);
a.processEvents(0x00);
count--;
}
return a.exec();
}
Related
I have a problem when use QGraphicsView and QGraphicsBlurEffect in my project. When I put them together, my program does not work normally. I wrote a tiny program to reproduce this problem.
The Widget class is inherited from QGraphicsView.
Widget::Widget(QWidget *parent)
: QGraphicsView(parent)
{
scene = new QGraphicsScene(this);
this->setScene(scene);
label = new QLabel;
QPixmap pixmap = QPixmap("../partly_cloudy.png").scaledToWidth(200);
label->setPixmap(pixmap);
label->setGeometry(100,100, pixmap.width(), pixmap.height());
label->setStyleSheet("border:3px;border-color: rgb(255, 100, 0); border-style:solid;");
/* ********image won't show when adding following comment code********
QGraphicsBlurEffect *blur = new QGraphicsBlurEffect;
blur->setBlurRadius(10);
label->setGraphicsEffect(blur);
******* */
QGraphicsProxyWidget *proxyWidget = new QGraphicsProxyWidget;
proxyWidget->setWidget(label);
proxyWidget->setPos(10,10);
scene->addItem(proxyWidget);
}
and main.cpp is as follows.
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
This is a screenshot when QGraphicsBlurEffect is not used.
However, this is a screenshot when QLabel uses setGraphicsEffect() to bind blur effect.
To solve this problem, I tried to use a QWidget to wrap QLabel. When I did this, QLabel was rendered. However, it seems to be bounded by a rectangle area.
Widget::Widget(QWidget *parent)
: QGraphicsView(parent)
{
scene = new QGraphicsScene(this);
this->setScene(scene);
/* ********/
container = new QWidget;
container->setStyleSheet("border:3px;border-color: blue; border-style:solid;");
/******** */
label = new QLabel(container);
QPixmap pixmap = QPixmap("../partly_cloudy.png").scaledToWidth(200);
label->setPixmap(pixmap);
label->setGeometry(100,100, pixmap.width(), pixmap.height());
label->setStyleSheet("border:3px;border-color: red; border-style:solid;");
QGraphicsBlurEffect *blur = new QGraphicsBlurEffect;
blur->setBlurRadius(10);
label->setGraphicsEffect(blur);
QGraphicsProxyWidget *proxyWidget = new QGraphicsProxyWidget;
proxyWidget->setWidget(container);
proxyWidget->setPos(80,80);
qDebug() << proxyWidget->boundingRect();
scene->addItem(proxyWidget);
this->setSceneRect(0,0,640,480);
}
The screenshot of the result is.
I tried to set proxyWidget position to {0,0}, and it works normally. it seems that the position of effect rectangle will not influenced by proxyWidget position.
By the way, the version of Qt is 5.14.2.
I've searched for a long time on net. But no use. Please help or try to give some ideas how to achieve this.
I was trying to change the background color of a QPlainTextEdit widget to black and apply a glow effect to it. But when I apply a DropShadowEffect to a QTextEdit or a QPlainTextEdit widget, its background color reverts back to the original and refuses to change. I'm using Qt version 5.12.2. Here's the code:
#include <QtWidgets>
int main(int argc, char** argv)
{
QApplication app(argc, argv);
QMainWindow window;
window.setWindowTitle("Title");
window.setStyleSheet("QMainWindow{background-color: #191b21}");
window.setFixedSize(800, 600);
window.show();
QPlainTextEdit* w = new QPlainTextEdit(&window);
w->setGeometry(250, 50, 300, 50);
w->setStyleSheet("QPlainTextEdit{background-color: black; color: white;}");
QGraphicsDropShadowEffect* effect = new QGraphicsDropShadowEffect();
effect->setColor("#59f1ff");
effect->setBlurRadius(30);
effect->setOffset(.0);
w->setGraphicsEffect(effect);
w->show();
return app.exec();
}
I've tried using QPalette but it won't work too. What am I doing wrong?
How I can change title shown on the picture to "№". Thanks.
That widget is an object of the QTableCornerButton class that inherits from QAbstractButton but it is a class that is part of the private Qt API that does not use text, so you can not use setText() of QAbstractButton, so the other option is establish a QLabel with a layout above:
#include <QtWidgets>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QTableView w;
QStandardItemModel model(10, 10);
w.setModel(&model);
QAbstractButton *button = w.findChild<QAbstractButton *>();
if(button){
QVBoxLayout *lay = new QVBoxLayout(button);
lay->setContentsMargins(0, 0, 0, 0);
QLabel *label = new QLabel("№");
label->setContentsMargins(0, 0, 0, 0);
lay->addWidget(label);
}
w.show();
return a.exec();
}
I have more than one widget in QGraphicsScene. The first created widget is on top of scene, other widgets is under first widget. I want such functionality - if i press one of widget it will be on top of the scene. How i can make it?
Also i don't know, how to make border to the widget in QGraphicsScene like the normal widget. This is the code:
QGraphicsScene *scene ;
QGraphicsProxyWidget *pw()
{
QWidget *w = new QWidget;
w->resize(580, 280);
w->setStyleSheet("background-color: rgb(110, 149, 255)");
QPushButton *p = new QPushButton;
p->setParent(w);
p->move(30, 50);
p->setText("hello");
QPushButton *p2 = new QPushButton;
p2->setParent(w);
p2->move(110, 50);
p2->setText("world");
QGraphicsWidget *parentWidget = new QGraphicsWidget();
parentWidget->setMinimumSize(QSizeF(100, 30));
parentWidget->setFlags(QGraphicsItem::ItemIsMovable);
parentWidget->setAutoFillBackground(true);
parentWidget->resize(580, 280);
scene->addItem(parentWidget);
QGraphicsProxyWidget *proxyWidget = new QGraphicsProxyWidget;
proxyWidget->setWidget(w);
proxyWidget->setParentItem(parentWidget);
return proxyWidget;
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
scene = new QGraphicsScene;
scene->setSceneRect(0, 0, 800, 480);
scene->addItem(pw());
scene->addItem(pw());
scene->addItem(pw());
scene->setBackgroundBrush(Qt::darkGreen);
QGraphicsView view(scene);
view.show();
return a.exec();
}
I have a QWidget which contains a QVBoxLayout and that layout contains a QLabel and QToolButtons. My problem is, that the QLabel takes all the space. The only solution I found is to set maximumHeight to the QLabel, but if I do that, the Qt::AlignTop doesn't work anymore.
main.cpp:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget window_main;
QWidget *widget_steps = new QWidget(&window_main);
widget_steps->setFixedWidth(75);
widget_steps->move(QPoint(0, 0));
widget_steps->setStyleSheet("background-color: red;");
QVBoxLayout *layout_steps = new QVBoxLayout(widget_steps);
layout_steps->setContentsMargins(0, 0, 0, 0);
layout_steps->setSpacing(0);
QLabel *label_steps_start = new QLabel("steps:");
label_steps_start->setAlignment(Qt::AlignHCenter | Qt::AlignTop);
label_steps_start->setStyleSheet("background-color: blue;");
layout_steps->addWidget(label_steps_start);
QToolButton *tbutton_step1 = new QToolButton();
layout_steps->addWidget(tbutton_step1);
QToolButton *tbutton_step2 = new QToolButton();
layout_steps->addWidget(tbutton_step2);
QToolButton *tbutton_step3 = new QToolButton();
layout_steps->addWidget(tbutton_step3);
window_main.showMaximized();
return a.exec();
}
Here a picture that shows how much space the QLable takes(the blue space):
So please help to minimize the space the QLable takes :)
Your problem is that the tool buttons have a fixed size, and therefore when resizing, the label is the only type that can grow: Therefore:
After adding the label, add stretch to the layout:
layout_steps->addWidget(label_steps_start);
layout_steps->addStretch();
Modified code - adds stretch at the bottom. Label size remains fixed, and buttons remain under it. I've removed the whole main window around the outside for the sake of testing.
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget *widget_steps = new QWidget;
widget_steps->setFixedWidth(75);
widget_steps->move(QPoint(0, 0));
widget_steps->setStyleSheet("background-color: red;");
QVBoxLayout *layout_steps = new QVBoxLayout(widget_steps);
layout_steps->setContentsMargins(0, 0, 0, 0);
layout_steps->setSpacing(0);
QLabel *label_steps_start = new QLabel("steps:");
label_steps_start->setAlignment(Qt::AlignHCenter | Qt::AlignTop);
label_steps_start->setStyleSheet("background-color: blue;");
layout_steps->addWidget(label_steps_start);
//--- Removed.... layout_steps->addStretch();
QToolButton *tbutton_step1 = new QToolButton();
layout_steps->addWidget(tbutton_step1);
QToolButton *tbutton_step2 = new QToolButton();
layout_steps->addWidget(tbutton_step2);
QToolButton *tbutton_step3 = new QToolButton();
layout_steps->addWidget(tbutton_step3);
layout_steps->addStretch(); //<----- Added!
widget_steps->show();
return a.exec();
}
One way you could do is to set the stretch factor for that particular widget inside the QVBoxLayout. You can find the documentation for that in here.
Basically, when you add a widget you can set that, for instance:
#include <QtWidgets/QApplication>
#include <QtWidgets/QLabel>
#include <QtWidgets/QToolButton>
#include <QtWidgets/QVBoxLayout>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget window_main;
QWidget *widget_steps = new QWidget(&window_main);
widget_steps->setFixedWidth(75);
widget_steps->move(QPoint(0, 0));
widget_steps->setStyleSheet("background-color: red;");
QVBoxLayout *layout_steps = new QVBoxLayout(widget_steps);
layout_steps->setContentsMargins(0, 0, 0, 0);
layout_steps->setSpacing(0);
QLabel *label_steps_start = new QLabel("steps:");
label_steps_start->setAlignment(Qt::AlignHCenter | Qt::AlignTop);
label_steps_start->setStyleSheet("background-color: blue;");
layout_steps->addWidget(label_steps_start, 3, Qt::AlignTop);
layout_steps->addStretch();
QToolButton *tbutton_step1 = new QToolButton();
layout_steps->addWidget(tbutton_step1, 1);
QToolButton *tbutton_step2 = new QToolButton();
layout_steps->addWidget(tbutton_step2, 1);
QToolButton *tbutton_step3 = new QToolButton();
layout_steps->addWidget(tbutton_step3, 1);
window_main.showMaximized();
return a.exec();
}