QOpenGLWidget draw line sometimes invisible or bold - qt

I'm subclassing QOpenGLWidget for drawing a crosshair follow mouse. But drawing in QOpenGLWidget is little weird. The horizontal line could disappear or be bold in some height while I moving the mouse up and down. The problem won't appeare in QWidget,Why?
class OpenGLWidget : public QOpenGLWidget {
public:
OpenGLWidget(QWidget *parent = nullptr) : QOpenGLWidget(parent) {
setMouseTracking(true);
}
void mouseMoveEvent(QMouseEvent *event) {
m_mousePoint = event->pos();
update();
}
void paintGL() {
QPainter p(this);
p.setPen(Qt::white);
p.drawLine(QLineF(0, m_mousePoint.y(), width(), m_mousePoint.y()));
p.drawLine(QLineF(m_mousePoint.x(), 0, m_mousePoint.x(), height()));
}
QPointF m_mousePoint;
};
the horizontal line looks bold
the horizontal line disapeared here

Related

Why doesn't child QGraphicsObject repaint its background correctly

The QGraphicsObject in green rectangle, it is the parent of the QGraphicsObject in red rectangle.
childInRed->setParentItem(this);
When I drag the parent object in green rect and move it fast, the background of the child object in red rect is not repainted correctly.
I know I can use update in the parent's mouseMoveEvent force the child to repaint. But this is not good, because I don't need to repaint the parent at all.
#include "asdf.h"
#include <QtWidgets/QGraphicsScene>
#include <QtWidgets/QGraphicsView>
#include <QtWidgets>
class CTestGraphicsObject : public QGraphicsObject
{
public:
QColor m_c;
CTestGraphicsObject(QColor c)
: QGraphicsObject(NULL)
, m_c(c)
{
setFlag(QGraphicsItem::ItemIsMovable, true);
setFlag(QGraphicsItem::ItemIsFocusable, true);
setFlag(QGraphicsItem::ItemIsSelectable, true);
auto effect = new QGraphicsDropShadowEffect;
effect->setOffset(4, 4);
effect->setBlurRadius(20);
setGraphicsEffect(effect);
}
virtual QRectF boundingRect() const override
{
auto rc = QRectF(0, 0, 100, 100);
return rc;
}
virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override
{
painter->setPen(QPen(m_c));
painter->drawRect(this->boundingRect());
}
};
asdf::asdf(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
auto s = new QGraphicsScene(this);
auto v = new QGraphicsView;
v->setScene(s);
CTestGraphicsObject* pParent = new CTestGraphicsObject(Qt::green);
CTestGraphicsObject* pChild = new CTestGraphicsObject(Qt::red);
pChild->setParentItem(pParent);
pChild->setPos(0, 100);
s->addItem(pParent);
s->addItem(pChild);
QVBoxLayout* l = new QVBoxLayout(this->centralWidget());
l->addWidget(v);
}
asdf::~asdf()
{
}
The QGraphicsDropShadowEffect causes this problem, It seems I'm not using it in right way.
According to the Qt documentation, the scene uses the bounding rect and region to define the area to repainted when an item is updated (moved in your case).
If you child is outside its parent, the scene will miss some part when repainting...
Extend the bouding rect/region to cover its children.
If you do something like that, it will work:
virtual QRectF boundingRect() const override
{
if (this->childrenBoundingRect().isEmpty()) // No children
return QRectF(0, 0, 100, 100);
return QRectF(0, 0, 100, 100).united(this->childrenBoundingRect());
}
virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override
{
painter->setPen(QPen(m_c));
painter->drawRect(QRectF(0, 0, 100, 100));
}

QRubberBand and MPlayer in QLabel

