I'm trying draw some rhombuses with random colors in a QWidget. And i want to save the current QWidget as image. I use such code to do this:
QPixmap pixmap(this->size());
this->render(&pixmap);
pixmap.save("test.png");
The problem is that the render() seems to call paintEvent again, and the paintEvent will draw the rhombuses with new random colors, so that i always get a different Image saved compared to the image displayed. Can someone tell me how to save the current QWidget? Thanks in advance.
Code for drawing rhombuses:
void Dialog::paintEvent(QPaintEvent *e) {
QPainter painter(this);
QRect background(0,0,this->geometry().width(),this->geometry().height());
painter.setBrush( QBrush( Qt::white ) );
painter.setPen( Qt::NoPen );
//QBrush bbrush(Qt::black,Qt::SolidPattern);
painter.drawRect(background);
int width = this->geometry().width();
int height = this->geometry().height();
//draw rectangles
int rec_size=64;
int rows=0;
int cols=0;
rows=floor((double)height/(double)rec_size);
cols=floor((double)width/(double)rec_size);
QPointF points[4]; // QRect rec(0,0,rec_size,rec_size);
for (int i=0;i<floor(rows);i++){
for (int j=0;j<floor(cols);j++){
painter.setBrush( QBrush( colors[rand() % color_size] ) );
//QPainter painter(this);
points[0] = QPointF(rec_size*(j),rec_size*(i+0.5));
points[1] = QPointF(rec_size*(j+0.5),rec_size*(i));
points[2] = QPointF(rec_size*(j+1),rec_size*(i+0.5));
points[3] = QPointF(rec_size*(j+0.5),rec_size*(i+1));
painter.drawPolygon(points, 4);
}
}
painter.end();
}
You can have a class member variable of boolean type to check in the paintEvent whether a random color should be used. Also a variable to save the index of the last color used is necessary:
bool isRandom;
int lastColor;
The paintEvent should be like :
void Dialog::paintEvent(QPaintEvent *e) {
...
if(isRandom)
{
lastColor = rand() % color_size;
painter.setBrush( QBrush( colors[lastColor] ) );
}
else
painter.setBrush( QBrush( colors[lastColor] ) );
...
}
The variable has true when drawing the widget regularly. When you want to save it's image, assign the variable to false, save the image and assign it to true again :
isRandom = false;
QPixmap pixmap(this->size());
this->render(&pixmap);
pixmap.save("test.png");
isRandom = true;
Related
I would like to know how many pixels need this items in QComboBox:
red - checkbox
green - distance between end of checkbox and beggining of the text
blue - distance between end of border and beggining of the checkbox
In doc of QStyle I find two methods:
subElementRect()
pixelMetric()
I think I have to use them, but I don't know, which args I need to use.
It depends on the style.
QCommonStyle for example draws the ComboBox's label like this:
if (const QStyleOptionComboBox *cb = qstyleoption_cast<const QStyleOptionComboBox *>(opt)) {
QRect editRect = proxy()->subControlRect(CC_ComboBox, cb, SC_ComboBoxEditField, widget);
p->save();
p->setClipRect(editRect);
if (!cb->currentIcon.isNull()) {
QIcon::Mode mode = cb->state & State_Enabled ? QIcon::Normal
: QIcon::Disabled;
QPixmap pixmap = cb->currentIcon.pixmap(qt_getWindow(widget), cb->iconSize, mode);
QRect iconRect(editRect);
iconRect.setWidth(cb->iconSize.width() + 4);
iconRect = alignedRect(cb->direction,
Qt::AlignLeft | Qt::AlignVCenter,
iconRect.size(), editRect);
if (cb->editable)
p->fillRect(iconRect, opt->palette.brush(QPalette::Base));
proxy()->drawItemPixmap(p, iconRect, Qt::AlignCenter, pixmap);
if (cb->direction == Qt::RightToLeft)
editRect.translate(-4 - cb->iconSize.width(), 0);
else
editRect.translate(cb->iconSize.width() + 4, 0);
}
if (!cb->currentText.isEmpty() && !cb->editable) {
proxy()->drawItemText(p, editRect.adjusted(1, 0, -1, 0),
visualAlignment(cb->direction, Qt::AlignLeft | Qt::AlignVCenter),
cb->palette, cb->state & State_Enabled, cb->currentText);
}
p->restore();
}
This means, that the actual size of the icon's rectangle can be determined by subclassing QComboBox (to access its protected initStyleOption method) and creating a new public getIconRect method in the following way:
QRect ComboBox::getIconRect()
{
QStyleOptionComboBox opt;
initStyleOption(&opt);
QRect rect(style()->subControlRect(QStyle::CC_ComboBox, &opt, QStyle::SC_ComboBoxEditField));
rect.setWidth(opt.iconSize.width() + 4);
return rect;
}
Then in MainWindow for example create a ComboBox and call its getIconSize method like this:
auto *cmbBox = new ComboBox(this);
qDebug() << cmbBox->getIconRect();
For me, on Windows 10, this gives:
QRect(3,3 20x14)
There could be other ways, but if you insist on using a method similar to QStyle::subControlRect, those are the correct arguments;
I want to paint a circle with user provided color and keep a line edit adjustment on it on horizontal alignment.
Used painter function call on slot, but its not working
#include <QPainter>
#include "cascadeColorHighlightWidget.h"
CascadeColorHighlightWidget::CascadeColorHighlightWidget(QWidget *parent) : QWidget(parent)
{
setWindowFlags(Qt::FramelessWindowHint | Qt::Widget);
setAttribute( Qt::WA_DeleteOnClose, true );
setFixedSize(187,164);
setContentsMargins(0,0,0,0);
}
void CascadeColorHighlightWidget::paintEvent(QPaintEvent *event)
{
Q_UNUSED(event);
QPainter painter(this);
painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
QRectF contRect = contentsRect().adjusted(1, 1, -1, -1);
painter.setPen(QPen(QColor(176, 176, 176),1));
painter.setBrush(QColor(255,255,255));
painter.drawRect(contRect);
painter.setPen(QPen(QColor(51,51,51),1));
QFont font( "Calibri" );
font.setPixelSize(14);
painter.setFont( font );
painter.drawText(QPointF(contRect.x() + 18, contRect.y() + 28), "Color Highlight");
}
void CascadeColorHighlightWidget::focusOutEvent(QFocusEvent *event)
{
Q_UNUSED(event);
close();
}
void CascadeColorHighlightWidget::setColors(QColor color)
{
QPainter painter(this);
painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
QRectF contRect = contentsRect().adjusted(1, 1, -1, -1);
int rectYPos = contRect.y() + 55;
painter.setPen(Qt::NoPen);
QRectF ellipseRect = QRectF(contRect.x() + 18, rectYPos, 16, 16);
painter.setPen(Qt::NoPen);
painter.setBrush(color);
painter.drawEllipse(ellipseRect);
/*After this ellipse I need to draw a line edit where user can edit anytime*/
}
But by calling setcolot its not drawing the ellipse on the widget. Only the items in paintEvent worked.
Is it possible to do with painter or I need to keep widgetItems and insert in this wideget. please give some suggestions
All painting work should happen in paintEvent. You have to keep state, and paint items accordingly. Have methods that take QPainter as an argument and call them from within paintEvent method, passing to them the QPainter object you created there.
Example:
In your widget header have:
private:
void setColors(QColor c) { color = c; }
void drawEllipse(QPainter & painter);
QColor color;
bool draw_ellipse;
As you can see, the setColors method only sets a color and you keep that color in a private instance variable color.
A new method hosts the painting job (previously in setColors):
void CascadeColorHighlightWidget::drawEllipse(QPainter &painter)
{
painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
QRectF contRect = contentsRect().adjusted(1, 1, -1, -1);
int rectYPos = contRect.y() + 55;
painter.setPen(Qt::NoPen);
QRectF ellipseRect = QRectF(contRect.x() + 18, rectYPos, 16, 16);
painter.setPen(Qt::NoPen);
painter.setBrush(color);
painter.drawEllipse(ellipseRect);
/*After this ellipse I need to draw a line edit where user can edit anytime*/
}
The variable color in this line
painter.setBrush(color);
is the one you set using the setColors method.
The paintEvent method should be like:
void CascadeColorHighlightWidget::paintEvent(QPaintEvent *event)
{
Q_UNUSED(event);
QPainter painter(this);
painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
QRectF contRect = contentsRect().adjusted(1, 1, -1, -1);
painter.setPen(QPen(QColor(176, 176, 176),1));
painter.setBrush(QColor(255,255,255));
painter.drawRect(contRect);
painter.setPen(QPen(QColor(51,51,51),1));
QFont font( "Calibri" );
font.setPixelSize(14);
painter.setFont( font );
painter.drawText(QPointF(contRect.x() + 18, contRect.y() + 28), "Color Highlight");
if(draw_ellipse)
{
drawEllipse(painter);
}
}
At the end of it, you test draw_ellipse (don't forget to initialize it to false in the constructor) and call the drawEllipse method if it's true.
Let's draw the ellipse, for example using QWidget's mousePressEvent:
void CascadeColorHighlightWidget::mousePressEvent(QMouseEvent *event)
{
setColors(QColor(Qt::red));
draw_ellipse = true;
update();
}
Here, you set a color, first, then set draw_ellipse to true, then (and it matters a lot) you call the update slot of QWidget:
[...] it schedules a paint event for processing when Qt returns to the
main event loop.
So the paintEvent method will be called, and your paintings updated accordingly to your class's state (color and draw_ellipse variables).
How is it possible to change the color and/or text(format) of a QProgressBar if the mouse is over the filled part of the bar?
I have stacked multiple QProgressBars on top of each other, each showing more content than the previous.
I want to highlight the biggest bar still under the mouse on mouseover and show some bar-specific text. However, the bars are the same size and therefore I want to recognize the filled area of the bar.
E.g., if the mouse is over the third bar, I want to highlight the part from the left to the third bar and show text specific to the third bar.
This is the code I use for the stacked ProgressBars:
class MultiProgressBar : public QWidget
{
Q_OBJECT
public:
MultiProgressBar( QWidget* parent = Q_NULLPTR ) : QWidget( parent ), layout( new QStackedLayout( this ) )
{
layout->setMargin( 0 );
layout->setStackingMode( QStackedLayout::StackAll );
}
void insertBar( QColor const& color )
{
auto bar = new QProgressBar();
bar->setTextVisible( false );
bar->setRange( 0, 10000 );
QPalette palette = this->palette();
palette.setColor( QPalette::Highlight, QColor( color.red(), color.green(), color.blue(), 100 ) );
palette.setColor( QPalette::Base, QColor( color.red(), color.green(), color.blue(), 0 ) );
bar->setPalette( palette );
layout->addWidget( bar );
progress_bars.push_back( bar );
}
public slots:
void setValues( const std::vector< int >& values, const std::vector< std::string >& names )
{
if ( values.size() < progress_bars.size() )
{
for ( auto* widget : progress_bars )
{
layout->removeWidget( widget );
}
progress_bars.clear();
}
while ( progress_bars.size() < values.size() )
{
insertBar( QColor( 0x46, 0xA1, 0xD9, 255 ) );
}
for ( auto i = 0; i < progress_bars.size(); ++i )
{
progress_bars[ i ]->setValue( values[ i ] );
// progress_bars[ i ]->setFormat( QString::fromStdString( names[ i ] ) );
}
}
private:
std::vector< QProgressBar* > progress_bars;
QStackedLayout* layout;
};
1) To change color of filled area you can set new color to the palette of progressbar like this:
QPalette newPalette = bar.palette();
newPalette.setColor(QPalette::Highlight, "red"); // setting color to red
bar.setPalette(newPalette);
2.1) Assign text to progressbar you can with void setFormat(const QString &format); function like in your commented string
progress_bars[ i ]->setFormat( QString::fromStdString( names[ i ] ) );
2.2) Get this text you can by calling QProgressBar::text() function;
3) If you want to highlight specific progressbars you can reimplement
QWidget::mouseMoveEvent(QMouseEvent *event);
in which you can calculate margins of filled area:
void mouseMoveEvent(QMouseEvent *e){
highlightProgressBars(e->pos()); // passing position of mouse cursor
}
NOTE: Don't forget to enable mouse tracking for progressbars and your MultiProgressBar widget:
bar->setMouseTracking(true);
4) Function below gets position of mouse in parameter. It highlights area from left to pointed progressbar inclusively and shows in console text assigned to highlighted progressbars through format property
void highlightProgressBars(QPoint point){
int widthOfBar = ((QProgressBar*)progress_bars.at(0))->width();
int valueForPoint = 10000 / widthOfBar; // value of progressbar for width==1
for (auto pb = this->progress_bars.begin(); pb != this->progress_bars.end(); ++pb) { // iterating vector to paint progressbars
int leftMargin = 0; // "left margin" of current progressbar is in fact right margin of filled area of previous progressbar
if(pb != this->progress_bars.begin()){ // except first progressbar in vector which doesn't have previous progressbar
--pb; // get previous progressbar
leftMargin = ((QProgressBar*)*pb)->value() / valueForPoint; // get width of filled area
++pb; // return to current progressbar
}
if(leftMargin < point.x()) { // if position of cursor is to the right of "left margin" of current progressbar we highlight it
QPalette newPal = ((QProgressBar*)*pb)->palette(); //getting palette
newPal.setColor(QPalette::Highlight,"red"); // and setting color of filled area
((QProgressBar*)*pb)->setPalette(newPal); // finally setting palette to widget
qDebug() << ((QProgressBar*)*pb)->text(); // show text of highlighted progressbar in console
}
else{ // if not we highlight it another way
QPalette newPal = ((QProgressBar*)*pb)->palette();
newPal.setColor(QPalette::Highlight,"lightblue");
((QProgressBar*)*pb)->setPalette(newPal);
}
}
}
If you want to highlight only one progressbar you should change in code above this:
if(leftMargin < point.x()) {
to:
int rightMargin = ((QProgressBar*)*pb)->value() / valueForPoint;
if(leftMargin < point.x() && point.x() < rightMargin) {
I want to show a pixmap on Qt and make able to zoom, I'm using a QWidget inside a QScrollArea, but the ScrollBars of the area are not working, when I zoom on the image, the image gets bigger, but there is no scrollbars so i can move, let me show you some code :
Here is how I'm declaring the Widgets :
_scroll = new QScrollArea(_frame);
_image_widget = new QImageWidget(_scroll);
_scroll->setBackgroundRole(QPalette::Dark);
_scroll->setWidget(_image_widget);
_scroll->setWidgetResizable(true);
_frame is the area where i should show the hole thing, _image_widget is an object of QImageWidget which is inheriting from QWidget
if I dont use this : _scroll->setWidgetResizable(true); the image is just too small
And here is how I deal with the Zoom :
void QImageWidget::paintEvent(QPaintEvent *ev) {
QPainter p(this);
if(pixmap() != NULL){
int w = pixmap()->width();
int h = pixmap()->height();
QPixmap map = pixmap()->scaled(w*zoom,h*zoom,Qt::KeepAspectRatio);
p.drawPixmap(0, 0, map );
}
}
void QImageWidget::wheelEvent ( QWheelEvent * e )
{
int x = e->pos( ).x();
int y = e->pos( ).y();
if (e->delta() > 0)
zoom *= 2;
else
if(zoom > 1)
zoom /= 2;
}
SO the problem is as I said, the image keeps getting bigger when i'm zooming until it takes the hole area of the QScrollArea and then when I keep zooming, the zoom is working but there is no ScrollBars so i can see the other part of the Image.
Tell me if i'm not understandable !
thanks !
I found the answer to my question, all i had to do is add this resize(sizeHint()*zoom); before drawing the pixmap :
void QImageWidget::paintEvent(QPaintEvent *ev) {
QPainter p(this);
if(pixmap() != NULL){
int w = pixmap()->width();
int h = pixmap()->height();
QPixmap map = pixmap()->scaled(w*zoom,h*zoom,Qt::KeepAspectRatio);
resize(sizeHint()*zoom);
p.drawPixmap(0, 0, map );
}
}
the pixels were updated but not the size of the widget !
I tried to find some helps around internet, i had some codes to test but none works like i want.
I would draw with Qt something pixel by pixel.
I tried with a QImage within a QLabel with the protected event mousePressEvent it worked, but the pixels are too small to see them.
I tried to scale my Image, it's much better, but the position X, Y are not the same position of the pixels now or they're not synchronized with the pixels locations.
Here a screenshot of what i want do, if someone has an idea to recreate this...
If i can exactly done this via Qt i'll save a lots of time, just a basic drawing pixel by pixel, i created this example with Paint, Black and White format zoomed to 800%.
Thanks.
This is a solution hard-coded for a specific size - just implement relative coordinate mapping between pixmap and draw widget and you are set.
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = 0) : QWidget(parent), pressed(0) {
color = Qt::black;
pixmap = new QPixmap("h:/small.png");
resize(240, 240);
}
~Widget() { if (pixmap) delete pixmap; }
protected:
void paintEvent(QPaintEvent *) {
QPainter painter(this);
painter.drawPixmap(0, 0, pixmap->scaled(240, 240));
painter.drawPixmap(0, 0, *pixmap);
}
void mousePressEvent(QMouseEvent *e) {
if (e->button() == Qt::RightButton)
color = color == Qt::black ? Qt::white : Qt::black;
else {
pressed = 1;
draw(e);
}
}
void mouseReleaseEvent(QMouseEvent *) { pressed = 0; }
void mouseMoveEvent(QMouseEvent *e) { draw(e); }
private:
void draw(QMouseEvent *e) {
if (pressed) {
QPainter painter(pixmap);
painter.setPen(color);
int x = e->pos().x() / 12;
int y = e->pos().y() / 12;
painter.drawPoint(x, y);
repaint();
}
}
QColor color;
QPixmap *pixmap;
bool pressed;
};