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. Unable to draw onto a scrollarea, with all code pasted
Forum Updated to NodeBB v4.3 + New Features

Unable to draw onto a scrollarea, with all code pasted

Scheduled Pinned Locked Moved Solved General and Desktop
qscrollableqpainter
4 Posts 2 Posters 1.1k Views 1 Watching
  • 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.
  • Q Offline
    Q Offline
    qt_fan_4k
    wrote on last edited by
    #1

    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));
    }
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • Q Offline
        Q Offline
        qt_fan_4k
        wrote on last edited by
        #3

        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()));
        
        
        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

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

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          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