Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    QScrollArea how to question (inactive scrollbars) [solved]

    General and Desktop
    2
    6
    1346
    Loading More Posts
    • 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.
    • K
      krix last edited by

      I have a class named avl_widget, the class can draw binary tree data structures. I want to make it to be scrollable. In the example I use only a QLabel.

      Header:

      @#ifndef ALV_DRAW_H
      #define ALV_DRAW_H
      #include <QtGui>

      class AVL_draw : public QWidget
      {
      Q_OBJECT
      public:
      explicit AVL_draw(QWidget parent = 0);
      protected:
      void paintEvent(QPaintEvent
      );
      QLabel l1;
      };

      #endif // ALV_DRAW_H@

      Source:

      @#include "avl_draw.h"

      AVL_draw::AVL_draw(QWidget *parent) :
      QWidget(parent)
      {
      l1.setParent(this);
      l1.setText("X");
      l1.move(320, 240);
      show();
      }

      void AVL_draw::paintEvent(QPaintEvent*)
      {
      QPainter painter(this);
      painter.setRenderHint(QPainter::Antialiasing);
      painter.setPen(Qt::white);
      painter.setBrush(QBrush(Qt::white));
      painter.drawRect(0,0, this->width(), this->height());
      }@

      The parent widget has a layout:

      @#ifndef AVL_WIDGET_H
      #define AVL_WIDGET_H
      #include "avl_draw.h"

      class AVL_widget : public QWidget
      {
      Q_OBJECT
      public:
      explicit AVL_widget(QWidget* parent = 0);
      protected:
      QScrollArea draw_scrollarea;
      QVBoxLayout main;
      QGridLayout buttons;
      QGridLayout buttons_bottom;
      AVL_draw draw;
      };

      #endif // AVL_WIDGET_H
      @

      Source:

      @#include "avl_widget.h"

      AVL_widget::AVL_widget(QWidget *parent) :
      QWidget(parent)
      {
      setLayout(&main);
      main.addLayout(&buttons);

      //main.addWidget(&draw);
      draw_scrollarea.setGeometry(draw.geometry());
      draw_scrollarea.setWidget(&draw);
      draw_scrollarea.setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
      draw_scrollarea.setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
      draw_scrollarea.setWidgetResizable(true);
      main.addWidget(&draw_scrollarea);
      
      main.addLayout(&buttons_bottom);
      

      }
      @

      The scollbars are disabled, but the label is out of range. What is the problem?

      1 Reply Last reply Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        Hi,

        @l1.move(320, 240);@

        You just put it at that fixed position and never update its position or size. Since you don't put it in layout, it's your job to do it. But since your label only contains X and you are painting the widget yourself, why not draw the X yourself at the right place ?

        On a side note, you should use pointers to your widgets/layouts

        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 Reply Quote 0
        • K
          krix last edited by

          Hi,

          Which function can update the position and size?
          I accept your hint, that paint the X to the correct place, I only used QLabel to simplify the code. If I paint graphes with painter to widget, I can not scrolling it too.
          What is the reason that I should use pointers to widgets/layouts?

          Many thanks SGaist!

          1 Reply Last reply Reply Quote 0
          • SGaist
            SGaist Lifetime Qt Champion last edited by

            The resize function for example.

            Have a look at the "Basic drawing example" it should help you get started.

            As for why you should rather do heap allocation see "this ":http://qt-project.org/forums/viewthread/2962 thread. More specifically the answer from peppe from the 4th january 2011

            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 Reply Quote 0
            • K
              krix last edited by

              Unfortunately I could not correct the code. I read more examples but I can not see the problem.

              1 Reply Last reply Reply Quote 0
              • K
                krix last edited by

                After that I used the setMinimumSize() it works. Before the widget has autosizing.

                1 Reply Last reply Reply Quote 0
                • First post
                  Last post