drawRect not show left and top border line
-
Hi,all, I have a class with my custom paintEvent function, and I will draw four black border line,and I find when I adjust window size, it has different effect.The left and top line is missing and right and bottom line is more bigger .Bellow is my code:
void CustomWidgetBase::drawWidget(QPainter* qp) { int w, h; w = size().width(); h = size().height(); QLinearGradient gradient = QLinearGradient(w, 0, w, h); gradient.setColorAt(0, QColor(m_widgetConfig.m_startColor)); gradient.setColorAt(1, QColor(m_widgetConfig.m_stopColor)); qp->setPen(Qt::black); qp->setBrush(gradient); qp->drawRect(0, 0, w-1, h-1); } void CustomWidgetBase::paintEvent(QPaintEvent* event) { QPainter qp(this); qp.begin(this); drawWidget(&qp); qp.end(); }
sometime, it shows normal
but when I adjust the window, it shows error
I think it the layout has caused the problem.But I don't know how to solve it.Please help me. Thinks a lot.
-
Works fine for me with Qt6.7
class MyWidget : public QWidget { public: using QWidget::QWidget; void paintEvent(QPaintEvent* event) override { QPainter qp(this); int w, h; w = size().width(); h = size().height(); QLinearGradient gradient = QLinearGradient(w, 0, w, h); gradient.setColorAt(0, QColor(Qt::red)); gradient.setColorAt(1, QColor(Qt::green)); qp.setPen(Qt::black); qp.setBrush(gradient); qp.drawRect(0, 0, w - 1, h - 1); qp.end(); } }; int main(int argc, char** argv) { QApplication app(argc, argv); QWidget w; QHBoxLayout* lay = new QHBoxLayout(&w); lay->addWidget(new MyWidget); w.show(); return app.exec(); }
-
@Christian-Ehrlicher Hi, it seems my screen is high dpi. When I set RoundPreferFloor scale policy, it works fine, but the widget size is less smaller.
int main(int argc, char* argv[]){ QApplication::setAttribute(Qt::AA_EnableHighDpiScaling); //QApplication::setHighDpiScaleFactorRoundingPolicy(Qt::HighDpiScaleFactorRoundingPolicy::PassThrough); QApplication::setHighDpiScaleFactorRoundingPolicy(Qt::HighDpiScaleFactorRoundingPolicy::RoundPreferFloor); QApplication a(argc, argv); AppMainWindow w; w.show(); return a.exec(); }
-
You should create a bug report about this with a minimal, compilable example. I took a short look at the code but can't see any obvious here.