Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. drawEllipse() draws too many
Qt 6.12 LTS is out! See what's new in the release blog

drawEllipse() draws too many

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 1 Posters 623 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.
  • T Offline
    T Offline
    TomNow99
    wrote on last edited by TomNow99
    #1

    Hi,

    I have QScrollArea and QWidget in this area. That QWidget has QGridLayout and in this grid I have many QButtons in the first column ( I use only this one column, so I can use Vertical Layout rather than QGridLayout ). Now I would like to draw Circles on this QWidget. So in this QWidget I have paintEvent() function like this:

            firstButtonPositionY=buttons.first()->geometry().y()+buttons.first()->height()/2;
            lastButtonPositionY = buttons.last()->geometry().y()+buttons.last()->height()/2;
    

    I get proper values.

    Next I do:

        while( firstButtonPositionY < lastButtonPositionY )
        {
            firstButtonPositionY+=100;
            painter.drawEllipse(QPointF(width()/2, firstButtonPositionY) , 5, 5);
        }
    

    I get too many circles:

    exam.png

    I would like to have only green circles, but I get that red circles too ( in the picture I draw 2 rects: the first and the last buttons ).

    When I change drawElippse to drawRect() or drawPoint() I get what I want.

        painter.drawRect(width()/2-5, firstButtonPositionY , 10, 30);
    
     or 
    
       painter.drawPoint(width()/2,firstButtonPositionY);
    

    I get rects / points only between buttons, not after last Button or before the first Button.

    So what I do wrong with this drawEllipse()?

    EDIT: that problem is when I have many QButtons ( 500+ ).

    EDIT2: The problem is when QWidget has height = 65640. For height = 65490 everything is ok. Pow(2,16) = 65536, so this is very strange.

    EDIT3: When I change my code to:

        while( firstButtonPositionY < 100 )       // lastButtonPositionY is for example 300000
        {
            firstButtonPositionY+=1;
            painter.drawEllipse(QPointF(width()/2, firstButtonPositionY) , 5, 5);
        }
    

    I get 100 long lines in 5 different places. So it looks like my QWidget is end on 2^16 height and started next. Next Widget started on 2 * 2^16, next 3* 2^16, next 4* 2^16.
    exam2.png

    EDIT 4

    Please check that code:

    mainWindow.h:

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    #include <QScrollArea>
    #include "mywidget.h"
    #include <QGridLayout>
    
    QT_BEGIN_NAMESPACE
    namespace Ui { class MainWindow; }
    QT_END_NAMESPACE
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        MainWindow(QWidget *parent = nullptr);
        ~MainWindow();
    
    private:
        Ui::MainWindow *ui;
        QScrollArea *area;
        myWidget *widget;
        QGridLayout *grid;
    };
    #endif // MAINWINDOW_H
    

    mainWindow.cpp

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        area = new QScrollArea;
        widget = new myWidget;
        grid = new QGridLayout;
    
        centralWidget()->setLayout(grid);
    
        grid->addWidget(area);
        area->setWidget(widget);
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    

    myWidget.h

    #ifndef MYWIDGET_H
    #define MYWIDGET_H
    #include <QWidget>
    #include <QPaintEvent>
    #include <QPushButton>
    #include <QGridLayout>
    class myWidget: public QWidget
    {
        Q_OBJECT
        void paintEvent(QPaintEvent *event);
        QGridLayout *grid;
        QVector <QPushButton *> buttons;
    public:
        myWidget(QWidget *parent = nullptr);
    };
    
    #endif // MYWIDGET_H
    

    myWidget.cpp

    #include "mywidget.h"
    #include <QPainter>
    myWidget::myWidget(QWidget *parent): QWidget(parent)
    {
        grid = new QGridLayout;
        setLayout(grid);
    
        for(int i=0;i<1000;i++)
        {
            buttons.append(new QPushButton);
            buttons.last()->setFixedSize(50,50);
            grid->addWidget(buttons.last(),i,0);
        }
        grid->setSpacing(100);
        resize(minimumSizeHint());
    }
    
    void myWidget::paintEvent(QPaintEvent *event)
    {
        QPainter painter(this);
    
        int begin = buttons.first()->y()+ buttons.first()->height();
        
        int xButtons = buttons.first()->x()+buttons.first()->width()/2;
    
        for(int i=begin; i<300;i++)            
        {
            painter.drawEllipse(QPointF(xButtons, i),1,1);
        }
    
        QWidget::paintEvent(event);
    }
    

    And pictures:

    first.png
    second.png
    third.png

    I would like to draw a few circles only at the beginning of myWidget

        for(int i=begin; i<300;i++)            
        {
            painter.drawEllipse(QPointF(xButtons, i),1,1);
        }
    

    But there are circles in 3 places ( look at the picture with red and green circles ).

    1 Reply Last reply
    0
    • T Offline
      T Offline
      TomNow99
      wrote on last edited by
      #2

      I add the fourth EDIT - I think the most important

      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
      • Unsolved