Why QPainter might reject to work inside paintEvent? - c++

I have:
class QTextEditEnter : public QTextEdit
{
Q_OBJECT
public:
QTextEditEnter( QWidget *_parent ) : QTextEdit(_parent)
{
this -> setFrameStyle( QFrame::Sunken ); // Sunken!
}
protected:
virtual void keyPressEvent(QKeyEvent * event);
virtual void paintEvent(QPaintEvent *_event)
{
QTextEdit::paintEvent( _event );
QPainter pnt(this);
pnt.setPen( QColor( 0, 0, 0, 0xff ));
pnt.drawRect( 0, 0, width(), height());
}
signals:
void signalPressEnter();
};
that gives:
QPainter::begin: Widget painting can only begin as a result of a paintEvent
QPainter::setPen: Painter not active
QPainter::drawRects: Painter not active
QPainter::begin: Widget painting can only begin as a result of a paintEvent
QPainter::setPen: Painter not active
QPainter::drawRects: Painter not active
Why that could be?
Update
// QPainter( this )
// QTextEdit::paintEvent at the begining of custom PaintEvent
// RESULT: "QPainter::begin: Widget painting can only begin as a result of a paintEvent" ...
virtual void paintEvent(QPaintEvent *_event)
{
QTextEdit::paintEvent( _event );
QPainter pnt( this );
pnt.setPen( QColor( 0, 0, 0, 0xff ));
pnt.drawRect( 0, 0, width()-1, height()-1);
}
// QPainter ( viewport() )
// QTextEdit::paintEvent at the begining of custom PaintEvent
// RESULT: works.
virtual void paintEvent(QPaintEvent *_event)
{
QTextEdit::paintEvent( _event );
QPainter pnt( viewport() );
pnt.setPen( QColor( 0, 0, 0, 0xff ));
pnt.drawRect( 0, 0, width()-1, height()-1);
}
// *** BONUS ***
// QPainter( viewport() ) or QPainter ( this )
// QTextEdit::paintEvent after QPainter() constructor.
// RESULT: Segmentation fault.
virtual void paintEvent(QPaintEvent *_event)
{
QPainter pnt( viewport() );
pnt.setPen( QColor( 0, 0, 0, 0xff ));
QTextEdit::paintEvent( _event ); // WRONG PLACE!!!
pnt.drawRect( 0, 0, width()-1, height()-1);
}

Instead of
QPainter pnt(this);
try
QPainter pnt(viewport());
pnt.setPen(QColor( 0, 0, 0, 0xff ));
pnt.drawRect(viewport()->rect());
viewport() is the paintable area and could be whats causing the issue

Related

Circle user avatar issue

I want to make QLabel with the image circle:
Code:
QLabel *label = new QLabel(this);
QPixmap avatarPixmap(":/Icon/default_avatar.png");
label->setPixmap(avatarPixmap);
label->setStyleSheet("border: 0.5px solid red; border-radius: 50%; background-clip: padding;");
It only rounds the QLabel, not the image. How to fix it? Thanks.
Update:
The only way is to override the paintEvent for QLabel
Code:
void AccountImage::paintEvent(QPaintEvent *event)
{
QPixmap pixmap(":/Icon/default_avatar.png");
QBrush brush(pixmap);
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
painter.setBrush(brush);
painter.drawRoundedRect(0, 0, width(), height(), 100, 100);
QLabel::paintEvent(event);
}
The image is rounded but not properly scaled. Any ideas?
try to set mask on the label like:
int w = // set the width here
int h = // set the height here
QRect *rct = new QRect(0, 0, w, h);
QRegion *reg = new QRegion(*rct, QRegion::Ellipse);
label->setMask(*reg);
see: http://doc.qt.io/archives/qt-4.8/qwidget.html#setMask
The solution by overriding QLabel paintEvent method.
Code:
void AccountImage::paintEvent(QPaintEvent *event)
{
QPixmap pixmap(":/Icon/my_avatar.png");
QPixmap scaled = pixmap.scaled(width(), height(), Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation);
QBrush brush(scaled);
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
painter.setBrush(brush);
painter.drawRoundedRect(0, 0, width(), height(), 100, 100);
QLabel::paintEvent(event);
}
Result:

Applying QPropertyAnimation to QRect

I've created a QRect object
QRect ellipse(10.0 , 10.0 , 10.0 , 10.0);
QPainter painter(this);
painter.setBrush(Qt::red);
painter.drawEllipse(ellipse);
Now I want to animate it using QPropertyAnimation, but since it can only be applied to QObject objects(as far as I get it), I need to somehow convert QRect into QObject. Is there a way to do it?
No need to create a class, you can use your own widget, you must add a new property.
Example:
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QPaintEvent>
#include <QWidget>
class Widget : public QWidget
{
Q_OBJECT
Q_PROPERTY(QRect nrect READ nRect WRITE setNRect)
public:
explicit Widget(QWidget *parent = 0);
~Widget();
QRect nRect() const;
void setNRect(const QRect &rect);
protected:
void paintEvent(QPaintEvent *event);
private:
QRect mRect;
};
#endif // WIDGET_H
widget.cpp
#include "widget.h"
#include <QPainter>
#include <QPropertyAnimation>
Widget::Widget(QWidget *parent) :
QWidget(parent)
{
QPropertyAnimation *animation = new QPropertyAnimation(this, "nrect");
//animation->setEasingCurve(QEasingCurve::InBack);
animation->setDuration(1000);
animation->setStartValue(QRect(0, 0, 10, 10));
animation->setEndValue(QRect(0, 0, 200, 200));
animation->start();
connect(animation, &QPropertyAnimation::valueChanged, [=](){
update();
});
}
Widget::~Widget()
{
}
QRect Widget::nRect() const
{
return mRect;
}
void Widget::setNRect(const QRect &rect)
{
mRect = rect;
}
void Widget::paintEvent(QPaintEvent *event)
{
Q_UNUSED(event)
QRect ellipse(mRect);
QPainter painter(this);
painter.setBrush(Qt::red);
painter.drawEllipse(ellipse);
}
code

