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
QtWS25 Last Chance

drawRect not show left and top border line

Scheduled Pinned Locked Moved Unsolved General and Desktop
painteventlayoutdrawrect
4 Posts 2 Posters 638 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 2 Oct 2023, 17:32 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.

    C 1 Reply Last reply 2 Oct 2023, 17:51
    0
    • J jinming
      2 Oct 2023, 17:32

      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.

      C Offline
      C Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 2 Oct 2023, 17:51 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 4 Oct 2023, 07:38
      0
      • C Christian Ehrlicher
        2 Oct 2023, 17:51

        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 4 Oct 2023, 07:38 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();
            }
        C 1 Reply Last reply 4 Oct 2023, 10:21
        0
        • J jinming
          4 Oct 2023, 07:38

          @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();
              }
          C Offline
          C Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 4 Oct 2023, 10:21 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

          2/4

          2 Oct 2023, 17:51

          • Login

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