Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. drawRect not show left and top border line
Forum Updated to NodeBB v4.3 + New Features

drawRect not show left and top border line

Scheduled Pinned Locked Moved Unsolved General and Desktop
painteventlayoutdrawrect
4 Posts 2 Posters 685 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jinming
    wrote on last edited by
    #1

    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
    border_ok.png

    but when I adjust the window, it shows error
    border_error.png

    I think it the layout has caused the problem.But I don't know how to solve it.Please help me. Thinks a lot.

    Christian EhrlicherC 1 Reply Last reply
    0
    • J jinming

      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
      border_ok.png

      but when I adjust the window, it shows error
      border_error.png

      I think it the layout has caused the problem.But I don't know how to solve it.Please help me. Thinks a lot.

      Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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();
      }
      

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      J 1 Reply Last reply
      0
      • Christian EhrlicherC Christian Ehrlicher

        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();
        }
        
        J Offline
        J Offline
        jinming
        wrote on last edited by
        #3

        @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();
            }
        Christian EhrlicherC 1 Reply Last reply
        0
        • J jinming

          @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();
              }
          Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved