Unable to draw onto a scrollarea, with all code pasted



  • I'm trying to draw many rectangles in a scrollarea, but my tiny demo below did never work
    The mainwindow is below. The code below can work, but not scrollable, and when I change 0 to 1, enabling the scrollarea, nothing displayed.

    The code below is all I have. I really can't fingure out what's wrong here.

    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
    {
        if (0) {
            QScrollArea *scrollArea = new QScrollArea(this);
            scrollArea->setBackgroundRole(QPalette::Dark);
            QmQTargetCanvas *targetCanvas = new QmQTargetCanvas(scrollArea);
            scrollArea->setWidget(targetCanvas);
        } else {
            QmQTargetCanvas *targetCanvas = new QmQTargetCanvas(this);
            this->setCentralWidget(targetCanvas);
        }
        this->showMaximized();
    }
    
    QmQTargetCanvas::QmQTargetCanvas(QWidget *parent) :
        QWidget(parent)
    {
        setBackgroundRole(QPalette::Base);
        setAutoFillBackground(true);
    }
    
    void QmQTargetCanvas::paintEvent(QPaintEvent * /* event */)
    {
        QRect rect(10, 20, 80, 60);
    
        QPainter painter(this);
        painter.setPen(QPen(Qt::red, 2, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin ));
        QRadialGradient radialGradient(50, 50, 50, 80, 20);
        radialGradient.setColorAt(0.0, Qt::white);
        radialGradient.setColorAt(0.2, Qt::darkCyan);
        radialGradient.setColorAt(1.0, Qt::black);
        painter.setBrush(radialGradient);
    
        for (int x = 0; x < width(); x += 100) {
            for (int y = 0; y < 900; y += 100) {
                painter.save();
                painter.translate(x, y);
                painter.drawRoundedRect(rect, 25, 25, Qt::RelativeSize);
                painter.drawText(rect, Qt::AlignCenter, QString::number(height()));
            }
        }
    
        painter.setPen(palette().dark().color());
        painter.setBrush(Qt::NoBrush);
        painter.drawRect(QRect(0, 0, width() - 1, height() - 1));
    }
    

  • Lifetime Qt Champion

    Hi,

    You're not setting your scrollArea as your MainWindow central widget.



  • Thank you, SGaist. Yes, I forgot to centralize the scrollarea as I did for the canvas.
    What really bothers me is that the scrollarea is not scrollable.

    As you can see, I was intending to draw these rectancles as many as to fill a 900-pixel high canvas, but the painter never drew outside the physical area of visible canvas.
    I tried resize, but it did not work.

        for (int x = 0; x < width(); x += 100) {
            for (int y = 0; y < ***900***; y += 100) {
                painter.save();
                painter.translate(x, y);
                painter.drawRoundedRect(rect, 25, 25, Qt::RelativeSize);
                painter.drawText(rect, Qt::AlignCenter, QString::number(height()));
    
    

  • Lifetime Qt Champion

    You should also set the height you want on the QmQTargetCanvas instance.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.