I have already tried many solution that are provided on stackoverflow to make it transparent.
I want to make QRubberBand transparent and i am also facing problem regarding that green color which is due to mplayer.
#include "physician.h"
#include "ui_physician.h"
Physician::Physician(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::Physician)
{
ui->setupUi(this);
ui->sendROIButton->setStyleSheet(
"background-color: #d9d9d9;"
"border-radius: 10px;"
"color: Black; "
"font-size: 15px;"
);
}
Physician::~Physician()
{
delete ui;
}
void Physician::mouseMoveEvent(QMouseEvent *e)
{
rubberBand->hide();
bottomRight = e->pos();
QRect rect = QRect(topLeft, bottomRight).normalized();
rubberBand->setGeometry(rect);//Area Bounding
QToolTip::showText(e->globalPos(), QString("%1,%2")
.arg(rubberBand->size().width())
.arg(rubberBand->size().height()), this);
}
void Physician::mousePressEvent(QMouseEvent *e)
{
wWidth=ui->videoShowLabel->width();
wHeight = ui->videoShowLabel->height();
rubberBand->setGeometry(QRect(0, 0, 0, 0).normalized());
rubberBand->hide();
topLeft = e->pos();
}
void Physician::mouseReleaseEvent(QMouseEvent *e){
rubberBand->show();
}
void Physician::on_manualROIRadioButton_clicked()
{
rubberBand = new QRubberBand(QRubberBand::Rectangle, this);
}
void Physician::on_autoROIRadioButton_clicked()
{
QString winNuber= QString::number((int)(ui->videoShowLabel->winId()));
QStringList argsList ;
argsList << "-slave" << "-quiet" << "-wid" << winNuber << "zoom" << "-
vo" << "gl" << "C:/../../../Physician21/PhotoshopQML.mkv";
mplayer_proc = new QProcess;
mplayer_proc-
>start("C:/../../../PhysicianTest/mplayer/mplayer.exe",argsList);
}
Firstly, regarding "QRubberBand should work only on that QLabel". You need to make the QLabel the parent of the QRubberBand to achieve that.
Secondly, regarding transparency I assume you mean that the output from mplayer should be visible through the rectangle painted by the QRubberBand? I'm not sure you will be able to do that. Doing so would required the rubber band painting logic to act as a compositor but in order to do that it needs to know both the source and destination(mplayer) images. Since mplayer draws directly on the underlying native window Qt has know knowledge of the current destination image and so can not merge the source image with it. I think your best bet would be to find/generate a style that causes QRubberBand to draw the rectangle outline only -- not sure if that's possible. You could subclass it and do your own painting with something like...
class rubber_band: public QRubberBand {
using super = QRubberBand;
public:
template<typename... Types>
explicit rubber_band (const Types &... args)
: super(args...)
{}
protected:
virtual void paintEvent (QPaintEvent *event) override
{
QPainter painter(this);
painter.setPen(Qt::red);
painter.setBrush(Qt::NoBrush);
painter.drawRect(rect().adjusted(0, 0, -1, -1));
}
};
The above could still leave visual artifacts on the widget though.

How to resize a frameless widget in Qt

I am researching on how to resize the frameless widget in Qt 5.1. Due to cross-platform consideration, Windows-related codes will not be appropriate.
Now my method is: when a user hovers mouse at the edge of the window(widget),cursor shape changes to hint the window can be resized at that time. After clicking and then drag, a rubberband is being shown to demonstrate the rectangle area that the window would resize to. Once the user releases the mouse, rubberband disappears, and the window then resize to the desired size. Have I made it clear?
Since Qt can't make drawing directly on the screen, as a makeshift, a full screenshot(put in a QLabel) is utilized to simulate the actual screen, and a rubberband can then be constructed in the full-screen QLabel. It seems to the users that they are dragging the window in the real window screen.
My problem is: after the user clicked the main widget,though a full-screen QLabel is shown, the rubberband refuses to appear. Instead, only after you release mouse first and then click-drag again can the rubberband appear.
Although I have sent one "MouseButtonRelease" event to the main widget and a "MouseMove" event to the label, it doesn't seem to work. Any hint or suggestion will be highly appreciated, thanks a lot!
(To simplify the code, any other extra codes have been removed)
mainwidget.h
class MainWidget : public QWidget
{
Q_OBJECT
public:
MainWidget(QWidget *parent = 0);
~MainWidget();
private:
QScreen *screen;
QLabel *fullScreenLabel;
QPixmap fullScreenPixmap;
QRubberBand *rubberBand;
protected:
virtual bool eventFilter(QObject *o, QEvent *e);
void mousePressEvent(QMouseEvent *e);
};
mainwidget.cpp
MainWidget::MainWidget(QWidget *parent)
: QWidget(parent)
{
this->setWindowFlags(Qt::FramelessWindowHint);
screen = QGuiApplication::primaryScreen();
rubberBand=NULL;
fullScreenLabel=new QLabel();
fullScreenLabel->installEventFilter(this);
resize(600,450);
}
MainWidget::~MainWidget()
{
delete fullScreenLabel;
}
bool MainWidget::eventFilter(QObject *o, QEvent *e)
{
if(o!=fullScreenLabel)
return QWidget::eventFilter(o,e);
QMouseEvent *mouseEvent=static_cast<QMouseEvent*>(e);
if(mouseEvent->type()==QEvent::MouseMove)
{
qDebug()<<"label mouse move: "<< mouseEvent->pos();
if(!rubberBand)
{
rubberBand=new QRubberBand(QRubberBand::Rectangle,fullScreenLabel);
rubberBand->setGeometry(QRect(this->pos(),QSize()));
rubberBand->show();
}
rubberBand->setGeometry(QRect(this->pos(),mouseEvent->pos()).normalized());
return true;
}
if((mouseEvent->button()==Qt::LeftButton)
&& (mouseEvent->type()==QEvent::MouseButtonRelease))
{
if(rubberBand)
{
rubberBand->hide();
delete rubberBand;
rubberBand=NULL;
}
return true;
}
return false;
}
void MainWidget::mousePressEvent(QMouseEvent *e)
{
if(screen)
{
fullScreenPixmap=QPixmap();
fullScreenPixmap=screen->grabWindow(0,0,0,-1,-1);
}
fullScreenLabel->setPixmap(fullScreenPixmap);
fullScreenLabel->showFullScreen();
QMouseEvent clickEvent(QEvent::MouseButtonRelease,
e->pos(),
Qt::LeftButton,
Qt::LeftButton,
Qt::NoModifier);
qApp->sendEvent(this,&clickEvent);
QMouseEvent moveEvent(QEvent::MouseMove,
this->pos(),
Qt::NoButton,
Qt::NoButton,
Qt::NoModifier);
qApp->sendEvent(fullScreenLabel,&moveEvent);
}
You may research how it's done in Qt - QSizeGrip. There are same question