CompositionMode_Clear gives me a black rectangle

I've trying to draw a circle with the center transparent using Qt in paintEvent.
I have this code:
void CircleWidget::paintEvent(QPaintEvent * event)
{
QPainter painter;
QPixmap pix1(width(), height());
QPixmap pix2(width()/2, height()/2);
QBitmap mask1(pix1.size());
QBitmap mask2(pix2.size());
mask1.fill(Qt::color0);
mask2.fill(Qt::color0);
pix1.setMask(mask1);
pix2.setMask(mask2);
if (painter.begin(&pix1))
{
painter.setPen(Qt::NoPen);
painter.setRenderHint(QPainter::Antialiasing);
painter.setBrush(QBrush(QColor(0xff, 0x0, 0x0)));
painter.drawEllipse(0, 0, width(), height());
painter.end();
if (painter.begin(&pix2))
{
painter.setPen(Qt::NoPen);
painter.setRenderHint(QPainter::Antialiasing);
painter.setBrush(QBrush(QColor(0x0, 0xff, 0xff)));
painter.drawEllipse(0, 0, width()/2, height()/2);
painter.end();
if (painter.begin(this))
{
painter.drawPixmap(0, 0, pix1);
painter.setCompositionMode(QPainter::CompositionMode_Clear);
painter.drawPixmap(width()/4, height()/4, pix2);
painter.end();
}
}
}
}
But, the result is this:
Do you have any idea? Thanks a lot.

Change QWidget Title Color

I have a custom class called Window that extends QWidget.
#include "window.h"
// This is the base that all (MDI) sub-windows extend off
Window::Window()
{
// Add fake icon to remove icon in all
setWindowIcon(QIcon("."));
}
void Window::paintEvent(QPaintEvent *)
{
QStyleOption opt;
opt.init(this);
QPainter p(this);
style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
}
css:
QWidget:title
{
background-color: QLinearGradient( x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #ffa02f, stop: 1 #ca0619);
}
When I do this the buttons (minimize, maximize and close) disappear (as seen in the picture below).
So my question, how do I properly style my Window titlebar?

Erase a row from a Gtkmm::Table

How do I erase a row from a Gtkmm::Table in C++?
Documentation is really poor :-(. I have tried using resize() with no effect, and also calling remove at a Gtk::Widget& that I obtained through get_children(), but the I get segfault...
If Gtkmm::Table is GtkTable then gtk_container_remove should, probably, do what you need. I've made a small example (see below) it removes all label widget from the table container.
#include <gtk/gtk.h>
void remove_widgets(GtkWidget *widget, gpointer data)
{
if (GTK_IS_LABEL(widget))
{
g_print("remove label\n");
gtk_container_remove(GTK_CONTAINER(data), widget);
}
}
void remove_rows(GtkWidget *button, GtkWidget* data)
{
g_print("remove rows with labels\n");
gtk_container_foreach(GTK_CONTAINER(data), remove_widgets, data);
}
int main( int argc, char *argv[])
{
GtkWidget *window;
GtkWidget *table;
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
gtk_widget_set_size_request (window, 300, 250);
gtk_window_set_resizable(GTK_WINDOW(window), FALSE);
gtk_window_set_title(GTK_WINDOW(window), "Test Table Remove");
gtk_container_set_border_width(GTK_CONTAINER(window), 15);
table = gtk_table_new(3, 2, FALSE);
gtk_table_set_col_spacings(GTK_TABLE(table), 3);
GtkWidget *label0 = gtk_label_new("Test Label 0");
gtk_table_attach(GTK_TABLE(table), label0, 0, 1, 0, 1, GTK_FILL, GTK_FILL, 0, 0);
GtkWidget *label1 = gtk_label_new("Test Label 1");
gtk_table_attach(GTK_TABLE(table), label1, 1, 2, 0, 1, GTK_FILL, GTK_FILL, 0, 0);
GtkWidget *label2 = gtk_label_new("Test Label 3");
gtk_table_attach(GTK_TABLE(table), label2, 0, 1, 1, 2, GTK_FILL, GTK_FILL, 0, 0);
GtkWidget *label3 = gtk_label_new("Test Label 4");
gtk_table_attach(GTK_TABLE(table), label3, 1, 2, 1, 2, GTK_FILL, GTK_FILL, 0, 0);
GtkWidget *button0 = gtk_button_new_with_label("Remove Labels");
gtk_widget_set_size_request(button0, 70, 30);
gtk_table_attach(GTK_TABLE(table), button0, 0, 1, 2, 3, GTK_FILL, GTK_FILL, 0, 0);
g_signal_connect(G_OBJECT(button0), "clicked", G_CALLBACK(remove_rows), table);
gtk_container_add(GTK_CONTAINER(window), table);
g_signal_connect_swapped(G_OBJECT(window), "destroy", G_CALLBACK(gtk_main_quit), G_OBJECT(window));
gtk_widget_show_all(window);
gtk_main();
return 0;
}
hope this helps, regards