Why is the widget not getting resizeEvent?

I am trying to implement a new custom scrollbar. In order to do so, I have placed it onto the parent manually with this->setParent(area);
class QMacScrollBar : public QScrollBar
{
Q_OBJECT
public:
QMacScrollBar(QAbstractScrollArea *area, Qt::Orientation orientation, QWidget *parent = 0);
virtual void paintEvent ( QPaintEvent * );
QAbstractScrollArea *area;
signals:
public slots:
virtual void resizeEvent(QResizeEvent *event);
};
QMacScrollBar::QMacScrollBar(QAbstractScrollArea *area, Qt::Orientation orientation, QWidget *parent) :
QScrollBar(area)
{
this->setMouseTracking(true);
this->setOrientation(orientation);
this->setParent(area);
this->area = area;
}
void QMacScrollBar::resizeEvent(QResizeEvent *event){
QRect geometry = QRect(QPoint(0, 0), event->size());
if (area != NULL){
geometry.setHeight(area->height());
geometry.moveRight(area->width());
geometry.adjust(-5, 0, -5, 0);
this->setGeometry(geometry);
}
QScrollBar::resizeEvent(event);
}
I do get the first few resizes, but none after that. So far my guess is that when I resize the window, the QScrollArea doesnt actually resize, it just hides more of the viewport. I need the resize event because I need to move the place that I display the bar. How do I fix this?
EDIT: The only resizes I am getting are the ones that I spawn via connecting to the underlying scrollbar's setRange signal.

How to make a QWidget alpha-transparent

I need to create an alpha transparent widget, it's basically a navigation bar with a shadow and the widgets below need to be partially visible through the shadow. The widget loads a PNG then draws it on the paint event. The problem is that the shadow is all black and is not alpha-transparent.
This is the code I'm currently using:
NavigationBar::NavigationBar(QWidget *parent) : XQWidget(parent) {
backgroundPixmap_ = new QPixmap();
backgroundPixmap_->load(FilePaths::skinFile("NavigationBarBackground.png"), "png");
setAttribute(Qt::WA_NoBackground, true); // This is supposed to remove the background but there's still a (black) background
}
void NavigationBar::paintEvent(QPaintEvent* event) {
QWidget::paintEvent(event);
QPainter painter(this);
int x = 0;
while (x < width()) {
painter.drawPixmap(x, 0, backgroundPixmap_->width(), backgroundPixmap_->height(), *backgroundPixmap_);
x += backgroundPixmap_->width();
}
}
Does anybody know what I need to change to make sure the widget is really transparent?
You're doing too much work :-)
The setAttribute call is not necessary. By default, a widget will not draw anything on its background (assuming Qt >= 4.1). Calling QWidget::paintEvent is also unnecessary - you don't want it to do anything.
Rather than doing the pattern fill yourself, let Qt do it with a QBrush:
NavigationBar::NavigationBar(QWidget *parent) : XQWidget(parent) {
backgroundPixmap_ = new QPixmap();
backgroundPixmap_->load(FilePaths::skinFile("NavigationBarBackground.png"), "png");
// debug check here:
if (!backgroundPixmap_->hasAlphaChannel()) {
// won't work
}
}
void NavigationBar::paintEvent(QPaintEvent* event) {
QPainter painter(this);
painter.fillRect(0, 0, width(), height(), QBrush(*backgroundPixmap));
}
Adjust the height parameter if you don't want the pattern to repeat vertically.
Are you sure your PNG file is actually transparent? The following (which is essentially what you are doing) is working for me. If this fails on your machine, perhaps include what version of Qt you are using, and what platform.
#include <QtGui>
class TransparentWidget : public QWidget {
public:
TransparentWidget()
: QWidget(),
background_pixmap_(":/semi_transparent.png") {
setFixedSize(400, 100);
}
protected:
void paintEvent(QPaintEvent *) {
QPainter painter(this);
int x = 0;
while (x < width()) {
painter.drawPixmap(x, 0, background_pixmap_);
x += background_pixmap_.width();
}
}
private:
QPixmap background_pixmap_;
};
class ParentWidget : public QWidget {
public:
ParentWidget() : QWidget() {
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(new TransparentWidget);
layout->addWidget(new QPushButton("Button"));
setLayout(layout);
setBackgroundRole(QPalette::Dark);
setAutoFillBackground(true);
}
};
int main(int argc, char **argv) {
QApplication app(argc, argv);
ParentWidget w;
w.show();
return app.exec();
}

Resources